]> granicus.if.org Git - file/commitdiff
don't use strlen in strndup().
authorChristos Zoulas <christos@zoulas.com>
Tue, 10 Mar 2009 20:52:50 +0000 (20:52 +0000)
committerChristos Zoulas <christos@zoulas.com>
Tue, 10 Mar 2009 20:52:50 +0000 (20:52 +0000)
ChangeLog
src/softmagic.c

index 11438e86f5e5ba30a29fcbb18fafc92e9a58833e..8b6fb9ccbf16b33ba72bc92d777260d7307f398a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2009-03-10  16:50  Christos Zoulas <christos@zoulas.com>
+
+       * don't use strlen in strndup() (Toby Peterson)
+
 2009-03-10  7:45  Christos Zoulas <christos@zoulas.com>
 
        * avoid c99 syntax.
index daf9ccd6d506ee08872e3cd6b3acd3093d694f0d..02ad1fdf6606b46f2e936a8abcdd1a268ec5f4bf 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.133 2008/11/07 22:50:37 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.134 2009/03/10 20:52:50 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -338,14 +338,13 @@ strndup(const char *str, size_t n)
        size_t len;
        char *copy;
 
-       len = strlen(str);
-       if (len > n)
-               len = n;
-       if (!(copy = malloc(len + 1)))
-               return (NULL);
-       (void) memcpy(copy, str, len + 1);
+       for (len = 0; len < n && str[len]; len++)
+               continue;
+       if ((copy = malloc(len + 1)) == NULL)
+               return NULL;
+       (void)memcpy(copy, str, len);
        copy[len] = '\0';
-       return (copy);
+       return copy;
 }
 #endif /* HAVE_STRNDUP */