Coverity fixes from Nelson
authorSteve Youngs <steve@sxemacs.org>
Sat, 25 Feb 2012 06:19:43 +0000 (16:19 +1000)
committerSteve Youngs <steve@sxemacs.org>
Sat, 25 Feb 2012 06:19:43 +0000 (16:19 +1000)
* merges:
  Coverity: Overrun static CID: 138
  Coverity: CID 681: TAINTED STRING
  Coverity: CID 610-DEAD CODE 611-UNUSED VALUE
  Coverity: TOCTOU: CID 387
  Coverity:Tainted string: CID 384
  Coverity: Tainted string: CID 386

lib-src/etags.c
lib-src/movemail.c
src/dumper.c
src/emacs.c

index b58ff0a..8fcc2e9 100644 (file)
@@ -1516,11 +1516,13 @@ char *file;
 language *lang;
 {
        struct stat stat_buf;
-       FILE *inf;
-       fdesc *fdp;
-       compressor *compr;
-       char *compressed_name, *uncompressed_name;
-       char *ext, *real_name;
+       FILE *inf = NULL;
+       fdesc *fdp = NULL;
+       compressor *compr = NULL;
+       char *compressed_name = NULL, 
+            *uncompressed_name = NULL;
+       char *ext = NULL, 
+            *real_name = NULL;
        int retval;
 
        canonicalize_filename (file);
@@ -1529,9 +1531,8 @@ language *lang;
                error ("skipping inclusion of %s in self.", file);
                return;
        }
-       if ((compr = get_compressor_from_suffix (file, &ext)) == NULL)
+       if ( get_compressor_from_suffix (file, &ext) == NULL)
        {
-               compressed_name = NULL;
                real_name = uncompressed_name = savestr (file);
        }
        else
@@ -1549,52 +1550,50 @@ language *lang;
                        goto cleanup;
        }
 
