]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Issue 657: Allow up to 8k for the test root directory name
authorTim Kientzle <kientzle@gmail.com>
Fri, 3 Jun 2016 03:17:13 +0000 (20:17 -0700)
committerTim Kientzle <kientzle@gmail.com>
Fri, 3 Jun 2016 03:17:13 +0000 (20:17 -0700)
cat/test/main.c
cpio/test/main.c
libarchive/test/main.c
tar/test/main.c

index 319f68c774f0fbb1170d582cef05d75ea8021ed4..0aa1deb572b361caf67132553e5d43f839455515 100644 (file)
@@ -2534,18 +2534,36 @@ usage(const char *program)
 static char *
 get_refdir(const char *d)
 {
-       char tried[512] = { '\0' };
-       char buff[128];
-       char *pwd, *p;
+       size_t tried_size, buff_size;
+       char *buff, *tried, *pwd = NULL, *p = NULL;
+
+#ifdef PATH_MAX
+       buff_size = PATH_MAX;
+#else
+       buff_size = 8192;
+#endif
+       buff = calloc(buff_size, 1);
+       if (buff == NULL) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
+
+       /* Allocate a buffer to hold the various directories we checked. */
+       tried_size = buff_size * 2;
+       tried = calloc(tried_size, 1);
+       if (tried == NULL) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
 
        /* If a dir was specified, try that */
        if (d != NULL) {
                pwd = NULL;
-               snprintf(buff, sizeof(buff), "%s", d);
+               snprintf(buff, buff_size, "%s", d);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
                goto failure;
        }
 
@@ -2559,48 +2577,48 @@ get_refdir(const char *d)
                pwd[strlen(pwd) - 1] = '\0';
 
        /* Look for a known file. */
-       snprintf(buff, sizeof(buff), "%s", pwd);
+       snprintf(buff, buff_size, "%s", pwd);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
-       snprintf(buff, sizeof(buff), "%s/test", pwd);
+       snprintf(buff, buff_size, "%s/test", pwd);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
 #if defined(LIBRARY)
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
 #else
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
 #endif
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
 #if defined(PROGRAM_ALIAS)
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 #endif
 
        if (memcmp(pwd, "/usr/obj", 8) == 0) {
-               snprintf(buff, sizeof(buff), "%s", pwd + 8);
+               snprintf(buff, buff_size, "%s", pwd + 8);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
-               snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
+               snprintf(buff, buff_size, "%s/test", pwd + 8);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
        }
 
 failure:
@@ -2615,7 +2633,12 @@ failure:
 success:
        free(p);
        free(pwd);
-       return strdup(buff);
+       free(tried);
+
+       /* Copy result into a fresh buffer to reduce memory usage. */
+       p = strdup(buff);
+       free(buff);
+       return p;
 }
 
 int
index fa22adfbc81a806d2380292278e524d5c3eb8909..1c9ae6e2be5437e0371dd5761ebf14cd40d435f4 100644 (file)
@@ -2535,18 +2535,36 @@ usage(const char *program)
 static char *
 get_refdir(const char *d)
 {
-       char tried[512] = { '\0' };
-       char buff[128];
-       char *pwd, *p;
+       size_t tried_size, buff_size;
+       char *buff, *tried, *pwd = NULL, *p = NULL;
+
+#ifdef PATH_MAX
+       buff_size = PATH_MAX;
+#else
+       buff_size = 8192;
+#endif
+       buff = calloc(buff_size, 1);
+       if (buff == NULL) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
+
+       /* Allocate a buffer to hold the various directories we checked. */
+       tried_size = buff_size * 2;
+       tried = calloc(tried_size, 1);
+       if (tried == NULL) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
 
        /* If a dir was specified, try that */
        if (d != NULL) {
                pwd = NULL;
-               snprintf(buff, sizeof(buff), "%s", d);
+               snprintf(buff, buff_size, "%s", d);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
                goto failure;
        }
 
@@ -2560,48 +2578,48 @@ get_refdir(const char *d)
                pwd[strlen(pwd) - 1] = '\0';
 
        /* Look for a known file. */
-       snprintf(buff, sizeof(buff), "%s", pwd);
+       snprintf(buff, buff_size, "%s", pwd);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
-       snprintf(buff, sizeof(buff), "%s/test", pwd);
+       snprintf(buff, buff_size, "%s/test", pwd);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
 #if defined(LIBRARY)
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
 #else
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
 #endif
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
 #if defined(PROGRAM_ALIAS)
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 #endif
 
        if (memcmp(pwd, "/usr/obj", 8) == 0) {
-               snprintf(buff, sizeof(buff), "%s", pwd + 8);
+               snprintf(buff, buff_size, "%s", pwd + 8);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
-               snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
+               snprintf(buff, buff_size, "%s/test", pwd + 8);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
        }
 
 failure:
@@ -2616,7 +2634,12 @@ failure:
 success:
        free(p);
        free(pwd);
-       return strdup(buff);
+       free(tried);
+
+       /* Copy result into a fresh buffer to reduce memory usage. */
+       p = strdup(buff);
+       free(buff);
+       return p;
 }
 
 int
