Skip to content

Commit 880fb8d

Browse files
rscharfegitster
authored andcommitted
sha1_file: replace PATH_MAX buffer with strbuf in prepare_packed_git_one()
Instead of using strbuf to create a message string in case a path is too long for our fixed-size buffer, replace that buffer with a strbuf and thus get rid of the limitation. Helped-by: Duy Nguyen <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 341e7e8 commit 880fb8d

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

sha1_file.c

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,56 +1177,45 @@ static void report_pack_garbage(struct string_list *list)
11771177

11781178
static void prepare_packed_git_one(char *objdir, int local)
11791179
{
1180-
/* Ensure that this buffer is large enough so that we can
1181-
append "/pack/" without clobbering the stack even if
1182-
strlen(objdir) were PATH_MAX. */
1183-
char path[PATH_MAX + 1 + 4 + 1 + 1];
1184-
int len;
1180+
struct strbuf path = STRBUF_INIT;
1181+
size_t dirnamelen;
11851182
DIR *dir;
11861183
struct dirent *de;
11871184
struct string_list garbage = STRING_LIST_INIT_DUP;
11881185

1189-
sprintf(path, "%s/pack", objdir);
1190-
len = strlen(path);
1191-
dir = opendir(path);
1186+
strbuf_addstr(&path, objdir);
1187+
strbuf_addstr(&path, "/pack");
1188+
dir = opendir(path.buf);
11921189
if (!dir) {
11931190
if (errno != ENOENT)
11941191
error("unable to open object pack directory: %s: %s",
1195-
path, strerror(errno));
1192+
path.buf, strerror(errno));
1193+
strbuf_release(&path);
11961194
return;
11971195
}
1198-
path[len++] = '/';
1196+
strbuf_addch(&path, '/');
1197+
dirnamelen = path.len;
11991198
while ((de = readdir(dir)) != NULL) {
1200-
int namelen = strlen(de->d_name);
12011199
struct packed_git *p;
12021200

1203-
if (len + namelen + 1 > sizeof(path)) {
1204-
if (report_garbage) {
1205-
struct strbuf sb = STRBUF_INIT;
1206-
strbuf_addf(&sb, "%.*s/%s", len - 1, path, de->d_name);
1207-
report_garbage("path too long", sb.buf);
1208-
strbuf_release(&sb);
1209-
}
1210-
continue;
1211-
}
1212-
12131201
if (is_dot_or_dotdot(de->d_name))
12141202
continue;
12151203

1216-
strcpy(path + len, de->d_name);
1204+
strbuf_setlen(&path, dirnamelen);
1205+
strbuf_addstr(&path, de->d_name);
12171206

12181207
if (has_extension(de->d_name, ".idx")) {
12191208
/* Don't reopen a pack we already have. */
12201209
for (p = packed_git; p; p = p->next) {
1221-
if (!memcmp(path, p->pack_name, len + namelen - 4))
1210+
if (!memcmp(path.buf, p->pack_name, path.len - 4))
12221211
break;
12231212
}
12241213
if (p == NULL &&
12251214
/*
12261215
* See if it really is a valid .idx file with
12271216
* corresponding .pack file that we can map.
12281217
*/
1229-
(p = add_packed_git(path, len + namelen, local)) != NULL)
1218+
(p = add_packed_git(path.buf, path.len, local)) != NULL)
12301219
install_packed_git(p);
12311220
}
12321221

@@ -1237,13 +1226,14 @@ static void prepare_packed_git_one(char *objdir, int local)
12371226
has_extension(de->d_name, ".pack") ||
12381227
has_extension(de->d_name, ".bitmap") ||
12391228
has_extension(de->d_name, ".keep"))
1240-
string_list_append(&garbage, path);
1229+
string_list_append(&garbage, path.buf);
12411230
else
1242-
report_garbage("garbage found", path);
1231+
report_garbage("garbage found", path.buf);
12431232
}
12441233
closedir(dir);
12451234
report_pack_garbage(&garbage);
12461235
string_list_clear(&garbage, 0);
1236+
strbuf_release(&path);
12471237
}
12481238

12491239
static int sort_pack(const void *a_, const void *b_)

0 commit comments

Comments
 (0)