From 30ad8e35e531e71e3c55c4e8b862974758519cec Mon Sep 17 00:00:00 2001 From: Peter Howkins Date: Wed, 11 Apr 2018 19:31:03 +0100 Subject: [PATCH] libtt: Resolve coverity warnings --- cde/lib/tt/bin/ttauth/process.c | 6 +++++- cde/lib/tt/bin/ttdbserverd/dm_server.C | 2 ++ cde/lib/tt/bin/tttar/tttar_file_utils.C | 1 + cde/lib/tt/lib/api/c/api_file.C | 5 ++--- cde/lib/tt/lib/api/c/api_message.C | 2 +- cde/lib/tt/lib/mp/mp_auth.C | 7 ++++++- cde/lib/tt/mini_isam/iserase.c | 6 +++--- cde/lib/tt/mini_isam/isfcb.c | 27 ++++++++++++++----------- cde/lib/tt/mini_isam/isgarbage.c | 3 +-- cde/lib/tt/mini_isam/isrename.c | 26 ++++++++++++------------ cde/lib/tt/mini_isam/isstart.c | 3 +-- 11 files changed, 50 insertions(+), 38 deletions(-) diff --git a/cde/lib/tt/bin/ttauth/process.c b/cde/lib/tt/bin/ttauth/process.c index 1a217182..65bacdff 100644 --- a/cde/lib/tt/bin/ttauth/process.c +++ b/cde/lib/tt/bin/ttauth/process.c @@ -231,10 +231,12 @@ static int original_umask = 0; /* for restoring */ static char * copystring(const char *src) { - int len = strlen (src); + int len; char *cp; if (!src) return NULL; + + len = strlen (src); cp = malloc (len + 1); if (cp) strcpy (cp, src); @@ -1256,6 +1258,7 @@ do_add(char *inputfilename, int lineno, int argc, char *argv[]) prefix (inputfilename, lineno); fprintf (stderr, "protodata_hex contains odd number of or non-hex characters\n"); + free(protodata); return (1); } } @@ -1290,6 +1293,7 @@ do_add(char *inputfilename, int lineno, int argc, char *argv[]) fprintf (stderr, "authdata_hex contains odd number of or non-hex characters\n"); free (protodata); + free (authdata); return (1); } } diff --git a/cde/lib/tt/bin/ttdbserverd/dm_server.C b/cde/lib/tt/bin/ttdbserverd/dm_server.C index 0b4eb9c8..43ccae01 100644 --- a/cde/lib/tt/bin/ttdbserverd/dm_server.C +++ b/cde/lib/tt/bin/ttdbserverd/dm_server.C @@ -239,6 +239,7 @@ _tt_get_record(int prefix_len, const char *prefix, const char *db_name, char *re isfd = stat(dblong,&statbuf); if (-1==isfd) { _tt_syslog(errstr, LOG_ERR, "%s: %m", dblong); + free(db); free(dblong); return 0; } @@ -300,6 +301,7 @@ _tt_write_record(int prefix_len, const char *prefix, const char *db_name, isfd = stat(dblong,&statbuf); if (-1==isfd) { _tt_syslog(errstr, LOG_ERR, "%s: %m", dblong); + free(db); free(dblong); return 0; } diff --git a/cde/lib/tt/bin/tttar/tttar_file_utils.C b/cde/lib/tt/bin/tttar/tttar_file_utils.C index 0770986b..790bfb29 100644 --- a/cde/lib/tt/bin/tttar/tttar_file_utils.C +++ b/cde/lib/tt/bin/tttar/tttar_file_utils.C @@ -248,6 +248,7 @@ realtrees( _Tt_string_list_ptr paths, bool_t follow_symlinks ) strncat( resolved_path_buf, base, MAXPATHLEN - len ); } + free(dir); } } else { resolved_path = _tt_get_realpath( (char *)path, diff --git a/cde/lib/tt/lib/api/c/api_file.C b/cde/lib/tt/lib/api/c/api_file.C index 643cc7fb..3ab775b0 100644 --- a/cde/lib/tt/lib/api/c/api_file.C +++ b/cde/lib/tt/lib/api/c/api_file.C @@ -426,9 +426,8 @@ _ttds_file_server(const char *filename, const char **hostname) _Tt_string hostname_s; _Tt_string partition; _Tt_string network_path; - - *hostname = ""; - if (filename==0 || hostname==0) { + + if (filename == NULL || hostname == NULL) { return TT_ERR_POINTER; } filename_s = filename; diff --git a/cde/lib/tt/lib/api/c/api_message.C b/cde/lib/tt/lib/api/c/api_message.C index ae1c98ae..fddcbc18 100644 --- a/cde/lib/tt/lib/api/c/api_message.C +++ b/cde/lib/tt/lib/api/c/api_message.C @@ -2476,7 +2476,7 @@ _tt_message_context_bval(Tt_message m, const char *slotname, } *len = value.len(); *val = (unsigned char *)_tt_strdup( value, *len ); - if (val == 0) { + if (*val == NULL) { return TT_ERR_NOMEM; } return TT_OK; diff --git a/cde/lib/tt/lib/mp/mp_auth.C b/cde/lib/tt/lib/mp/mp_auth.C index 458d57eb..b4babaf2 100644 --- a/cde/lib/tt/lib/mp/mp_auth.C +++ b/cde/lib/tt/lib/mp/mp_auth.C @@ -291,6 +291,8 @@ write_auth_file(char *filename) else (void) unlink(tmpnam); + free(tmpnam); + return TT_OK; } Tt_status _Tt_auth:: @@ -306,7 +308,10 @@ retrieve_auth_cookie() return TT_AUTHFILE_ENTRY_MISSING; buff = (char*) malloc(entry->auth_data_length + 1); - if (NULL == buff) return TT_ERR_NOMEM; + if (NULL == buff) { + _tt_FreeAuthFileEntry(entry); + return TT_ERR_NOMEM; + } strncpy(buff, entry->auth_data, entry->auth_data_length); buff[entry->auth_data_length] = '\0'; diff --git a/cde/lib/tt/mini_isam/iserase.c b/cde/lib/tt/mini_isam/iserase.c index 32688e17..667ed1a7 100644 --- a/cde/lib/tt/mini_isam/iserase.c +++ b/cde/lib/tt/mini_isam/iserase.c @@ -159,7 +159,7 @@ _unlink_datfile(isfname) { char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makedat_isfname(namebuf); (void)unlink(namebuf); @@ -172,7 +172,7 @@ _unlink_indfile(isfname) { char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makeind_isfname(namebuf); (void)unlink(namebuf); @@ -185,7 +185,7 @@ _unlink_varfile(isfname) { char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makevar_isfname(namebuf); (void)unlink(namebuf); diff --git a/cde/lib/tt/mini_isam/isfcb.c b/cde/lib/tt/mini_isam/isfcb.c index 375b3a47..8d58957a 100644 --- a/cde/lib/tt/mini_isam/isfcb.c +++ b/cde/lib/tt/mini_isam/isfcb.c @@ -197,8 +197,11 @@ _isfcb_open(isfname, errcode) * Open the UNIX file for .rec file. */ if ((dat_fd = _open_datfile (isfname, &rdonly)) == -1 || errno == EMFILE) { - _amseterrcode(errcode, errno); - return (NULL); + _amseterrcode(errcode, errno); + if(dat_fd != -1) { + close(dat_fd); + } + return (NULL); } /* @@ -640,7 +643,7 @@ _create_datfile(isfname) int fd; char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makedat_isfname(namebuf); fd = open (namebuf, O_CREAT | O_EXCL | O_RDWR, 0666); @@ -663,7 +666,7 @@ _create_indfile(isfname) int fd; char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makeind_isfname(namebuf); fd = open (namebuf, O_CREAT | O_EXCL | O_RDWR, 0666); @@ -686,7 +689,7 @@ _create_varfile(isfname) int fd; char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makevar_isfname(namebuf); fd = open (namebuf, O_CREAT | O_EXCL | O_RDWR, 0666); @@ -709,7 +712,7 @@ _remove_datfile(isfname) { char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makedat_isfname(namebuf); (void) unlink(namebuf); @@ -727,7 +730,7 @@ _remove_indfile(isfname) { char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makeind_isfname(namebuf); (void) unlink(namebuf); @@ -745,7 +748,7 @@ _remove_varfile(isfname) { char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makevar_isfname(namebuf); (void) unlink(namebuf); @@ -766,7 +769,7 @@ _open_datfile(isfname, rdonly) char namebuf[MAXPATHLEN]; int ret; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makedat_isfname(namebuf); if ((ret = open (namebuf, O_RDWR)) != -1) { @@ -797,7 +800,7 @@ _open_indfile(isfname, rdonly) int fd; char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makeind_isfname(namebuf); fd = open (namebuf, (rdonly==TRUE)?O_RDONLY:O_RDWR); @@ -821,7 +824,7 @@ _open_varfile(isfname, rdonly) int fd; char namebuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); _makevar_isfname(namebuf); fd = open (namebuf, (rdonly==TRUE)?O_RDONLY:O_RDWR); @@ -867,7 +870,7 @@ _open2_indfile(fcb) if (fcb->indfd != -1) return (ISOK); - (void) strcpy(namebuf, fcb->isfname); + snprintf(namebuf, sizeof(namebuf), "%s", fcb->isfname); _makeind_isfname(namebuf); (void)fstat(fcb->datfd, &buf); diff --git a/cde/lib/tt/mini_isam/isgarbage.c b/cde/lib/tt/mini_isam/isgarbage.c index c270e5d9..f227ccb5 100644 --- a/cde/lib/tt/mini_isam/isgarbage.c +++ b/cde/lib/tt/mini_isam/isgarbage.c @@ -42,8 +42,7 @@ isgarbage(char * isfname) struct stat statbuf; int count = 0,i; - (void)strcpy(isfname2, isfname); - (void)strcat(isfname2, "~"); + snprintf(isfname2, sizeof(isfname2), "%s~", isfname); if ((isfd = isopen(isfname, ISEXCLLOCK + ISINPUT)) == ISERROR) { goto ERROR; diff --git a/cde/lib/tt/mini_isam/isrename.c b/cde/lib/tt/mini_isam/isrename.c index 42126d49..73cdeb30 100644 --- a/cde/lib/tt/mini_isam/isrename.c +++ b/cde/lib/tt/mini_isam/isrename.c @@ -130,7 +130,7 @@ isrename(oldname, newname) * still thinks that the file exists for a few seconds. */ - (void)strcpy(datfname, oldname); + snprintf(datfname, sizeof(datfname), "%s", oldname); _makedat_isfname(datfname); (void)unlink(datfname); @@ -218,16 +218,16 @@ _rename_datfile(isfname, newname) char namebuf[MAXPATHLEN]; char newbuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); - (void) strcpy(newbuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); + snprintf(newbuf, sizeof(newbuf), "%s", isfname); /* * Replace the last element of the old path with newname. */ _removelast(newbuf); if (strcmp(newbuf, "/") != 0) - (void) strcat(newbuf, "/"); - (void)strcat(newbuf, newname); + snprintf(newbuf, sizeof(newbuf), "%s/", newbuf); + snprintf(newbuf, sizeof(newbuf), "%s%s", newbuf, newname); _makedat_isfname(namebuf); _makedat_isfname(newbuf); @@ -243,16 +243,16 @@ _rename_indfile(isfname, newname) char namebuf[MAXPATHLEN]; char newbuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); - (void) strcpy(newbuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); + snprintf(newbuf, sizeof(newbuf), "%s", isfname); /* * Replace the last element of the old path with newname. */ _removelast(newbuf); if (strcmp(newbuf, "/") != 0) - (void) strcat(newbuf, "/"); - (void)strcat(newbuf, newname); + snprintf(newbuf, sizeof(newbuf), "%s/", newbuf); + snprintf(newbuf, sizeof(newbuf), "%s%s", newbuf, newname); _makeind_isfname(namebuf); _makeind_isfname(newbuf); @@ -268,16 +268,16 @@ _rename_varfile(isfname, newname) char namebuf[MAXPATHLEN]; char newbuf[MAXPATHLEN]; - (void) strcpy(namebuf, isfname); - (void) strcpy(newbuf, isfname); + snprintf(namebuf, sizeof(namebuf), "%s", isfname); + snprintf(newbuf, sizeof(newbuf), "%s", isfname); /* * Replace the last element of the old path with newname. */ _removelast(newbuf); if (strcmp(newbuf, "/") != 0) - (void) strcat(newbuf, "/"); - (void)strcat(newbuf, newname); + snprintf(newbuf, sizeof(newbuf), "%s/", newbuf); + snprintf(newbuf, sizeof(newbuf), "%s%s", newbuf, newname); _makevar_isfname(namebuf); _makevar_isfname(newbuf); diff --git a/cde/lib/tt/mini_isam/isstart.c b/cde/lib/tt/mini_isam/isstart.c index 84f0fad8..0d112e9c 100644 --- a/cde/lib/tt/mini_isam/isstart.c +++ b/cde/lib/tt/mini_isam/isstart.c @@ -591,8 +591,7 @@ _amstart(isfhandle, record, reclen, readmode, _amseterrcode(errcode, ISOK); /* Clean-up work. */ - if (newcrp != NULL) - free(newcrp); + free(newcrp); _isdisk_commit(); /* This will only check * that we unfixed all fixed -- 2.25.1