From 6201d377561c3230ca68e6e9871db8e2912644d2 Mon Sep 17 00:00:00 2001 From: Eric Norum Date: Fri, 14 Dec 2018 11:38:31 -0600 Subject: [PATCH] Remove epicsTempName() routine It's unsafe and generates obnoxious warnings on modern compilers. This also replaces internal useage with epicsTempFile(). There appears to be no external code that calls this routine. --- src/libCom/flex/flex.c | 14 ++---- src/libCom/flex/flexdef.h | 2 - src/libCom/flex/gen.c | 9 +--- src/libCom/misc/truncateFile.c | 61 +++++++++++------------ src/libCom/osi/epicsTempFile.h | 1 - src/libCom/osi/os/WIN32/epicsTempFile.cpp | 22 -------- src/libCom/osi/os/posix/epicsTempFile.cpp | 14 ------ 7 files changed, 35 insertions(+), 88 deletions(-) diff --git a/src/libCom/flex/flex.c b/src/libCom/flex/flex.c index 8d0400c2b..008e5e670 100644 --- a/src/libCom/flex/flex.c +++ b/src/libCom/flex/flex.c @@ -98,7 +98,6 @@ int num_backtracking, bol_needed; FILE *temp_action_file; FILE *backtrack_file; int end_of_buffer_state; -char action_file_name[256>L_tmpnam?256:L_tmpnam]; char **input_files; int num_input_files; char *program_name; @@ -210,10 +209,7 @@ void flexend(int status) else if ( fclose( temp_action_file ) ) flexfatal( "error occurred when closing temporary action file" ); - - else if ( unlink( action_file_name ) ) - flexfatal( "error occurred when deleting temporary action file" ); - } + } if ( status != 0 && outfile_created ) { @@ -598,15 +594,11 @@ get_next_arg: /* used by -C and -S flags in lieu of a "continue 2" control */ if ( (skelfile = fopen( skelname, "r" )) == NULL ) lerrsf( "can't open skeleton file %s", skelname ); - epicsTempName ( action_file_name, sizeof ( action_file_name ) ); - if ( action_file_name[0] == '\0' ) + if ( ( temp_action_file = epicsTempFile () ) == NULL ) { - lerrsf( "can't create temporary file name", "" ); + lerrsf( "can't create temporary action file", "" ); } - if ( ( temp_action_file = fopen ( action_file_name, "w" ) ) == NULL ) - lerrsf( "can't open temporary action file %s", action_file_name ); - lastdfa = lastnfa = num_rules = numas = numsnpairs = tmpuses = 0; numecs = numeps = eps2 = num_reallocs = hshcol = dfaeql = totnst = 0; numuniq = numdup = hshsave = eofseen = datapos = dataline = 0; diff --git a/src/libCom/flex/flexdef.h b/src/libCom/flex/flexdef.h index 79cfa9427..cf972b274 100644 --- a/src/libCom/flex/flexdef.h +++ b/src/libCom/flex/flexdef.h @@ -350,7 +350,6 @@ extern int yymore_really_used, reject_really_used; * temp_action_file - temporary file to hold actions * backtrack_file - file to summarize backtracking states to * infilename - name of input file - * action_file_name - name of the temporary file * input_files - array holding names of input files * num_input_files - size of input_files array * program_name - name with which program was invoked @@ -359,7 +358,6 @@ extern int yymore_really_used, reject_really_used; extern int datapos, dataline, linenum; extern FILE *skelfile, *yyin, *temp_action_file, *backtrack_file; extern char *infilename; -extern char action_file_name[]; extern char **input_files; extern int num_input_files; extern char *program_name; diff --git a/src/libCom/flex/gen.c b/src/libCom/flex/gen.c index aad884074..d0219ec94 100644 --- a/src/libCom/flex/gen.c +++ b/src/libCom/flex/gen.c @@ -1164,13 +1164,8 @@ void make_tables(void) if ( ferror( temp_action_file ) ) flexfatal( "error occurred when writing temporary action file" ); - else if ( fclose( temp_action_file ) ) - flexfatal( "error occurred when closing temporary action file" ); - - temp_action_file = fopen( action_file_name, "r" ); - - if ( temp_action_file == NULL ) - flexfatal( "could not re-open temporary action file" ); + else if ( fseek( temp_action_file, 0L, SEEK_SET) != 0 ) + flexfatal( "error occurred when rewinding temporary action file" ); /* copy prolog from action_file to output file */ action_out(); diff --git a/src/libCom/misc/truncateFile.c b/src/libCom/misc/truncateFile.c index ccb1450c2..4fd83075f 100644 --- a/src/libCom/misc/truncateFile.c +++ b/src/libCom/misc/truncateFile.c @@ -27,7 +27,6 @@ */ epicsShareFunc enum TF_RETURN truncateFile (const char *pFileName, unsigned long size) { - char tmpName[256>L_tmpnam?256:L_tmpnam]; long filePos; FILE *pFile; FILE *ptmp; @@ -67,18 +66,10 @@ epicsShareFunc enum TF_RETURN truncateFile (const char *pFileName, unsigned lon return TF_OK; } - epicsTempName ( tmpName, sizeof (tmpName) ); - if ( tmpName[0] == '\0' ) { - fprintf (stderr,"Unable to create tmp file name?\n"); - fclose (pFile); - return TF_ERROR; - } - - ptmp = fopen (tmpName, "w"); + ptmp = epicsTempFile(); if (!ptmp) { fprintf (stderr, - "File access problems to `%s' because `%s'\n", - tmpName, + "File access problems to temp file because `%s'\n", strerror(errno)); fclose (pFile); return TF_ERROR; @@ -89,48 +80,56 @@ epicsShareFunc enum TF_RETURN truncateFile (const char *pFileName, unsigned lon c = getc (pFile); if (c==EOF) { fprintf (stderr, - "File access problems to `%s' because `%s'\n", - pFileName, + "File access problems to temp file because `%s'\n", strerror(errno)); fclose (pFile); fclose (ptmp); - remove (tmpName); return TF_ERROR; } status = putc (c, ptmp); if (status==EOF) { fprintf(stderr, - "File access problems to `%s' because `%s'\n", - tmpName, + "File access problems to temp file because `%s'\n", strerror(errno)); fclose (pFile); fclose (ptmp); - remove (tmpName); return TF_ERROR; } charNo++; } fclose (pFile); - fclose (ptmp); - status = remove (pFileName); - if (status!=TF_OK) { + pFile = fopen(pFileName, "w"); + if (!pFile) { fprintf (stderr, - "Unable to remove `%s' during truncate because `%s'\n", + "File access problems to `%s' because `%s'\n", pFileName, strerror(errno)); - remove (tmpName); + fclose (ptmp); return TF_ERROR; } - status = rename (tmpName, pFileName); - if (status!=TF_OK) { - fprintf (stderr, - "Unable to rename %s to `%s' because `%s'\n", - tmpName, - pFileName, - strerror(errno)); - remove (tmpName); - return TF_ERROR; + rewind (ptmp); + while ((c = getc (ptmp)) != EOF) { + if (ferror(ptmp)) { + fprintf (stderr, + "File access problems to temp file because `%s'\n", + strerror(errno)); + fclose (pFile); + fclose (ptmp); + return TF_ERROR; + } + status = putc (c, pFile); + if (status==EOF) { + fprintf(stderr, + "File access problems to `%s' because `%s'\n", + pFileName, + strerror(errno)); + fclose (pFile); + fclose (ptmp); + return TF_ERROR; + } } + fclose(ptmp); + fclose(pFile); return TF_OK; } diff --git a/src/libCom/osi/epicsTempFile.h b/src/libCom/osi/epicsTempFile.h index c48e336ec..e20879d04 100644 --- a/src/libCom/osi/epicsTempFile.h +++ b/src/libCom/osi/epicsTempFile.h @@ -20,7 +20,6 @@ extern "C" { #endif -epicsShareFunc void epicsShareAPI epicsTempName(char *pbuf, size_t bufLen); epicsShareFunc FILE * epicsShareAPI epicsTempFile(void); #ifdef __cplusplus diff --git a/src/libCom/osi/os/WIN32/epicsTempFile.cpp b/src/libCom/osi/os/WIN32/epicsTempFile.cpp index 11b835134..2283a8574 100644 --- a/src/libCom/osi/os/WIN32/epicsTempFile.cpp +++ b/src/libCom/osi/os/WIN32/epicsTempFile.cpp @@ -21,28 +21,6 @@ #define epicsExportSharedSymbols #include "epicsTempFile.h" -// -// epicsTempName -// -// allow the teporary file directory to be set with the -// TMP environment varianble -// -extern "C" -epicsShareFunc void epicsShareAPI epicsTempName ( - char * pNameBuf, size_t nameBufLength ) -{ - if ( nameBufLength ) { - pNameBuf[0] = '\0'; - char * pName = _tempnam ( "c:\\tmp", "epics" ); - if ( pName ) { - if ( nameBufLength > strlen ( pName ) ) { - strncpy ( pNameBuf, pName, nameBufLength ); - } - free ( pName ); - } - } -} - // // epicsTmpFile // diff --git a/src/libCom/osi/os/posix/epicsTempFile.cpp b/src/libCom/osi/os/posix/epicsTempFile.cpp index 19b5fb4bc..8e666b4b6 100644 --- a/src/libCom/osi/os/posix/epicsTempFile.cpp +++ b/src/libCom/osi/os/posix/epicsTempFile.cpp @@ -14,20 +14,6 @@ #include "epicsTempFile.h" extern "C" -epicsShareFunc void epicsShareAPI epicsTempName ( - char * pNameBuf, size_t nameBufLength ) -{ - if ( nameBufLength ) { - pNameBuf[0] = '\0'; - char nameBuf[L_tmpnam]; - if ( tmpnam ( nameBuf ) ) { - if ( nameBufLength > strlen ( nameBuf ) ) { - strncpy ( pNameBuf, nameBuf, nameBufLength ); - } - } - } -} - extern "C" epicsShareFunc FILE * epicsShareAPI epicsTempFile ( void )