From 64a0c2d2552f7459c7e775b2177585c2b55f6899 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Thu, 24 Jan 2008 09:24:30 +0000 Subject: [PATCH] log string truncation by strlcpy --- include/util.h | 7 +++++++ src/admin.c | 6 +++--- src/loader.c | 2 +- src/objects.c | 10 +++++----- src/slab.c | 2 +- src/varcache.c | 2 +- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/util.h b/include/util.h index d7ff956..1f4540f 100644 --- a/include/util.h +++ b/include/util.h @@ -111,3 +111,10 @@ void fill_local_addr(PgSocket *sk, int fd, bool is_unix); void rescue_timers(void); void safe_evtimer_add(struct event *ev, struct timeval *tv); +/* log truncated strings */ +#define safe_strcpy(dst, src, dstlen) do { \ + size_t needed = strlcpy(dst, src, dstlen); \ + if (unlikely(needed >= (dstlen))) \ + log_warning("bug in %s:%d - string truncated", __FILE__, __LINE__); \ +} while (0) + diff --git a/src/admin.c b/src/admin.c index d9431ad..ce249ce 100644 --- a/src/admin.c +++ b/src/admin.c @@ -489,13 +489,13 @@ static void socket_header(PktBuf *buf, bool debug) "pkt_avail", "send_avail"); } -static void adr2txt(const PgAddr *adr, char *dst, int dstlen) +static void adr2txt(const PgAddr *adr, char *dst, unsigned dstlen) { if (adr->is_unix) { - strlcpy(dst, "unix", dstlen); + safe_strcpy(dst, "unix", dstlen); } else { char *tmp = inet_ntoa(adr->ip_addr); - strlcpy(dst, tmp, dstlen); + safe_strcpy(dst, tmp, dstlen); } } diff --git a/src/loader.c b/src/loader.c index 5ff6acf..6092bc0 100644 --- a/src/loader.c +++ b/src/loader.c @@ -268,7 +268,7 @@ void parse_database(char *name, char *connstr) db->addr.port = v_port; db->addr.ip_addr.s_addr = v_addr; db->addr.is_unix = host ? 0 : 1; - strlcpy(db->unix_socket_dir, unix_dir, sizeof(db->unix_socket_dir)); + safe_strcpy(db->unix_socket_dir, unix_dir, sizeof(db->unix_socket_dir)); if (host) log_debug("%s: host=%s/%s", name, host, inet_ntoa(db->addr.ip_addr)); diff --git a/src/objects.c b/src/objects.c index 853abfe..89c5b1c 100644 --- a/src/objects.c +++ b/src/objects.c @@ -297,7 +297,7 @@ PgDatabase *add_database(const char *name) return NULL; list_init(&db->head); - strlcpy(db->name, name, sizeof(db->name)); + safe_strcpy(db->name, name, sizeof(db->name)); put_in_order(&db->head, &database_list, cmp_database); } @@ -316,12 +316,12 @@ PgUser *add_user(const char *name, const char *passwd) list_init(&user->head); list_init(&user->pool_list); - strlcpy(user->name, name, sizeof(user->name)); + safe_strcpy(user->name, name, sizeof(user->name)); put_in_order(&user->head, &user_list, cmp_user); tree_insert(&user_tree, (long)user->name, &user->tree_node); } - strlcpy(user->passwd, passwd, sizeof(user->passwd)); + safe_strcpy(user->passwd, passwd, sizeof(user->passwd)); return user; } @@ -336,8 +336,8 @@ PgUser *force_user(PgDatabase *db, const char *name, const char *passwd) list_init(&user->head); list_init(&user->pool_list); } - strlcpy(user->name, name, sizeof(user->name)); - strlcpy(user->passwd, passwd, sizeof(user->passwd)); + safe_strcpy(user->name, name, sizeof(user->name)); + safe_strcpy(user->passwd, passwd, sizeof(user->passwd)); db->forced_user = user; return user; } diff --git a/src/slab.c b/src/slab.c index 27d4d8d..d54f88c 100644 --- a/src/slab.c +++ b/src/slab.c @@ -69,7 +69,7 @@ static void init_objcache(ObjectCache *cache, list_init(&cache->head); statlist_init(&cache->freelist, name); statlist_init(&cache->slablist, name); - strlcpy(cache->name, name, sizeof(cache->name)); + safe_strcpy(cache->name, name, sizeof(cache->name)); cache->total_count = 0; cache->init_func = init_func; statlist_append(&cache->head, &objcache_list); diff --git a/src/varcache.c b/src/varcache.c index 1dde16c..1f112ea 100644 --- a/src/varcache.c +++ b/src/varcache.c @@ -169,7 +169,7 @@ void varcache_fill_unset(VarCache *src, PgSocket *dst) srcval = get_value(src, lk); dstval = get_value(&dst->vars, lk); if (!*dstval) - strlcpy(dstval, srcval, lk->len); + memcpy(dstval, srcval, lk->len); } } -- 2.40.0