diff options
| author | Tom Lane <tgl@sss.pgh.pa.us> | 2006-09-27 18:40:10 +0000 |
|---|---|---|
| committer | Tom Lane <tgl@sss.pgh.pa.us> | 2006-09-27 18:40:10 +0000 |
| commit | c92f7e258ee579abd0f95183598edf250d351b2c (patch) | |
| tree | 10c6b377a74c61b71ece70cfd3cb209795bf1051 /src/backend/utils/hash/dynahash.c | |
| parent | 996b203e621bc76985ff0156b4f2ef720944b41b (diff) | |
| download | postgresql-c92f7e258ee579abd0f95183598edf250d351b2c.tar.gz | |
Replace strncpy with strlcpy in selected places that seem possibly relevant
to performance. (A wholesale effort to get rid of strncpy should be
undertaken sometime, but not during beta.) This commit also fixes dynahash.c
to correctly truncate overlength string keys for hashtables, so that its
callers don't have to anymore.
Diffstat (limited to 'src/backend/utils/hash/dynahash.c')
| -rw-r--r-- | src/backend/utils/hash/dynahash.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c index b2974da999..aa0a6b11f2 100644 --- a/src/backend/utils/hash/dynahash.c +++ b/src/backend/utils/hash/dynahash.c @@ -26,7 +26,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/hash/dynahash.c,v 1.71 2006/08/14 12:39:55 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/hash/dynahash.c,v 1.72 2006/09/27 18:40:09 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -209,6 +209,20 @@ DynaHashAlloc(Size size) } +/* + * HashCompareFunc for string keys + * + * Because we copy keys with strlcpy(), they will be truncated at keysize-1 + * bytes, so we can only compare that many ... hence strncmp is almost but + * not quite the right thing. + */ +static int +string_compare(const char *key1, const char *key2, Size keysize) +{ + return strncmp(key1, key2, keysize - 1); +} + + /************************** CREATE ROUTINES **********************/ /* @@ -273,24 +287,24 @@ hash_create(const char *tabname, long nelem, HASHCTL *info, int flags) hashp->hash = string_hash; /* default hash function */ /* - * If you don't specify a match function, it defaults to strncmp() if you - * used string_hash (either explicitly or by default) and to memcmp() - * otherwise. (Prior to PostgreSQL 7.4, memcmp() was always used.) + * If you don't specify a match function, it defaults to string_compare + * if you used string_hash (either explicitly or by default) and to memcmp + * otherwise. (Prior to PostgreSQL 7.4, memcmp was always used.) */ if (flags & HASH_COMPARE) hashp->match = info->match; else if (hashp->hash == string_hash) - hashp->match = (HashCompareFunc) strncmp; + hashp->match = (HashCompareFunc) string_compare; else hashp->match = memcmp; /* - * Similarly, the key-copying function defaults to strncpy() or memcpy(). + * Similarly, the key-copying function defaults to strlcpy or memcpy. */ if (flags & HASH_KEYCOPY) hashp->keycopy = info->keycopy; else if (hashp->hash == string_hash) - hashp->keycopy = (HashCopyFunc) strncpy; + hashp->keycopy = (HashCopyFunc) strlcpy; else hashp->keycopy = memcpy; |
