util: use strnlen() in strndup() implementations

If the string being copied is not NULL-terminated the result of
strlen() is undefined.

Signed-off-by: Samuel Iglesias Gonsalvez <siglesias@igalia.com>
Reviewed-by: Neil Roberts <neil@linux.intel.com>
Reviewed-by: Jose Fonseca <jfonseca@vmware.com>
This commit is contained in:
Samuel Iglesias Gonsalvez 2015-09-29 16:10:02 +02:00
parent 023165a734
commit f3afcbecc6
2 changed files with 2 additions and 8 deletions

View file

@ -359,10 +359,7 @@ ralloc_strndup(const void *ctx, const char *str, size_t max)
if (unlikely(str == NULL))
return NULL;
n = strlen(str);
if (n > max)
n = max;
n = strnlen(str, max);
ptr = ralloc_array(ctx, char, n + 1);
memcpy(ptr, str, n);
ptr[n] = '\0';

View file

@ -35,10 +35,7 @@ strndup(const char *str, size_t max)
if (!str)
return NULL;
n = strlen(str);
if (n > max)
n = max;
n = strnlen(str, max);
ptr = (char *) calloc(n + 1, sizeof(char));
if (!ptr)
return NULL;