Blame SOURCES/gdbm-1.17-coverity-fixes.patch

fa3d5a
From 2ff4ae9c745d4b9e6ee36468c81554027f66c35b Mon Sep 17 00:00:00 2001
fa3d5a
From: Sergey Poznyakoff <gray@gnu.org>
fa3d5a
Date: Fri, 31 Aug 2018 08:26:31 +0000
fa3d5a
Subject: Various bugfixes.
fa3d5a
fa3d5a
* compat/dbmopen.c (ndbm_open_dir_file0): Ignore ENOENT.
fa3d5a
* src/falloc.c (push_avail_block): Free temporary storage no matter
fa3d5a
what return status.
fa3d5a
* src/gdbm.h.in (GDBM_FILE_TRUNCATE_ERROR): New error code.
fa3d5a
* src/gdbmdump.c (_gdbm_dump_ascii): Initialize rc.
fa3d5a
* src/gdbmerrno.c: Handle new error.code
fa3d5a
* src/gdbmload.c (gdbm_load_bdb_dump): Initialize rc
fa3d5a
* src/gdbmopen.c (_gdbm_ftruncate): New function.
fa3d5a
(gdbm_fd_open): Use _gdbm_ftruncate. Check its return.
fa3d5a
* src/gdbmseq.c (gdbm_firstkey): Initialize dsize
fa3d5a
* src/gdbmtool.c (command_generator): Check if cmd is NULL.
fa3d5a
(shouldn't happen, but anyways).
fa3d5a
* src/mmap.c (_gdbm_mapped_lseek): Check for vailidity of the 'whence'
fa3d5a
parameter.
fa3d5a
* src/systems.h (TRUNCATE): Remove macro.
fa3d5a
* src/util.c (vgetyn): Remove unnecessary assignment.
fa3d5a
---
fa3d5a
diff --git a/compat/dbmopen.c b/compat/dbmopen.c
fa3d5a
index b9e7518..0538992 100644
fa3d5a
--- a/compat/dbmopen.c
fa3d5a
+++ b/compat/dbmopen.c
fa3d5a
@@ -87,7 +87,7 @@ ndbm_open_dir_file0 (const char *file_name, int pagfd, int mode)
fa3d5a
 		  if ((mode & GDBM_OPENMASK) == GDBM_READER)
fa3d5a
 		    /* Ok, try to cope with it. */
fa3d5a
 		    return pagfd;
fa3d5a
-		  else
fa3d5a
+		  else if (errno != ENOENT)
fa3d5a
 		    {
fa3d5a
 		      gdbm_set_errno (NULL, GDBM_FILE_OPEN_ERROR, TRUE); 
fa3d5a
 		      return -1;
fa3d5a
diff --git a/src/falloc.c b/src/falloc.c
fa3d5a
index 09b40d4..7a94afb 100644
fa3d5a
--- a/src/falloc.c
fa3d5a
+++ b/src/falloc.c
fa3d5a
@@ -313,33 +313,43 @@ push_avail_block (GDBM_FILE dbf)
fa3d5a
   /* Update the header avail count to previous size divided by 2. */
fa3d5a
   dbf->header->avail.count >>= 1;
fa3d5a
 
fa3d5a
-  /* Free the unneeded space. */
fa3d5a
-  new_loc.av_adr += av_size;
fa3d5a
-  new_loc.av_size -= av_size;
fa3d5a
-  _gdbm_free (dbf, new_loc.av_adr, new_loc.av_size);
fa3d5a
-
fa3d5a
-  /* Update the disk. */
fa3d5a
-  file_pos = gdbm_file_seek (dbf, av_adr, SEEK_SET);
fa3d5a
-  if (file_pos != av_adr)
fa3d5a
+  rc = 0;
fa3d5a
+  do
fa3d5a
     {
fa3d5a
-      GDBM_SET_ERRNO (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
fa3d5a
-      _gdbm_fatal (dbf, _("lseek error"));
fa3d5a
-      return -1;
fa3d5a
-    }
fa3d5a
+      /* Free the unneeded space. */
fa3d5a
+      new_loc.av_adr += av_size;
fa3d5a
+      new_loc.av_size -= av_size;
fa3d5a
+      if (_gdbm_free (dbf, new_loc.av_adr, new_loc.av_size))
fa3d5a
+	{
fa3d5a
+	  rc = -1;
fa3d5a
+	  break;
fa3d5a
+	}
fa3d5a
+  
fa3d5a
+      /* Update the disk. */
fa3d5a
+      file_pos = gdbm_file_seek (dbf, av_adr, SEEK_SET);
fa3d5a
+      if (file_pos != av_adr)
fa3d5a
+	{
fa3d5a
+	  GDBM_SET_ERRNO (dbf, GDBM_FILE_SEEK_ERROR, TRUE);
fa3d5a
+	  _gdbm_fatal (dbf, _("lseek error"));
fa3d5a
+	  rc = -1;
fa3d5a
+	  break;
fa3d5a
+	}
fa3d5a
 
fa3d5a
-  rc = _gdbm_full_write (dbf, temp, av_size);
fa3d5a
-  if (rc)
fa3d5a
-    {
fa3d5a
-      GDBM_DEBUG (GDBM_DEBUG_STORE|GDBM_DEBUG_ERR,
fa3d5a
-		  "%s: error writing avail data: %s",
fa3d5a
-		  dbf->name, gdbm_db_strerror (dbf));	  
fa3d5a
-      _gdbm_fatal (dbf, gdbm_db_strerror (dbf));
fa3d5a
-      return -1;
fa3d5a
+      rc = _gdbm_full_write (dbf, temp, av_size);
fa3d5a
+      if (rc)
fa3d5a
+	{
fa3d5a
+	  GDBM_DEBUG (GDBM_DEBUG_STORE|GDBM_DEBUG_ERR,
fa3d5a
+		      "%s: error writing avail data: %s",
fa3d5a
+		      dbf->name, gdbm_db_strerror (dbf));	  
fa3d5a
+	  _gdbm_fatal (dbf, gdbm_db_strerror (dbf));
fa3d5a
+	  rc = -1;
fa3d5a
+	}
fa3d5a
     }
fa3d5a
-
fa3d5a
+  while (0);
fa3d5a
+  
fa3d5a
   free (temp);
fa3d5a
 
fa3d5a
-  return 0;
fa3d5a
+  return rc;
fa3d5a
 }
fa3d5a
 
fa3d5a
 /* AV_TABLE contains COUNT entries sorted by AV_SIZE in ascending order.
fa3d5a
diff --git a/src/gdbm.h.in b/src/gdbm.h.in
fa3d5a
index 6318ad8..f5eadc5 100644
fa3d5a
--- a/src/gdbm.h.in
fa3d5a
+++ b/src/gdbm.h.in
fa3d5a
@@ -227,9 +227,10 @@ extern int gdbm_copy_meta (GDBM_FILE dst, GDBM_FILE src);
fa3d5a
 # define GDBM_BAD_DIR_ENTRY             36
fa3d5a
 # define GDBM_FILE_CLOSE_ERROR          37  
fa3d5a
 # define GDBM_FILE_SYNC_ERROR           38
fa3d5a
+# define GDBM_FILE_TRUNCATE_ERROR       39
fa3d5a
   
fa3d5a
 # define _GDBM_MIN_ERRNO	0
fa3d5a
-# define _GDBM_MAX_ERRNO	GDBM_FILE_SYNC_ERROR
fa3d5a
+# define _GDBM_MAX_ERRNO	GDBM_FILE_TRUNCATE_ERROR
fa3d5a
 
fa3d5a
 /* This one was never used and will be removed in the future */
fa3d5a
 # define GDBM_UNKNOWN_UPDATE GDBM_UNKNOWN_ERROR
fa3d5a
diff --git a/src/gdbmdump.c b/src/gdbmdump.c
fa3d5a
index 2e6f5b0..a8c4ec5 100644
fa3d5a
--- a/src/gdbmdump.c
fa3d5a
+++ b/src/gdbmdump.c
fa3d5a
@@ -62,7 +62,7 @@ _gdbm_dump_ascii (GDBM_FILE dbf, FILE *fp)
fa3d5a
   size_t count = 0;
fa3d5a
   unsigned char *buffer = NULL;
fa3d5a
   size_t bufsize = 0;
fa3d5a
-  int rc;
fa3d5a
+  int rc = 0;
fa3d5a
 
fa3d5a
   fd = gdbm_fdesc (dbf);
fa3d5a
   if (fstat (fd, &st))
fa3d5a
diff --git a/src/gdbmerrno.c b/src/gdbmerrno.c
fa3d5a
index 4ce7f9d..6758272 100644
fa3d5a
--- a/src/gdbmerrno.c
fa3d5a
+++ b/src/gdbmerrno.c
fa3d5a
@@ -138,7 +138,8 @@ const char * const gdbm_errlist[_GDBM_MAX_ERRNO+1] = {
fa3d5a
   [GDBM_BAD_HASH_TABLE]         = N_("Malformed hash table"),
fa3d5a
   [GDBM_BAD_DIR_ENTRY]          = N_("Invalid directory entry"),
fa3d5a
   [GDBM_FILE_CLOSE_ERROR]       = N_("Error closing file"),
fa3d5a
-  [GDBM_FILE_SYNC_ERROR]        = N_("Error synchronizing file")
fa3d5a
+  [GDBM_FILE_SYNC_ERROR]        = N_("Error synchronizing file"),
fa3d5a
+  [GDBM_FILE_TRUNCATE_ERROR]    = N_("Error truncating file")
fa3d5a
 };
fa3d5a
 
fa3d5a
 const char *
fa3d5a
@@ -182,7 +183,8 @@ int const gdbm_syserr[_GDBM_MAX_ERRNO+1] = {
fa3d5a
   [GDBM_FILE_STAT_ERROR]        = 1,
fa3d5a
   [GDBM_BACKUP_FAILED]          = 1,
fa3d5a
   [GDBM_FILE_CLOSE_ERROR]       = 1,
fa3d5a
-  [GDBM_FILE_SYNC_ERROR]        = 1
fa3d5a
+  [GDBM_FILE_SYNC_ERROR]        = 1,
fa3d5a
+  [GDBM_FILE_TRUNCATE_ERROR]    = 1
fa3d5a
 };
fa3d5a
 
fa3d5a
 /* Returns true if system errno value is meaningful for GDBM error
fa3d5a
diff --git a/src/gdbmload.c b/src/gdbmload.c
fa3d5a
index 008bcb9..f5b7869 100644
fa3d5a
--- a/src/gdbmload.c
fa3d5a
+++ b/src/gdbmload.c
fa3d5a
@@ -542,6 +542,7 @@ gdbm_load_bdb_dump (struct dump_file *file, GDBM_FILE dbf, int replace)
fa3d5a
   memset (&xd, 0, sizeof (xd));
fa3d5a
   xs[0] = xs[1] = 0;
fa3d5a
   i = 0;
fa3d5a
+  rc = 0;
fa3d5a
   while ((c = fgetc (file->fp)) == ' ')
fa3d5a
     {
fa3d5a
       rc = xdatum_read (file->fp, &xd[i], &xs[i]);
fa3d5a
diff --git a/src/gdbmopen.c b/src/gdbmopen.c
fa3d5a
index 908887c..7ec57e7 100644
fa3d5a
--- a/src/gdbmopen.c
fa3d5a
+++ b/src/gdbmopen.c
fa3d5a
@@ -199,6 +199,21 @@ validate_header (gdbm_file_header const *hdr, struct stat const *st)
fa3d5a
   return 0;
fa3d5a
 }
fa3d5a
   
fa3d5a
+/* Do we have ftruncate? */
fa3d5a
+static inline int
fa3d5a
+_gdbm_ftruncate (GDBM_FILE dbf)
fa3d5a
+{
fa3d5a
+#if HAVE_FTRUNCATE
fa3d5a
+  return ftruncate (dbf->desc, 0);
fa3d5a
+#else
fa3d5a
+  int fd;
fa3d5a
+  fd = open (dbf->name, O_RDWR|O_TRUNC, mode);
fa3d5a
+  if (fd == -1)
fa3d5a
+    return -1;
fa3d5a
+  return close (fd);
fa3d5a
+#endif
fa3d5a
+}
fa3d5a
+
fa3d5a
 GDBM_FILE 
fa3d5a
 gdbm_fd_open (int fd, const char *file_name, int block_size,
fa3d5a
 	      int flags, void (*fatal_func) (const char *))
fa3d5a
@@ -320,14 +335,22 @@ gdbm_fd_open (int fd, const char *file_name, int block_size,
fa3d5a
      now time to truncate the file. */
fa3d5a
   if ((flags & GDBM_OPENMASK) == GDBM_NEWDB && file_stat.st_size != 0)
fa3d5a
     {
fa3d5a
-      TRUNCATE (dbf);
fa3d5a
-      if (fstat (dbf->desc, &file_stat))
fa3d5a
+      if (_gdbm_ftruncate (dbf))
fa3d5a
+	{
fa3d5a
+	  GDBM_SET_ERRNO2 (dbf, GDBM_FILE_TRUNCATE_ERROR, FALSE,
fa3d5a
+			   GDBM_DEBUG_OPEN);
fa3d5a
+	}
fa3d5a
+      else if (fstat (dbf->desc, &file_stat))
fa3d5a
+	{
fa3d5a
+	  GDBM_SET_ERRNO2 (dbf, GDBM_FILE_STAT_ERROR, FALSE, GDBM_DEBUG_OPEN);
fa3d5a
+	}
fa3d5a
+
fa3d5a
+      if (gdbm_last_errno (dbf))
fa3d5a
 	{
fa3d5a
 	  if (flags & GDBM_CLOERROR)
fa3d5a
 	    close (dbf->desc);
fa3d5a
 	  free (dbf->name);
fa3d5a
 	  free (dbf);
fa3d5a
-	  GDBM_SET_ERRNO2 (NULL, GDBM_FILE_STAT_ERROR, FALSE, GDBM_DEBUG_OPEN);
fa3d5a
 	  return NULL;
fa3d5a
 	}
fa3d5a
     }
fa3d5a
diff --git a/src/gdbmseq.c b/src/gdbmseq.c
fa3d5a
index e74d78d..ee7ebf3 100644
fa3d5a
--- a/src/gdbmseq.c
fa3d5a
+++ b/src/gdbmseq.c
fa3d5a
@@ -101,6 +101,7 @@ gdbm_firstkey (GDBM_FILE dbf)
fa3d5a
 
fa3d5a
   /* Set the default return value for not finding a first entry. */
fa3d5a
   return_val.dptr = NULL;
fa3d5a
+  return_val.dsize = 0;
fa3d5a
 
fa3d5a
   GDBM_DEBUG (GDBM_DEBUG_READ, "%s: getting first key", dbf->name);
fa3d5a
   
fa3d5a
diff --git a/src/gdbmtool.c b/src/gdbmtool.c
fa3d5a
index 454465e..8c97e1e 100644
fa3d5a
--- a/src/gdbmtool.c
fa3d5a
+++ b/src/gdbmtool.c
fa3d5a
@@ -1435,7 +1435,7 @@ command_generator (const char *text, int state)
fa3d5a
       len = strlen (text);
fa3d5a
     }
fa3d5a
 
fa3d5a
-  if (!cmd->name)
fa3d5a
+  if (!cmd || !cmd->name)
fa3d5a
     return NULL;
fa3d5a
 
fa3d5a
   /* Return the next name which partially matches from the command list. */
fa3d5a
diff --git a/src/mmap.c b/src/mmap.c
fa3d5a
index 48e84ae..148b852 100644
fa3d5a
--- a/src/mmap.c
fa3d5a
+++ b/src/mmap.c
fa3d5a
@@ -367,6 +367,10 @@ _gdbm_mapped_lseek (GDBM_FILE dbf, off_t offset, int whence)
fa3d5a
 	    needle = file_size - offset; 
fa3d5a
 	    break;
fa3d5a
 	  }
fa3d5a
+
fa3d5a
+	default:
fa3d5a
+	  errno = EINVAL;
fa3d5a
+	  return -1;
fa3d5a
 	}
