1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-26 12:27:52 +00:00

make semantics of custom strndup() comply with glibc

From strdup(3):
| If s is longer than n, only n characters are copied, and a
| terminating null byte ('\0') is added.

So allocate at most n+1 bytes and make sure the last one is zero, as
strncpy() doesn't add it itself.

So in fact to allow a maximum space for string dup of 23, strndup() has
to be called like this:
| dup = strndup(src, 23 - 1);

FIXME: Find the critical points in code this change touches and make
sure the invocation there is correct.
This commit is contained in:
Phil Sutter 2008-12-09 00:12:23 +01:00
parent f96e77c91b
commit 4b89c3b17d

View File

@ -35,9 +35,10 @@
// use our own strndup() if it's not available
char *strndup(const char *s, size_t n)
{
if (strlen(s) + 1 > n) {
char *ret = malloc(n);
if (strlen(s) > n) {
char *ret = malloc(n + 1);
strncpy(ret, s, n);
ret[n] = 0;
return ret;
} else {
return strdup(s);