index e0af4314ea0195b033f75f19e9a655a43d7883d4..0f50e940c9c6774131a9b8db720c44332485e95a 100644 (file)
@@ -2533,18 +2533,36 @@ usage(const char *program)
 static char *
 get_refdir(const char *d)
 {
-       char tried[512] = { '\0' };
-       char buff[128];
-       char *pwd, *p;
+       size_t tried_size, buff_size;
+       char *buff, *tried, *pwd = NULL, *p = NULL;
+
+#ifdef PATH_MAX
+       buff_size = PATH_MAX;
+#else
+       buff_size = 8192;
+#endif
+       buff = calloc(buff_size, 1);
+       if (buff == NULL) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
+
+       /* Allocate a buffer to hold the various directories we checked. */
+       tried_size = buff_size * 2;
+       tried = calloc(tried_size, 1);
+       if (tried == NULL) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
 
        /* If a dir was specified, try that */
        if (d != NULL) {
                pwd = NULL;
-               snprintf(buff, sizeof(buff), "%s", d);
+               snprintf(buff, buff_size, "%s", d);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
                goto failure;
        }
 
@@ -2558,48 +2576,48 @@ get_refdir(const char *d)
                pwd[strlen(pwd) - 1] = '\0';
 
        /* Look for a known file. */
-       snprintf(buff, sizeof(buff), "%s", pwd);
+       snprintf(buff, buff_size, "%s", pwd);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
-       snprintf(buff, sizeof(buff), "%s/test", pwd);
+       snprintf(buff, buff_size, "%s/test", pwd);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
 #if defined(LIBRARY)
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
 #else
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
 #endif
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
 #if defined(PROGRAM_ALIAS)
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 #endif
 
        if (memcmp(pwd, "/usr/obj", 8) == 0) {
-               snprintf(buff, sizeof(buff), "%s", pwd + 8);
+               snprintf(buff, buff_size, "%s", pwd + 8);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
-               snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
+               snprintf(buff, buff_size, "%s/test", pwd + 8);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
        }
 
 failure:
@@ -2614,7 +2632,12 @@ failure:
 success:
        free(p);
        free(pwd);
-       return strdup(buff);
+       free(tried);
+
+       /* Copy result into a fresh buffer to reduce memory usage. */
+       p = strdup(buff);
+       free(buff);
+       return p;
 }
 
 int
index 90801a98bc8fdb2bcdf403391d777cde8afe22ec..08ac6277370a2351fe07337e3eb61300e971e409 100644 (file)
@@ -2535,18 +2535,36 @@ usage(const char *program)
 static char *
 get_refdir(const char *d)
 {
-       char tried[512] = { '\0' };
-       char buff[128];
-       char *pwd, *p;
+       size_t tried_size, buff_size;
+       char *buff, *tried, *pwd = NULL, *p = NULL;
+
+#ifdef PATH_MAX
+       buff_size = PATH_MAX;
+#else
+       buff_size = 8192;
+#endif
+       buff = calloc(buff_size, 1);
+       if (buff == NULL) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
+
+       /* Allocate a buffer to hold the various directories we checked. */
+       tried_size = buff_size * 2;
+       tried = calloc(tried_size, 1);
+       if (tried == NULL) {
+               fprintf(stderr, "Unable to allocate memory\n");
+               exit(1);
+       }
 
        /* If a dir was specified, try that */
        if (d != NULL) {
                pwd = NULL;
-               snprintf(buff, sizeof(buff), "%s", d);
+               snprintf(buff, buff_size, "%s", d);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
                goto failure;
        }
 
@@ -2560,48 +2578,48 @@ get_refdir(const char *d)
                pwd[strlen(pwd) - 1] = '\0';
 
        /* Look for a known file. */
-       snprintf(buff, sizeof(buff), "%s", pwd);
+       snprintf(buff, buff_size, "%s", pwd);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
-       snprintf(buff, sizeof(buff), "%s/test", pwd);
+       snprintf(buff, buff_size, "%s/test", pwd);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
 #if defined(LIBRARY)
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, LIBRARY);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, LIBRARY);
 #else
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM);
 #endif
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
 #if defined(PROGRAM_ALIAS)
-       snprintf(buff, sizeof(buff), "%s/%s/test", pwd, PROGRAM_ALIAS);
+       snprintf(buff, buff_size, "%s/%s/test", pwd, PROGRAM_ALIAS);
        p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
        if (p != NULL) goto success;
-       strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-       strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+       strncat(tried, buff, tried_size - strlen(tried) - 1);
+       strncat(tried, "\n", tried_size - strlen(tried) - 1);
 #endif
 
        if (memcmp(pwd, "/usr/obj", 8) == 0) {
-               snprintf(buff, sizeof(buff), "%s", pwd + 8);
+               snprintf(buff, buff_size, "%s", pwd + 8);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
 
-               snprintf(buff, sizeof(buff), "%s/test", pwd + 8);
+               snprintf(buff, buff_size, "%s/test", pwd + 8);
                p = slurpfile(NULL, "%s/%s", buff, KNOWNREF);
                if (p != NULL) goto success;
-               strncat(tried, buff, sizeof(tried) - strlen(tried) - 1);
-               strncat(tried, "\n", sizeof(tried) - strlen(tried) - 1);
+               strncat(tried, buff, tried_size - strlen(tried) - 1);
+               strncat(tried, "\n", tried_size - strlen(tried) - 1);
        }
 
 failure:
@@ -2616,7 +2634,12 @@ failure:
 success:
        free(p);
        free(pwd);
-       return strdup(buff);
+       free(tried);
+
+       /* Copy result into a fresh buffer to reduce memory usage. */
+       p = strdup(buff);
+       free(buff);
+       return p;
 }
 
 int