maximilian attems
2011-Jun-10 14:43 UTC
[klibc] [PATCH] strndup(): Fix possible null pointer dereference
Directly return NULL if malloc failed. Signed-off-by: maximilian attems <max at stro.at> --- usr/klibc/strndup.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-) diff --git a/usr/klibc/strndup.c b/usr/klibc/strndup.c index 8b5974a..65afd44 100644 --- a/usr/klibc/strndup.c +++ b/usr/klibc/strndup.c @@ -10,8 +10,10 @@ char *strndup(const char *s, size_t n) int l = n > strlen(s) ? strlen(s) + 1 : n + 1; char *d = malloc(l); - if (d) - memcpy(d, s, l); + if (!d) + return NULL; + + memcpy(d, s, l); d[n] = '\0'; return d; } -- 1.7.2.5