summaryrefslogtreecommitdiff
path: root/cat
diff options
context:
space:
mode:
authorTim Kientzle <kientzle@gmail.com>2016-06-02 20:17:13 -0700
committerTim Kientzle <kientzle@gmail.com>2016-06-02 20:17:13 -0700
commit83c3e2e675d26bfafcc9b3e1bcf166ee4ba93611 (patch)
treec36a7581f11d4a0796eeefc245718972a3f3ed65 /cat
parented497f0e18339d5412e0dfd9e86e671187dadc9d (diff)
downloadlibarchive-83c3e2e675d26bfafcc9b3e1bcf166ee4ba93611.tar.gz
Issue 657: Allow up to 8k for the test root directory name
Diffstat (limited to 'cat')
-rw-r--r--cat/test/main.c75
1 files changed, 49 insertions, 26 deletions
diff --git a/cat/test/main.c b/cat/test/main.c
index 319f68c7..0aa1deb5 100644
--- a/cat/test/main.c
+++ b/cat/test/main.c
@@ -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