Skip to content

Commit 62f1751

Browse files
peffgitster
authored andcommitted
test-path-utils: fix normalize_path_copy output buffer size
The normalize_path_copy function needs an output buffer that is at least as long as its input (it may shrink the path, but never expand it). However, this test program feeds it static PATH_MAX-sized buffers, which have no relation to the input size. In the normalize_ceiling_entry case, we do at least check the size against PATH_MAX and die(), but that case is even more convoluted. We normalize into a fixed-size buffer, free the original, and then replace it with a strdup'd copy of the result. But normalize_path_copy explicitly allows normalizing in-place, so we can simply do that. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 5545f05 commit 62f1751

1 file changed

Lines changed: 4 additions & 11 deletions

File tree

test-path-utils.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,14 @@
88
*/
99
static int normalize_ceiling_entry(struct string_list_item *item, void *unused)
1010
{
11-
const char *ceil = item->string;
12-
int len = strlen(ceil);
13-
char buf[PATH_MAX+1];
11+
char *ceil = item->string;
1412

15-
if (len == 0)
13+
if (!*ceil)
1614
die("Empty path is not supported");
17-
if (len > PATH_MAX)
18-
die("Path \"%s\" is too long", ceil);
1915
if (!is_absolute_path(ceil))
2016
die("Path \"%s\" is not absolute", ceil);
21-
if (normalize_path_copy(buf, ceil) < 0)
17+
if (normalize_path_copy(ceil, ceil) < 0)
2218
die("Path \"%s\" could not be normalized", ceil);
23-
len = strlen(buf);
24-
free(item->string);
25-
item->string = xstrdup(buf);
2619
return 1;
2720
}
2821

@@ -166,7 +159,7 @@ static struct test_data dirname_data[] = {
166159
int main(int argc, char **argv)
167160
{
168161
if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
169-
char *buf = xmalloc(PATH_MAX + 1);
162+
char *buf = xmallocz(strlen(argv[2]));
170163
int rv = normalize_path_copy(buf, argv[2]);
171164
if (rv)
172165
buf = "++failed++";

0 commit comments

Comments
 (0)