$OpenBSD: patch-src_softmagic_c,v 1.3 2008/09/25 20:43:56 rui Exp $ --- src/softmagic.c.orig Mon Jul 28 18:25:21 2008 +++ src/softmagic.c Sat Sep 20 17:24:18 2008 @@ -303,21 +303,26 @@ check_fmt(struct magic_set *ms, struct magic *m) #ifndef HAVE_STRNDUP char * strndup(const char *, size_t); +size_t strnlen (const char *, size_t); +size_t +strnlen (const char *string, size_t maxlen) +{ + const char *end = memchr (string, '\0', maxlen); + return end ? (size_t) (end - string) : maxlen; +} + char * strndup(const char *str, size_t n) { - size_t len; - char *copy; + size_t len = strnlen (str, n); + char *copy = malloc (len + 1); - len = strlen(str); - if (len > n) - len = n; - if (!(copy = malloc(len + 1))) - return (NULL); - (void) memcpy(copy, str, len + 1); + if (copy == NULL) + return NULL; + copy[len] = '\0'; - return (copy); + return memcpy (copy, str, len); } #endif /* HAVE_STRNDUP */