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