commit ee49dc81c75806fa11ccec409a3ff34f6963b18b
Author: Jim Meyering <meyering(a)redhat.com>
Date: Mon Aug 6 18:59:50 2012 +0200
maint: avoid use of strncpy; use stzncpy instead
* rest.c (stzncpy): Define.
(proxy_bucket_post): Use stzncpy in place of deprecated strncpy.
rest.c | 17 ++++++++++++++++-
1 files changed, 16 insertions(+), 1 deletions(-)
---
diff --git a/rest.c b/rest.c
index 832c486..98e4d92 100644
--- a/rest.c
+++ b/rest.c
@@ -113,6 +113,21 @@ typedef struct {
MHD_AccessHandlerCallback handler;
} rule;
+/* Like stpncpy, but do ensure that the result is NUL-terminated,
+ and do not NUL-pad out to LEN. I.e., when strnlen (src, len) == len,
+ this function writes a NUL byte into dest[len]. Thus, the length
+ of the destination buffer must be at least LEN + 1.
+ The DEST and SRC buffers must not overlap. */
+static inline char *
+stzncpy (char *restrict dest, char const *restrict src, size_t len)
+{
+ char const *src_end = src + len;
+ while (src < src_end && *src)
+ *dest++ = *src++;
+ *dest = 0;
+ return dest;
+}
+
static void simple_closer (void *ctx);
static unsigned short my_port = MY_PORT;
@@ -1423,7 +1438,7 @@ proxy_bucket_post (void *cctx, struct MHD_Connection *conn, const char *url,
int rc = MHD_HTTP_BAD_REQUEST;
key = kv_hash_lookup(ms->dict,"_key");
if (key) {
- strncpy(ms->key,key,MAX_FIELD_LEN-1);
+ stzncpy(ms->key,key,MAX_FIELD_LEN-1);
kv_hash_delete(ms->dict,"_key");
if (!kv_find_val(ms->dict,post_find,NULL)) {
hash_do_for_each (ms->dict,post_foreach,ms);