hash table allocation failure

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.m4.patches
Message-ID <[email protected]>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks to Jim's recent work on the gnulib hash module, I audited the code
for cases where allocation failure could cause assertion failures instead
of expected xalloc_die behavior (either way, the program ends, but I want
it to be obvious from the stderr output that the user was short on memory,
and not that they triggered an internal error in m4).  I have a pending
patch that aims to clean up a memory leak in gnulib's hash_rehash
(admittedly a corner case, since there's not much you can do to recover
from an allocation failure on a rehash); but until that is reviewed and
applied, I'm using a gnulib local override to avoid a gcc warning.  Only
branch-1.6 is affected.

- --
Don't work too hard, make some time for fun as well!

Eric Blake             [email protected]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAko3EKgACgkQ84KuGfSFAYDvswCdEaAIsTgH5+wpq+GLfeJMhJVr
4AAAn0L/JxW1cOX/5/jGZAC6qmmN7nRS
=yIHW
-----END PGP SIGNATURE-----

_______________________________________________
M4-patches mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/m4-patches
m4.patch419 (text/plain, 3.8 KB)
From 5165ce08a8784648f4fa0fafe2d3005569f16bdd Mon Sep 17 00:00:00 2001
From: Eric Blake <[email protected]>
Date: Mon, 15 Jun 2009 09:17:41 -0600
Subject: [PATCH] Properly manage hash return values.

* src/symtab.c (symtab_init, lookup_symbol): React to allocation
failure.
* local/lib/hash.c.diff: New file to silence gcc warning, until
such time as upstream gnulib hash module is patched to avoid
memory leak.
* .gitattributes: Ignore spacing in diff.

Signed-off-by: Eric Blake <[email protected]>
---
 .gitattributes        |    3 +++
 ChangeLog             |   10 ++++++++++
 local/lib/hash.c.diff |   27 +++++++++++++++++++++++++++
 src/symtab.c          |   17 ++++++++++++++---
 4 files changed, 54 insertions(+), 3 deletions(-)
 create mode 100644 local/lib/hash.c.diff

diff --git a/.gitattributes b/.gitattributes
index 68d2d12..1b29560 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -2,3 +2,6 @@
 # needed for using these attributes effectively.
 ChangeLog merge=merge-changelog
 *.texi* diff=texinfo
+# Ignore whitespace in any gnulib local patches.
+local/* -whitespace
+local/*/* -whitespace
diff --git a/ChangeLog b/ChangeLog
index 3b3ca90..c9169f4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2009-06-15  Eric Blake  <[email protected]>
+
+	Properly manage hash return values.
+	* src/symtab.c (symtab_init, lookup_symbol): React to allocation
+	failure.
+	* local/lib/hash.c.diff: New file to silence gcc warning, until
+	such time as upstream gnulib hash module is patched to avoid
+	memory leak.
+	* .gitattributes: Ignore spacing in diff.
+
 2009-06-13  Eric Blake  <[email protected]>

 	Fix testsuite failure.
diff --git a/local/lib/hash.c.diff b/local/lib/hash.c.diff
new file mode 100644
index 0000000..8d27a36
--- /dev/null
+++ b/local/lib/hash.c.diff
@@ -0,0 +1,27 @@
+diff --git a/lib/hash.c b/lib/hash.c
+index 7d76d45..6f3a5c6 100644
+--- a/lib/hash.c
++++ b/lib/hash.c
+@@ -1,7 +1,7 @@
+ /* hash - hashing table processing.
+
+-   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007 Free
+-   Software Foundation, Inc.
++   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007,
++   2009 Free Software Foundation, Inc.
+
+    Written by Jim Meyering, 1992.
+
+@@ -1012,7 +1012,11 @@ hash_delete (Hash_table *table, const void *entry)
+ 		 : (table->n_buckets * tuning->shrink_factor
+ 		    * tuning->growth_threshold));
+
+-	      hash_rehash (table, candidate);
++	      if (hash_rehash (table, candidate))
++		{
++		  /* Failure to allocate memory in an attempt to
++		     shrink the table is not fatal.  */
++		}
+ 	    }
+ 	}
+     }
diff --git a/src/symtab.c b/src/symtab.c
index 6631e9c..338bf17 100644
--- a/src/symtab.c
+++ b/src/symtab.c
@@ -177,6 +177,8 @@ symtab_init (size_t size)
 {
   symtab = hash_initialize (size, NULL, symtab_hasher, symtab_comparator,
 			    symtab_free_entry);
+  if (!symtab)
+    xalloc_die ();

 #ifdef DEBUG_SYM
   atexit (show_profile); /* Ignore failure, since this is debug code.  */
@@ -277,7 +279,10 @@ lookup_symbol (const char *name, size_t len, symbol_lookup mode)
 		  assert (entry == old);
 		  sym->stack = sym;
 		  entry = (symbol *) hash_insert (symtab, sym);
-		  assert (sym == entry);
+		  if (entry)
+		    assert (sym == entry);
+		  else
+		    xalloc_die ();
 		}
 	      else
 		{
@@ -319,7 +324,10 @@ lookup_symbol (const char *name, size_t len, symbol_lookup mode)
 	{
 	  sym->stack = sym;
 	  entry = (symbol *) hash_insert (symtab, sym);
-	  assert (sym == entry);
+	  if (entry)
+	    assert (sym == entry);
+	  else
+	    xalloc_die ();
 	}
       return sym;

@@ -376,7 +384,10 @@ lookup_symbol (const char *name, size_t len, symbol_lookup mode)

 	    sym->stack = sym;
 	    entry = (symbol *) hash_insert (symtab, sym);
-	    assert (sym == entry);
+	    if (entry)
+	      assert (sym == entry);
+	    else
+	      xalloc_die ();
 	  }
 	return result;
       }
-- 
1.6.3.rc3.2.g4b51
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.