Commit 8a0cc32f authored by Niels Möller's avatar Niels Möller

Fixed bugs in replacement strndup, spotted by Avner BenHanoch.

parent 59f8d39f
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> 2008-08-26 Niels Möller <nisse@lysator.liu.se>
* testsuite/Makefile.in (tags): Put TAGS file in the source * testsuite/Makefile.in (tags): Put TAGS file in the source
......
...@@ -17,15 +17,15 @@ strndup (const char *s, size_t size) ...@@ -17,15 +17,15 @@ strndup (const char *s, size_t size)
char *end = memchr(s, 0, size); char *end = memchr(s, 0, size);
if (end) if (end)
/* Length + 1 */ /* strlen, i.e., excluding the terminating NUL. */
size = end - s + 1; size = end - s;
r = malloc(size); r = malloc(size+1);
if (size) if (r)
{ {
memcpy(r, s, size-1); memcpy(r, s, size);
r[size-1] = '\0'; r[size] = '\0';
} }
return r; return r;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment