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

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