[klibc] [PATCH] strndup(): Fix possible null pointer dereference

maximilian attems max at stro.at
Fri Jun 10 07:43:58 PDT 2011


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



More information about the klibc mailing list