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

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