-       if (stat (real_name, &stat_buf) != 0)
-       {
-               /* Reset real_name and try with a different name. */
-               real_name = NULL;
-               if (compressed_name != NULL) /* try with the given suffix */
+       compr = compressors;
+       do {
+               /* First try to open ... */
+               if (real_name == compressed_name)
                {
-                       if (stat (uncompressed_name, &stat_buf) == 0)
-                               real_name = uncompressed_name;
+                       char *cmd = concat (compr->command, " ", real_name);
+                       inf = (FILE *) popen (cmd, "r");
+                       free (cmd);
                }
-               else                    /* try all possible suffixes */
-               {
-                       for (compr = compressors; compr->suffix != NULL; compr++)
+               else
+                       inf = fopen (real_name, "r");
+               if ( inf != NULL ) {
+                       /* Open was successfull, check it is a regular file */
+                       if (stat (real_name, &stat_buf) == 0 && 
+                           !S_ISREG (stat_buf.st_mode))
+                       {
+                               error ("skipping %s: it is not a regular file.", 
+                                      real_name);
+                               fclose(inf);
+                               inf = NULL;
+                       }
+               } 
+               /* Not else from previous if because inner check may reset inf
+                  to NULL, at which case we will want to try the next?
+                  compressed filename... */
+               if ( inf == NULL ) {
+                       /* Reset real_name and try with a different name. */
+                       free(compressed_name);
+                       real_name = NULL;
+                       if (compressed_name != NULL) 
+                                /* try with the given suffix */
+                       {
+                               compressed_name = NULL;
+                               real_name = uncompressed_name;
+                       }
+                       else if ( compr && compr->suffix != NULL ) 
+                                /* try all possible suffixes */
                        {
                                compressed_name = concat (file, ".", compr->suffix);
-                               if (stat (compressed_name, &stat_buf) != 0)
-                               {
-                                       free (compressed_name);
-                                       compressed_name = NULL;
-                               }
-                               else
-                               {
-                                       real_name = compressed_name;
-                                       break;
-                               }
+                               real_name = compressed_name;
+                               compr++;
                        }
                }
-               if (real_name == NULL)
-               {
-                       perror (file);
-                       goto cleanup;
-               }
-       } /* try with a different name */
-
-       if (!S_ISREG (stat_buf.st_mode))
-       {
-               error ("skipping %s: it is not a regular file.", real_name);
-               goto cleanup;
-       }
-       if (real_name == compressed_name)
-       {
-               char *cmd = concat (compr->command, " ", real_name);
-               inf = (FILE *) popen (cmd, "r");
-               free (cmd);
-       }
-       else
-               inf = fopen (real_name, "r");
+       } while( inf == NULL && real_name != NULL);
        if (inf == NULL)
        {
                perror (real_name);
index ba22f92..f80b95d 100644 (file)
@@ -291,6 +291,7 @@ int main(int argc, char *argv[])
        }
 
        while (optind < argc) {
+               assert(argv[optind] != NULL);
                if (!inname) {
                        inname = argv[optind];
                } else if (!outname) {
index 2f6742b..1b782a9 100644 (file)
@@ -587,19 +587,20 @@ pdump_register_struct(const void *data,
                if (me >= 65536) {
                        stderr_out("Backtrace overflow, loop ?\n");
                        abort();
+               } else {
+                       backtrace[me].obj = 0;
+                       backtrace[me].position = 0;
+                       backtrace[me].offset = 0;
+                       
+                       pdump_add_entry(pdump_get_entry_list(sdesc),
+                                       data, sdesc->size, count);
+                       for (i = 0; i < count; i++) {
+                               pdump_register_sub(
+                                       ((const char*)data) + sdesc->size * i,
+                                       sdesc->description, me);
+                       }
+                       --depth;
                }
-               backtrace[me].obj = 0;
-               backtrace[me].position = 0;
-               backtrace[me].offset = 0;
-
-               pdump_add_entry(pdump_get_entry_list(sdesc),
-                               data, sdesc->size, count);
-               for (i = 0; i < count; i++) {
-                       pdump_register_sub(
-                               ((const char*)data) + sdesc->size * i,
-                               sdesc->description, me);
-               }
-               --depth;
        }
 }
 
index 4375c06..d1d1b78 100644 (file)
@@ -2572,34 +2572,47 @@ main(int argc, char **argv, char **envp)
        int volatile vol_argc = argc;
        char **volatile vol_argv = argv;
        char **volatile vol_envp = envp;
-       /* This is hairy.  We need to compute where the SXEmacs binary was invoked
-          from because temacs initialization requires it to find the lisp
-          directories.  The code that recomputes the path is guarded by the
-          restarted flag.  There are three possible paths I've found so far
-          through this:
-
-          temacs -- When running temacs for basic build stuff, the first main_1
-          will be the only one invoked.  It must compute the path else there
-          will be a very ugly bomb in startup.el (can't find obvious location
-          for doc-directory data-directory, etc.).
-
-          temacs w/ run-temacs on the command line -- This is run to bytecompile
-          all the out of date dumped lisp.  It will execute both of the main_1
-          calls and the second one must not touch the first computation because
-          argc/argv are hosed the second time through.
-
-          sxemacs -- Only the second main_1 is executed.  The invocation path must
-          computed but this only matters when running in place or when running
-          as a login shell.
-
-          As a bonus for straightening this out, SXEmacs can now be run in place
-          as a login shell.  This never used to work.
-
-          As another bonus, we can now guarantee that
-          (concat invocation-directory invocation-name) contains the filename
-          of the SXEmacs binary we are running.  This can now be used in a
-          definite test for out of date dumped files.  -slb */
+       /* This is hairy.  We need to compute where the SXEmacs binary
+          was invoked from because temacs initialization requires it
+          to find the lisp directories.  The code that recomputes the
+          path is guarded by the restarted flag.  There are three
+          possible paths I've found so far through this:
+
+          temacs -- When running temacs for basic build stuff, the
+          first main_1 will be the only one invoked.  It must compute
+          the path else there will be a very ugly bomb in startup.el
+          (can't find obvious location for doc-directory
+          data-directory, etc.).
+
+          temacs w/ run-temacs on the command line -- This is run to
+          bytecompile all the out of date dumped lisp.  It will
+          execute both of the main_1 calls and the second one must
+          not touch the first computation because argc/argv are hosed
+          the second time through.
+
+          sxemacs -- Only the second main_1 is executed.  The
+          invocation path must computed but this only matters when
+          running in place or when running as a login shell.
+
+          As a bonus for straightening this out, SXEmacs can now be
+          run in place as a login shell.  This never used to work.
+
+          As another bonus, we can now guarantee that (concat
+          invocation-directory invocation-name) contains the filename
+          of the SXEmacs binary we are running.  This can now be used
+          in a definite test for out of date dumped files.  -slb 
+       */
+
        int restarted = 0;
+
+       int arg;
+       assert(vol_argv[0] != NULL || vol_argv[0][0] != '\0');
+       assert(argc >= 1);
+       for( arg=1; arg < argc; arg++ ) {
+               assert(vol_argv[arg] != NULL);
+       }
+       assert(vol_argv[argc] == NULL);
+
 #ifdef QUANTIFY
        quantify_stop_recording_data();
        quantify_clear_data();