kore

Kore is a web application platform for writing scalable, concurrent web based processes in C or Python.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

commit 27acc51241edc357363d384ebfd39f256e204bd7
parent 589c7667bec30b9af1a1b7ae9f4e6792709c8f72
Author: Joris Vink <joris@coders.se>
Date:   Mon,  4 Jul 2016 11:41:37 +0200

Improve kore_strlcpy().

Make it return the original length of the input string so the caller
can check for truncation. Also guard against len being 0 as this would
not do anything with the destination string (not even NUL terminate it).

Diffstat:
includes/kore.h | 2+-
src/accesslog.c | 19+++++++++++++------
src/mem.c | 2+-
src/utils.c | 16++++++++++++----
4 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/includes/kore.h b/includes/kore.h @@ -522,7 +522,7 @@ char *kore_time_to_date(time_t); char *kore_strdup(const char *); void kore_log(int, const char *, ...); u_int64_t kore_strtonum64(const char *, int, int *); -void kore_strlcpy(char *, const char *, size_t); +size_t kore_strlcpy(char *, const char *, const size_t); void kore_server_disconnect(struct connection *); int kore_split_string(char *, char *, char **, size_t); void kore_strip_chars(char *, const char, char **); diff --git a/src/accesslog.c b/src/accesslog.c @@ -101,7 +101,7 @@ kore_accesslog_write(const void *data, u_int32_t len) if (inet_ntop(logpacket.addrtype, &(logpacket.addr), addr, sizeof(addr)) == NULL) - kore_strlcpy(addr, "unknown", sizeof(addr)); + (void)kore_strlcpy(addr, "unknown", sizeof(addr)); time(&now); tbuf = kore_time_to_date(now); @@ -150,14 +150,21 @@ kore_accesslog(struct http_request *req) logpacket.worker_id = worker->id; logpacket.worker_cpu = worker->cpu; logpacket.time_req = req->total; - kore_strlcpy(logpacket.host, req->host, sizeof(logpacket.host)); - kore_strlcpy(logpacket.path, req->path, sizeof(logpacket.path)); + + if (kore_strlcpy(logpacket.host, + req->host, sizeof(logpacket.host)) >= sizeof(logpacket.host)) + kore_log(LOG_NOTICE, "kore_accesslog: host truncated"); + + if (kore_strlcpy(logpacket.path, + req->path, sizeof(logpacket.path)) >= sizeof(logpacket.path)) + kore_log(LOG_NOTICE, "kore_accesslog: path truncated"); if (req->agent != NULL) { - kore_strlcpy(logpacket.agent, - req->agent, sizeof(logpacket.agent)); + if (kore_strlcpy(logpacket.agent, req->agent, + sizeof(logpacket.agent)) >= sizeof(logpacket.agent)) + kore_log(LOG_NOTICE, "kore_accesslog: agent truncated"); } else { - kore_strlcpy(logpacket.agent, "unknown", + (void)kore_strlcpy(logpacket.agent, "unknown", sizeof(logpacket.agent)); } diff --git a/src/mem.c b/src/mem.c @@ -122,7 +122,7 @@ kore_strdup(const char *str) len = strlen(str) + 1; nstr = kore_malloc(len); - kore_strlcpy(nstr, str, len); + (void)kore_strlcpy(nstr, str, len); return (nstr); } diff --git a/src/utils.c b/src/utils.c @@ -81,7 +81,7 @@ kore_log(int prio, const char *fmt, ...) (void)snprintf(tmp, sizeof(tmp), "wrk %d", worker->id); #if !defined(KORE_NO_TLS) if (worker->id == KORE_WORKER_KEYMGR) - kore_strlcpy(tmp, "keymgr", sizeof(tmp)); + (void)kore_strlcpy(tmp, "keymgr", sizeof(tmp)); #endif if (foreground) printf("[%s]: %s\n", tmp, buf); @@ -95,13 +95,16 @@ kore_log(int prio, const char *fmt, ...) } } -void -kore_strlcpy(char *dst, const char *src, size_t len) +size_t +kore_strlcpy(char *dst, const char *src, const size_t len) { char *d = dst; const char *s = src; const char *end = dst + len - 1; + if (len == 0) + fatal("kore_strlcpy: len == 0"); + while ((*d = *s) != '\0') { if (d == end) { *d = '\0'; @@ -111,6 +114,11 @@ kore_strlcpy(char *dst, const char *src, size_t len) d++; s++; } + + while (*s != '\0') + s++; + + return (s - src); } int @@ -416,7 +424,7 @@ kore_base64_encode(u_int8_t *data, u_int32_t len, char **out) pdata = kore_buf_release(res, &plen); *out = kore_malloc(plen + 1); - kore_strlcpy(*out, (char *)pdata, plen + 1); + (void)kore_strlcpy(*out, (char *)pdata, plen + 1); kore_mem_free(pdata); return (KORE_RESULT_OK);