Blame SOURCES/man-db-2.7.6.1-coverity.patch

a6936f
diff --git a/gnulib/lib/getopt.c b/gnulib/lib/getopt.c
a6936f
index 7d950af..fa30f13 100644
a6936f
--- a/gnulib/lib/getopt.c
a6936f
+++ b/gnulib/lib/getopt.c
a6936f
@@ -487,7 +487,15 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
a6936f
         const struct option *p;
a6936f
         struct option_list *next;
a6936f
       } *ambig_list = NULL;
a6936f
+# define free_option_list(l)                   \
a6936f
+      while (l != NULL)                        \
a6936f
+        {                                      \
a6936f
+          struct option_list *pn = l->next;    \
a6936f
+          free (l);                            \
a6936f
+          l = pn;                              \
a6936f
+        }
a6936f
       int exact = 0;
a6936f
+      int ambig = 0;
a6936f
       int indfound = -1;
a6936f
       int option_index;
a6936f
 
a6936f
@@ -521,15 +529,25 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
a6936f
               {
a6936f
                 /* Second or later nonexact match found.  */
a6936f
                 struct option_list *newp = malloc (sizeof (*newp));
a6936f
-                newp->p = p;
a6936f
-                newp->next = ambig_list;
a6936f
-                ambig_list = newp;
a6936f
+                if (newp == NULL)
a6936f
+                  {
a6936f
+                    ambig = 1; /* Use simpler fallback message.  */
a6936f
+                    free_option_list (ambig_list);
a6936f
+                    ambig_list = NULL;
a6936f
+                    break;
a6936f
+                  }
a6936f
+                else
a6936f
+                  {
a6936f
+                    newp->p = p;
a6936f
+                    newp->next = ambig_list;
a6936f
+                    ambig_list = newp;
a6936f
+                  }
a6936f
               }
a6936f
           }
a6936f
 
a6936f
-      if (ambig_list != NULL && !exact)
a6936f
+      if ((ambig || ambig_list) && !exact)
a6936f
         {
a6936f
-          if (print_errors)
a6936f
+          if (print_errors && ambig_list)
a6936f
             {
a6936f
               struct option_list first;
a6936f
               first.p = pfound;
a6936f
@@ -585,18 +603,20 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
a6936f
               fputc ('\n', stderr);
a6936f
 #endif
a6936f
             }
a6936f
+          else if (print_errors && ambig)
a6936f
+            {
a6936f
+              fprintf (stderr,
a6936f
+                       _("%s: option '%s' is ambiguous\n"),
a6936f
+                       argv[0], argv[d->optind]);
a6936f
+            }
a6936f
           d->__nextchar += strlen (d->__nextchar);
a6936f
           d->optind++;
a6936f
           d->optopt = 0;
a6936f
+          free_option_list (ambig_list);
a6936f
           return '?';
a6936f
         }
a6936f
 
a6936f
-      while (ambig_list != NULL)
a6936f
-        {
a6936f
-          struct option_list *pn = ambig_list->next;
a6936f
-          free (ambig_list);
a6936f
-          ambig_list = pn;
a6936f
-        }
a6936f
+      free_option_list (ambig_list);
a6936f
 
a6936f
       if (pfound != NULL)
a6936f
         {
a6936f
diff --git a/lib/decompress.c b/lib/decompress.c
a6936f
index a176a9d..2be532f 100644
a6936f
--- a/lib/decompress.c
a6936f
+++ b/lib/decompress.c
a6936f
@@ -48,11 +48,18 @@
a6936f
 static void decompress_zlib (void *data ATTRIBUTE_UNUSED)
a6936f
 {
a6936f
 	gzFile zlibfile;
a6936f
+	int fd;
a6936f
 
a6936f
-	zlibfile = gzdopen (dup (STDIN_FILENO), "r");
a6936f
-	if (!zlibfile)
a6936f
+	fd = dup (STDIN_FILENO);
a6936f
+	if (fd < 0)
a6936f
 		return;
a6936f
 
a6936f
+	zlibfile = gzdopen (fd, "r");
a6936f
+	if (!zlibfile) {
a6936f
+		close (fd);
a6936f
+		return;
a6936f
+	}
a6936f
+
a6936f
 	for (;;) {
a6936f
 		char buffer[4096];
a6936f
 		int r = gzread (zlibfile, buffer, 4096);
a6936f
diff --git a/lib/encodings.c b/lib/encodings.c
a6936f
index d982827..ec8fb6b 100644
a6936f
--- a/lib/encodings.c
a6936f
+++ b/lib/encodings.c
a6936f
@@ -605,8 +605,10 @@ char *find_charset_locale (const char *charset)
a6936f
 				if (setlocale (LC_CTYPE, locale)) {
a6936f
 					free (encoding);
a6936f
 					goto out;
a6936f
-				} else
a6936f
+				} else {
a6936f
+					free (locale);
a6936f
 					locale = NULL;
a6936f
+				}
a6936f
 			}
a6936f
 			free (encoding);
a6936f
 		}
