diff options
Diffstat (limited to 'src/backend/utils')
-rw-r--r-- | src/backend/utils/error/elog.c | 18 | ||||
-rw-r--r-- | src/backend/utils/fmgr/dfmgr.c | 8 | ||||
-rw-r--r-- | src/backend/utils/hash/dynahash.c | 28 | ||||
-rw-r--r-- | src/backend/utils/hash/hashfn.c | 12 | ||||
-rw-r--r-- | src/backend/utils/misc/ps_status.c | 4 | ||||
-rw-r--r-- | src/backend/utils/mmgr/portalmem.c | 20 |
6 files changed, 53 insertions, 37 deletions
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c index e46d16fa55d..2aff7f790aa 100644 --- a/src/backend/utils/error/elog.c +++ b/src/backend/utils/error/elog.c @@ -42,7 +42,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.173 2006/07/14 14:52:25 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/error/elog.c,v 1.174 2006/09/27 18:40:09 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -1225,6 +1225,7 @@ write_syslog(int level, const char *line) while (len > 0) { char buf[PG_SYSLOG_LIMIT + 1]; + const char *nlpos; int buflen; int i; @@ -1236,12 +1237,15 @@ write_syslog(int level, const char *line) continue; } - strncpy(buf, line, PG_SYSLOG_LIMIT); - buf[PG_SYSLOG_LIMIT] = '\0'; - if (strchr(buf, '\n') != NULL) - *strchr(buf, '\n') = '\0'; - - buflen = strlen(buf); + /* copy one line, or as much as will fit, to buf */ + nlpos = strchr(line, '\n'); + if (nlpos != NULL) + buflen = nlpos - line; + else + buflen = len; + buflen = Min(buflen, PG_SYSLOG_LIMIT); + memcpy(buf, line, buflen); + buf[buflen] = '\0'; /* trim to multibyte letter boundary */ buflen = pg_mbcliplen(buf, buflen, buflen); diff --git a/src/backend/utils/fmgr/dfmgr.c b/src/backend/utils/fmgr/dfmgr.c index b95180d17ef..b12674026e2 100644 --- a/src/backend/utils/fmgr/dfmgr.c +++ b/src/backend/utils/fmgr/dfmgr.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.89 2006/08/16 04:32:48 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/fmgr/dfmgr.c,v 1.90 2006/09/27 18:40:09 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -594,7 +594,6 @@ find_rendezvous_variable(const char *varName) { static HTAB *rendezvousHash = NULL; - char key[NAMEDATALEN]; rendezvousHashEntry *hentry; bool found; @@ -612,12 +611,9 @@ find_rendezvous_variable(const char *varName) HASH_ELEM); } - /* Turn the varName into a fixed-size string */ - StrNCpy(key, varName, sizeof(key)); - /* Find or create the hashtable entry for this varName */ hentry = (rendezvousHashEntry *) hash_search(rendezvousHash, - key, + varName, HASH_ENTER, &found); diff --git a/src/backend/utils/hash/dynahash.c b/src/backend/utils/hash/dynahash.c index b2974da999a..aa0a6b11f2a 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; diff --git a/src/backend/utils/hash/hashfn.c b/src/backend/utils/hash/hashfn.c index ed0826e8e51..af528881eff 100644 --- a/src/backend/utils/hash/hashfn.c +++ b/src/backend/utils/hash/hashfn.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/hash/hashfn.c,v 1.27 2006/07/14 14:52:25 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/hash/hashfn.c,v 1.28 2006/09/27 18:40:09 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -27,8 +27,16 @@ uint32 string_hash(const void *key, Size keysize) { + /* + * If the string exceeds keysize-1 bytes, we want to hash only that many, + * because when it is copied into the hash table it will be truncated at + * that length. + */ + Size s_len = strlen((const char *) key); + + s_len = Min(s_len, keysize-1); return DatumGetUInt32(hash_any((const unsigned char *) key, - (int) strlen((const char *) key))); + (int) s_len)); } /* diff --git a/src/backend/utils/misc/ps_status.c b/src/backend/utils/misc/ps_status.c index d23aa563f79..f6c4c588b20 100644 --- a/src/backend/utils/misc/ps_status.c +++ b/src/backend/utils/misc/ps_status.c @@ -5,7 +5,7 @@ * to contain some useful information. Mechanism differs wildly across * platforms. * - * $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.31 2006/06/27 22:16:44 momjian Exp $ + * $PostgreSQL: pgsql/src/backend/utils/misc/ps_status.c,v 1.32 2006/09/27 18:40:10 tgl Exp $ * * Copyright (c) 2000-2006, PostgreSQL Global Development Group * various details abducted from various places @@ -300,7 +300,7 @@ set_ps_display(const char *activity, bool force) #endif /* Update ps_buffer to contain both fixed part and activity */ - StrNCpy(ps_buffer + ps_buffer_fixed_size, activity, + strlcpy(ps_buffer + ps_buffer_fixed_size, activity, ps_buffer_size - ps_buffer_fixed_size); /* Transmit new setting to kernel, if necessary */ diff --git a/src/backend/utils/mmgr/portalmem.c b/src/backend/utils/mmgr/portalmem.c index e29ffb53c98..883f075eec2 100644 --- a/src/backend/utils/mmgr/portalmem.c +++ b/src/backend/utils/mmgr/portalmem.c @@ -12,7 +12,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.94 2006/09/07 22:52:01 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.95 2006/09/27 18:40:10 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -54,12 +54,10 @@ static HTAB *PortalHashTable = NULL; #define PortalHashTableLookup(NAME, PORTAL) \ do { \ - PortalHashEnt *hentry; char key[MAX_PORTALNAME_LEN]; \ + PortalHashEnt *hentry; \ \ - MemSet(key, 0, MAX_PORTALNAME_LEN); \ - StrNCpy(key, NAME, MAX_PORTALNAME_LEN); \ hentry = (PortalHashEnt *) hash_search(PortalHashTable, \ - key, HASH_FIND, NULL); \ + (NAME), HASH_FIND, NULL); \ if (hentry) \ PORTAL = hentry->portal; \ else \ @@ -68,12 +66,10 @@ do { \ #define PortalHashTableInsert(PORTAL, NAME) \ do { \ - PortalHashEnt *hentry; bool found; char key[MAX_PORTALNAME_LEN]; \ + PortalHashEnt *hentry; bool found; \ \ - MemSet(key, 0, MAX_PORTALNAME_LEN); \ - StrNCpy(key, NAME, MAX_PORTALNAME_LEN); \ hentry = (PortalHashEnt *) hash_search(PortalHashTable, \ - key, HASH_ENTER, &found); \ + (NAME), HASH_ENTER, &found); \ if (found) \ elog(ERROR, "duplicate portal name"); \ hentry->portal = PORTAL; \ @@ -83,12 +79,10 @@ do { \ #define PortalHashTableDelete(PORTAL) \ do { \ - PortalHashEnt *hentry; char key[MAX_PORTALNAME_LEN]; \ + PortalHashEnt *hentry; \ \ - MemSet(key, 0, MAX_PORTALNAME_LEN); \ - StrNCpy(key, PORTAL->name, MAX_PORTALNAME_LEN); \ hentry = (PortalHashEnt *) hash_search(PortalHashTable, \ - key, HASH_REMOVE, NULL); \ + PORTAL->name, HASH_REMOVE, NULL); \ if (hentry == NULL) \ elog(WARNING, "trying to delete portal name that does not exist"); \ } while(0) |