bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[wdiff-0.5] PATCH: SIGSEGV + use of tmpnam()


From: A. Guru
Subject: [wdiff-0.5] PATCH: SIGSEGV + use of tmpnam()
Date: Sun, 22 Aug 2004 03:56:07 -0400
User-agent: Mutt/1.4.1i

At compilation:

        gcc  -o wdiff wdiff.o readpipe.o writepipe.o libwd.a -ltermcap
        wdiff.o(.text+0x553): In function `split_file_into_words':
        /usr/local/src/wdiff-0.5/wdiff.c:569: warning: the use of `tmpnam' is 
dangerous, better use `mkstemp'

        gcc -Wall ...
                complains about missing prototype for wait()

At runtime:

        Program receives SIGSEGV on second attempt to fclose() the same file.
        Solution:
                Set global FILE * variables to NULL after each fclose().
                The program already checks whether variable is non NULL
                before second fclose().

Also:

        Confusion between -c and -i options to diff.



--- wdiff.c.orig-0.5    1994-11-06 02:57:23 -0500
+++ wdiff.c     2004-08-22 03:19:11 -0400
@@ -70,6 +70,7 @@ const char *tgetstr ();
 #endif
 
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <setjmp.h>
 #include <signal.h>
 #ifndef RETSIGTYPE
@@ -168,6 +169,7 @@ int interrupted;            /* set when some signa
 # define L_tmpnam PATH_MAX
 #endif
 
+#define TEMP_TEMPLATE "/tmp/wdiff.XXXXXX"
 typedef struct side SIDE;      /* all variables for one side */
 struct side
 {
@@ -175,7 +177,7 @@ struct side
   FILE *file;                  /* original input file */
   int position;                        /* number of words read so far */
   int character;               /* one character look ahead */
-  char temp_name[L_tmpnam];    /* temporary file name */
+  char temp_name[18];          /* temporary file name */
   FILE *temp_file;             /* temporary file */
 };
 SIDE side_array[2];            /* area for holding side descriptions */
@@ -555,6 +557,7 @@ static void
 split_file_into_words (SIDE *side)
 {
   struct stat stat_buffer;     /* for checking if file is directory */
+  int fd;
 
   /* Open files.  */
 
@@ -566,8 +569,9 @@ split_file_into_words (SIDE *side)
         this temporary local file.  Once done, prepare it for reading.
         We do not need the file name itself anymore.  */
 
-      tmpnam (side->temp_name);
-      side->file = fopen (side->temp_name, "w+");
+      strcpy(side->temp_name, TEMP_TEMPLATE);
+      fd = mkstemp(side->temp_name);
+      side->file = fd != -1 ? fdopen(fd, "w+") : NULL;
       if (side->file == NULL)
        error (EXIT_OTHER_REASON, errno, side->temp_name);
       if (unlink (side->temp_name) != 0)
@@ -593,8 +597,9 @@ split_file_into_words (SIDE *side)
   side->character = getc (side->file);
   side->position = 0;
 
-  tmpnam (side->temp_name);
-  side->temp_file = fopen (side->temp_name, "w");
+  strcpy(side->temp_name, TEMP_TEMPLATE);
+  fd = mkstemp(side->temp_name);
+  side->temp_file = fd != -1 ? fdopen(fd, "w") : NULL;
   if (side->temp_file == NULL)
     error (EXIT_OTHER_REASON, errno, side->temp_name);
 
@@ -612,6 +617,7 @@ split_file_into_words (SIDE *side)
       putc ('\n', side->temp_file);
     }
   fclose (side->temp_file);
+  side->temp_file = NULL;
 }
 
 /*-------------------------------------------------------------------.
@@ -877,7 +883,9 @@ reformat_diff_output (void)
   /* Close input files.  */
 
   fclose (left_side->file);
+  left_side->file = NULL;
   fclose (right_side->file);
+  right_side->file = NULL;
 }
 
 
@@ -893,7 +901,7 @@ launch_input_program (void)
   /* Launch the diff program.  */
 
   if (ignore_case)
-    input_file = readpipe (DIFF_PROGRAM, "-c", left_side->temp_name,
+    input_file = readpipe (DIFF_PROGRAM, "-i", left_side->temp_name,
                           right_side->temp_name, NULL);
   else
     input_file = readpipe (DIFF_PROGRAM, left_side->temp_name,
@@ -911,6 +919,7 @@ static void
 complete_input_program (void)
 {
   fclose (input_file);
+  input_file = NULL;
   wait (NULL);
 }
 
@@ -1012,6 +1021,7 @@ complete_output_program (void)
   if (output_file && output_file != stdout)
     {
       fclose (output_file);
+      output_file = NULL;
       wait (NULL);
     }
 
@@ -1135,7 +1145,7 @@ Mandatory arguments to long options are 
 | Main program.         |
 `---------------*/
 
-void
+int
 main (int argc, char *const argv[])
 {
   int option_char;             /* option character */
@@ -1303,6 +1313,7 @@ main (int argc, char *const argv[])
       initialize_strings ();
       reformat_diff_output ();
       fclose (input_file);
+      input_file = NULL;
     }
 
   /* Clean up.  Beware that input_file and output_file might not exist,
@@ -1329,5 +1340,5 @@ main (int argc, char *const argv[])
       || count_changed_left || count_changed_right)
     exit (EXIT_ANY_DIFFERENCE);
 
-  exit (EXIT_NO_DIFFERENCES);
+  return EXIT_NO_DIFFERENCES;
 }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]