a6936f
diff --git a/src/man.c b/src/man.c
a6936f
index cb0930b..12dca6e 100644
a6936f
--- a/src/man.c
a6936f
+++ b/src/man.c
a6936f
@@ -1404,6 +1404,7 @@ static pipeline *make_roff_command (const char *dir, const char *file,
a6936f
 		pipeline_command (p, cmd);
a6936f
 	}
a6936f
 
a6936f
+	free (fmt_prog);
a6936f
 	free (page_encoding);
a6936f
 	free (raw_pp_string);
a6936f
 	return p;
a6936f
diff --git a/src/mandb.c b/src/mandb.c
a6936f
index 550af8f..7ae0d02 100644
a6936f
--- a/src/mandb.c
a6936f
+++ b/src/mandb.c
a6936f
@@ -555,7 +555,7 @@ static int process_manpath (const char *manpath, int global_manpath,
a6936f
 	struct tried_catdirs_entry *tried;
a6936f
 	struct stat st;
a6936f
 	int run_mandb = 0;
a6936f
-	struct dbpaths *dbpaths;
a6936f
+	struct dbpaths *dbpaths = NULL;
a6936f
 	int amount = 0;
a6936f
 
a6936f
 	if (global_manpath) { 	/* system db */
a6936f
@@ -571,8 +571,10 @@ static int process_manpath (const char *manpath, int global_manpath,
a6936f
 	tried->seen = 0;
a6936f
 	hashtable_install (tried_catdirs, catpath, strlen (catpath), tried);
a6936f
 
a6936f
-	if (stat (manpath, &st) < 0 || !S_ISDIR (st.st_mode))
a6936f
-		return 0;
a6936f
+	if (stat (manpath, &st) < 0 || !S_ISDIR (st.st_mode)) {
a6936f
+		amount = 0;
a6936f
+		goto out;
a6936f
+	}
a6936f
 	tried->seen = 1;
a6936f
 
a6936f
 	if (single_filename) {
a6936f
@@ -615,10 +617,13 @@ static int process_manpath (const char *manpath, int global_manpath,
a6936f
 #endif /* SECURE_MAN_UID */
a6936f
 
a6936f
 out:
a6936f
-	cleanup_sigsafe (dbpaths);
a6936f
-	pop_cleanup (cleanup_sigsafe, dbpaths);
a6936f
-	cleanup (dbpaths);
a6936f
-	pop_cleanup (cleanup, dbpaths);
a6936f
+	if (dbpaths) {
a6936f
+		cleanup_sigsafe (dbpaths);
a6936f
+		pop_cleanup (cleanup_sigsafe, dbpaths);
a6936f
+		cleanup (dbpaths);
a6936f
+		pop_cleanup (cleanup, dbpaths);
a6936f
+	}
a6936f
+
a6936f
 	free (database);
a6936f
 	database = NULL;
a6936f
 
a6936f
diff --git a/src/whatis.c b/src/whatis.c
a6936f
index 4cf14a5..f804c0e 100644
a6936f
--- a/src/whatis.c
a6936f
+++ b/src/whatis.c
a6936f
@@ -810,6 +810,8 @@ nextpage:
a6936f
 		free_mandata_elements (&info;;
a6936f
 	}
a6936f
 
a6936f
+	free (found_here);
a6936f
+
a6936f
 	for (i = 0; i < num_pages; ++i)
a6936f
 		free (lowpages[i]);
a6936f
 	free (lowpages);