diff --git a/argp/ChangeLog b/argp/ChangeLog index 8794e28ea53dc307762a0164331091ab76127363..27b9228928cd39135dde33e89ca601c5b0d12959 100644 --- a/argp/ChangeLog +++ b/argp/ChangeLog @@ -1,3 +1,8 @@ +2014-10-02 Niels Möller <nisse@lysator.liu.se> + + * strndup.c (strndup): Fixed off-by-one error, and failure check. + Spotted by Avner BenHanoch. + 2008-08-26 Niels Möller <nisse@lysator.liu.se> * testsuite/Makefile.in (tags): Put TAGS file in the source diff --git a/argp/strndup.c b/argp/strndup.c index 085b7095e71c00e2539a9b5c5122476c42bc6eae..b1c69b07383eb1a71c03775858cb0ed527ff4bc6 100644 --- a/argp/strndup.c +++ b/argp/strndup.c @@ -17,15 +17,15 @@ strndup (const char *s, size_t size) char *end = memchr(s, 0, size); if (end) - /* Length + 1 */ - size = end - s + 1; + /* strlen, i.e., excluding the terminating NUL. */ + size = end - s; - r = malloc(size); + r = malloc(size+1); - if (size) + if (r) { - memcpy(r, s, size-1); - r[size-1] = '\0'; + memcpy(r, s, size); + r[size] = '\0'; } return r; }