From f8889a5d12d61df697c12ca60bd2ee7ef4ee0dbc Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Tue, 10 Mar 2009 20:52:50 +0000 Subject: [PATCH] don't use strlen in strndup(). --- ChangeLog | 4 ++++ src/softmagic.c | 15 +++++++-------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 11438e86..8b6fb9cc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2009-03-10 16:50 Christos Zoulas + + * don't use strlen in strndup() (Toby Peterson) + 2009-03-10 7:45 Christos Zoulas * avoid c99 syntax. diff --git a/src/softmagic.c b/src/softmagic.c index daf9ccd6..02ad1fdf 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -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 */ -- 2.50.1