fa3d5a
 
fa3d5a
       if (needle < 0)
fa3d5a
diff --git a/src/systems.h b/src/systems.h
fa3d5a
index 750aa51..f269060 100644
fa3d5a
--- a/src/systems.h
fa3d5a
+++ b/src/systems.h
fa3d5a
@@ -52,13 +52,6 @@
fa3d5a
 # define STATBLKSIZE(st) 1024
fa3d5a
 #endif
fa3d5a
 
fa3d5a
-/* Do we have ftruncate? */
fa3d5a
-#if HAVE_FTRUNCATE
fa3d5a
-# define TRUNCATE(dbf) ftruncate (dbf->desc, 0)
fa3d5a
-#else
fa3d5a
-# define TRUNCATE(dbf) close( open (dbf->name, O_RDWR|O_TRUNC, mode));
fa3d5a
-#endif
fa3d5a
-
fa3d5a
 #ifndef STDERR_FILENO
fa3d5a
 # define STDERR_FILENO 2
fa3d5a
 #endif
fa3d5a
diff --git a/src/util.c b/src/util.c
fa3d5a
index f254202..3493366 100644
fa3d5a
--- a/src/util.c
fa3d5a
+++ b/src/util.c
fa3d5a
@@ -98,8 +98,9 @@ vgetyn (const char *prompt, va_list ap)
fa3d5a
 		default:
fa3d5a
 		  fprintf (stdout, "%s\n", _("Please, reply 'y' or 'n'"));
fa3d5a
 		}
fa3d5a
-	      state = 0;
fa3d5a
-	    } else
fa3d5a
+	      /* fall through */
fa3d5a
+	    }
fa3d5a
+	  else
fa3d5a
 	    break;
fa3d5a
 	  
fa3d5a
 	case 0:
fa3d5a
--
fa3d5a
cgit v0.9.0.3