From: Christos Zoulas Date: Thu, 24 Feb 2011 03:35:59 +0000 (+0000) Subject: use asprintf() instead of snprintf() strdup() X-Git-Tag: FILE5_07~42 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=219f3c18fe3f46c2579db1ac4cf6f37eb9d924d2;p=file use asprintf() instead of snprintf() strdup() --- diff --git a/src/apprentice.c b/src/apprentice.c index 16ae5c12..ed16939c 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.164 2011/01/04 19:29:32 rrt Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.165 2011/01/16 19:30:36 rrt Exp $") #endif /* lint */ #include "magic.h" @@ -72,10 +72,6 @@ FILE_RCSID("@(#)$File: apprentice.c,v 1.164 2011/01/04 19:29:32 rrt Exp $") #define MAP_FILE 0 #endif -#ifndef MAXPATHLEN -#define MAXPATHLEN 1024 -#endif - struct magic_entry { struct magic *mp; uint32_t cont_count; @@ -756,7 +752,7 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, struct magic_entry *marray; uint32_t marraycount, i, mentrycount = 0, starttest; size_t slen, files = 0, maxfiles = 0; - char subfn[MAXPATHLEN], **filearr = NULL, *mfn; + char **filearr = NULL, *mfn; struct stat st; DIR *dir; struct dirent *d; @@ -783,15 +779,16 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, goto out; } while ((d = readdir(dir)) != NULL) { - (void)snprintf(subfn, sizeof(subfn), "%s/%s", - fn, d->d_name); - if (stat(subfn, &st) == -1 || !S_ISREG(st.st_mode)) - continue; - if ((mfn = strdup(subfn)) == NULL) { - file_oomem(ms, strlen(subfn)); + if (asprintf(&mfn, "%s/%s", fn, d->d_name) < 0) { + file_oomem(ms, + strlen(fn) + strlen(d->d_name) + 2); errs++; goto out; } + if (stat(mfn, &st) == -1 || !S_ISREG(st.st_mode)) { + free(mfn); + continue; + } if (files >= maxfiles) { size_t mlen; maxfiles = (maxfiles + 1) * 2; @@ -799,6 +796,7 @@ apprentice_load(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, if ((filearr = CAST(char **, realloc(filearr, mlen))) == NULL) { file_oomem(ms, mlen); + free(mfn); errs++; goto out; }