convert strncpy to memcpy
authorJeff King <peff@peff.net>
Thu, 24 Sep 2015 21:08:26 +0000 (17:08 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 5 Oct 2015 18:08:06 +0000 (11:08 -0700)
strncpy is known to be a confusing function because of its
termination semantics. These calls are all correct, but it
takes some examination to see why. In particular, every one
of them expects to copy up to the length limit, and then
makes some arrangement for terminating the result.

We can just use memcpy, along with noting explicitly how the
result is terminated (if it is not already obvious). That
should make it more clear to a reader that we are doing the
right thing.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/help.c
fast-import.c
tag.c
index e1650ab3f9a1b94e4df3542a8b1a9c7a43e2dd03..1cd0c1ee44daf056befb8a26bba2f4fd343c8658 100644 (file)
@@ -176,7 +176,7 @@ static void add_man_viewer(const char *name)
        while (*p)
                p = &((*p)->next);
        *p = xcalloc(1, (sizeof(**p) + len + 1));
-       strncpy((*p)->name, name, len);
+       memcpy((*p)->name, name, len); /* NUL-terminated by xcalloc */
 }
 
 static int supported_man_viewer(const char *name, size_t len)
@@ -192,7 +192,7 @@ static void do_add_man_viewer_info(const char *name,
 {
        struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1);
 
-       strncpy(new->name, name, len);
+       memcpy(new->name, name, len); /* NUL-terminated by xcalloc */
        new->info = xstrdup(value);
        new->next = man_viewer_info_list;
        man_viewer_info_list = new;
index cf6d8bc0ce430b5c6a05fd26e2abd9d473e8c832..4d01efcb9d1e41062d76e88a4f928ceb50f79ca8 100644 (file)
@@ -703,7 +703,7 @@ static struct atom_str *to_atom(const char *s, unsigned short len)
 
        c = pool_alloc(sizeof(struct atom_str) + len + 1);
        c->str_len = len;
-       strncpy(c->str_dat, s, len);
+       memcpy(c->str_dat, s, len);
        c->str_dat[len] = 0;
        c->next_atom = atom_table[hc];
        atom_table[hc] = c;
diff --git a/tag.c b/tag.c
index 5b0ac62ed846188ad44ceac41d278ff37ec8bd9d..5b2a06d92b75c7aea3f8cfb1cadb33d9da27f3c6 100644 (file)
--- a/tag.c
+++ b/tag.c
@@ -82,7 +82,7 @@ int parse_tag_buffer(struct tag *item, const void *data, unsigned long size)
        nl = memchr(bufptr, '\n', tail - bufptr);
        if (!nl || sizeof(type) <= (nl - bufptr))
                return -1;
-       strncpy(type, bufptr, nl - bufptr);
+       memcpy(type, bufptr, nl - bufptr);
        type[nl - bufptr] = '\0';
        bufptr = nl + 1;