{
/* 512 KiB chunk size */
ssize_t i, chunk_count = 1, chunk = 512 * 1024, entries = 0;
- char *input = malloc(sizeof(char) * chunk), *tmpdir;
- char cwd[PATH_MAX], *next, *prev, *tmp;
- size_t offsets[1 << 16];
+ char *input = malloc(sizeof(char) * chunk), *tmpdir = NULL;
+ char cwd[PATH_MAX], *next, *tmp;
char *paths[1 << 16];
ssize_t input_read, total_read = 0, off = 0;
return NULL;
}
+ if (!getcwd(cwd, PATH_MAX)) {
+ free(input);
+ return NULL;
+ }
+
while (chunk_count < 512) {
input_read = read(STDIN_FILENO, input, chunk);
if (input_read < 0) {
if (entries == (1 << 16))
goto malloc_1;
- offsets[entries++] = off;
+ paths[entries++] = input + off;
off = next - input;
}
break;
if (chunk_count == 512 || !(input = xrealloc(input, (chunk_count + 1) * chunk)))
- goto malloc_1;
+ goto malloc_2;
}
if (off != total_read) {
if (entries == (1 << 16))
- goto malloc_1;
+ goto malloc_2;
- offsets[entries++] = off;
+ paths[entries++] = input + off;
}
if (!entries)
input[total_read] = '\0';
- for (i = 0; i < entries; ++i)
- paths[i] = input + offsets[i];
-
- /* prev used as tmp variable */
- prev = getcwd(cwd, PATH_MAX);
- if (!prev)
- goto malloc_1;
-
- for (i = 0; i < entries; ++i) {
- if (!(paths[i] = xrealpath(paths[i], cwd))) {
- for (--i; i >= 0; --i)
- free(paths[i]);
- goto malloc_1;
- }
- }
-
g_prefixpath = malloc(sizeof(char) * PATH_MAX);
if (!g_prefixpath)
goto malloc_2;
+ if (!(paths[0] = xrealpath(paths[0], cwd)))
+ goto malloc_2; // free all entries
+
xstrlcpy(g_prefixpath, paths[0], strlen(paths[0]) + 1);
- for (i = 1; i < entries; ++i) {
- if (!common_prefix(paths[i], g_prefixpath))
- goto malloc_2;
- }
+
+ for (i = 1; i < entries; ++i)
+ if (!(paths[i] = xrealpath(paths[i], cwd)) || !common_prefix(paths[i], g_prefixpath))
+ goto malloc_2; // free all entries
if (entries == 1) {
tmp = xmemrchr((uchar *)g_prefixpath, '/', strlen(g_prefixpath));
tmpdir = make_tmp_tree(paths, entries, g_prefixpath);
- if (tmpdir) {
- for (i = entries - 1; i >= 0; --i)
- free(paths[i]);
- free(input);
-
- return tmpdir;
- }
-
malloc_2:
for (i = entries - 1; i >= 0; --i)
free(paths[i]);
malloc_1:
free(input);
- return NULL;
+ return tmpdir;
}
static void check_key_collision(void)