From: Luca Bacci Date: Thu, 29 Dec 2022 17:02:19 +0000 (+0100) Subject: Make mk_ancestry () / mk_ancestry_iter () work without fixed-size buffers X-Git-Tag: archive/raspbian/1%0.1.106-3+rpi1^2~15^2^2~38 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=219c6cc8a5da766b4d1b4a777d017d8f1b722c44;p=babl.git Make mk_ancestry () / mk_ancestry_iter () work without fixed-size buffers --- diff --git a/babl/babl-cache.c b/babl/babl-cache.c index 211bea5..9e5ff96 100644 --- a/babl/babl-cache.c +++ b/babl/babl-cache.c @@ -35,42 +35,62 @@ static int mk_ancestry_iter (const char *path) { - char copy[4096]; - strncpy (copy, path, 4096); - copy[sizeof (copy) - 1] = '\0'; - if (strrchr (copy, '/')) + char *copy = babl_strdup (path); + char *rchr = NULL; + int result = 0; + + if (!copy) + return -1; + + rchr = strrchr (copy, '/'); + if (rchr) { - *strrchr (copy, '/') = '\0'; + *rchr = '\0'; + if (copy[0]) { BablStat stat_buf; if ( ! (_babl_stat (copy, &stat_buf)==0 && S_ISDIR(stat_buf.st_mode))) { if (mk_ancestry_iter (copy) != 0) - return -1; + { + result = -1; + } + else + { #ifndef _WIN32 - return _babl_mkdir (copy, S_IRWXU); + result = _babl_mkdir (copy, S_IRWXU); #else - return _babl_mkdir (copy); + result = _babl_mkdir (copy); #endif + } } } } - return 0; + + babl_free (copy); + return result; } static int mk_ancestry (const char *path) { - char copy[4096]; - strncpy (copy, path, 4096); - copy[sizeof (copy) - 1] = '\0'; + char *copy = babl_strdup (path); + int result = 0; + + if (!copy) + return -1; + #ifdef _WIN32 for (char *c = copy; *c; c++) if (*c == '\\') *c = '/'; #endif - return mk_ancestry_iter (copy); + + result = mk_ancestry_iter (copy); + + babl_free (copy); + return result; } static char *