]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
When using C-string lookup keys in a dynahash.c hash table, use strncpy()
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 18 Jun 2005 20:51:44 +0000 (20:51 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 18 Jun 2005 20:51:44 +0000 (20:51 +0000)
not memcpy() to copy the offered key into the hash table during HASH_ENTER.
This avoids possible core dump if the passed key is located very near the
end of memory.  Per report from Stefan Kaltenbrunner.

src/backend/utils/hash/dynahash.c
src/include/utils/hsearch.h

index 06e848a47d381e53dd482486cbe1beac2e15153d..1a54005d2c8da8cf70070400a43c7907a253355d 100644 (file)
@@ -9,7 +9,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/hash/dynahash.c,v 1.58 2004/12/31 22:01:37 pgsql Exp $
+ *       $PostgreSQL: pgsql/src/backend/utils/hash/dynahash.c,v 1.58.4.1 2005/06/18 20:51:44 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -143,6 +143,14 @@ hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
        else
                hashp->match = memcmp;
 
+       /*
+        * Similarly, the key-copying function defaults to strncpy() or memcpy().
+        */
+       if (hashp->hash == string_hash)
+               hashp->keycopy = (HashCopyFunc) strncpy;
+       else
+               hashp->keycopy = memcpy;
+
        if (flags & HASH_SHARED_MEM)
        {
                /*
@@ -657,7 +665,7 @@ hash_search(HTAB *hashp,
 
                        /* copy key into record */
                        currBucket->hashvalue = hashvalue;
-                       memcpy(ELEMENTKEY(currBucket), keyPtr, hctl->keysize);
+                       hashp->keycopy(ELEMENTKEY(currBucket), keyPtr, hctl->keysize);
 
                        /* caller is expected to fill the data field on return */
 
index 9773dc733f3be4c4ed3a6ae621a35f09d25b7d3b..5a012309ea183bf50091896398c5cb8d428f55ae 100644 (file)
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/hsearch.h,v 1.34 2004/12/31 22:03:46 pgsql Exp $
+ * $PostgreSQL: pgsql/src/include/utils/hsearch.h,v 1.34.4.1 2005/06/18 20:51:44 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 
 
 /*
- * Hash and comparison functions must have these signatures.  Comparison
- * functions return zero for match, nonzero for no match.  (The comparison
- * function definition is designed to allow memcmp() and strncmp() to be
- * used directly as key comparison functions.)
+ * Hash functions must have this signature.
  */
 typedef uint32 (*HashValueFunc) (const void *key, Size keysize);
+
+/*
+ * Key comparison functions must have this signature.  Comparison functions
+ * return zero for match, nonzero for no match.  (The comparison function
+ * definition is designed to allow memcmp() and strncmp() to be used directly
+ * as key comparison functions.)
+ */
 typedef int (*HashCompareFunc) (const void *key1, const void *key2,
-                                                                                       Size keysize);
+                                                               Size keysize);
+
+/*
+ * Key copying functions must have this signature.  The return value is not
+ * used.  (The definition is set up to allow memcpy() and strncpy() to be
+ * used directly.)
+ */
+typedef void *(*HashCopyFunc) (void *dest, const void *src, Size keysize);
 
 /*
  * Space allocation function for a hashtable --- designed to match malloc().
@@ -108,6 +119,7 @@ typedef struct HTAB
                                                                 * used */
        char       *tabname;            /* table name (for error messages) */
        bool            isshared;               /* true if table is in shared memory */
+       HashCopyFunc keycopy;           /* key copying function */
 } HTAB;
 
 /* Parameter data structure for hash_create */