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 <pclouds@gmail.com> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jun 30, 2014 at 12:55 UTC
880fb8de67af3aeeae5a59ba5ce84ad3d0cf1dda
1 file changed
+16
-26
sha1_file.c
+16
-26
@@ -1177,48 +1177,37 @@ static void report_pack_garbage(struct string_list *list)
1177
1178
static void prepare_packed_git_one(char *objdir, int local)
1179
{
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;
1182
DIR *dir;
1183
struct dirent *de;
1184
struct string_list garbage = STRING_LIST_INIT_DUP;
1185
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);
1189
if (!dir) {
1190
if (errno != ENOENT)
1191
error("unable to open object pack directory: %s: %s",
1195
- path, strerror(errno));
1192
+ path.buf, strerror(errno));
1193
+ strbuf_release(&path);
1194
return;
1195
}
1198
- path[len++] = '/';
1196
+ strbuf_addch(&path, '/');
1197
+ dirnamelen = path.len;
1198
while ((de = readdir(dir)) != NULL) {
1200
- int namelen = strlen(de->d_name);
1199
struct packed_git *p;
1200
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
-
1201
if (is_dot_or_dotdot(de->d_name))
1202
continue;
1203
1216
- strcpy(path + len, de->d_name);
1204
+ strbuf_setlen(&path, dirnamelen);
1205
+ strbuf_addstr(&path, de->d_name);
1206
1207
if (has_extension(de->d_name, ".idx")) {
1208
/* Don't reopen a pack we already have. */
1209
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))
1211
break;
1212
}
1213
if (p == NULL &&
@@ -1226,7 +1215,7 @@ static void prepare_packed_git_one(char *objdir, int local)
1215
* See if it really is a valid .idx file with
1216
* corresponding .pack file that we can map.
1217
*/
1229
- (p = add_packed_git(path, len + namelen, local)) != NULL)
1218
+ (p = add_packed_git(path.buf, path.len, local)) != NULL)
1219
install_packed_git(p);
1220
}
1221
@@ -1237,13 +1226,14 @@ static void prepare_packed_git_one(char *objdir, int local)
1226
has_extension(de->d_name, ".pack") ||
1227
has_extension(de->d_name, ".bitmap") ||
1228
has_extension(de->d_name, ".keep"))
1240
- string_list_append(&garbage, path);
1229
+ string_list_append(&garbage, path.buf);
1230
else
1242
- report_garbage("garbage found", path);
1231
+ report_garbage("garbage found", path.buf);
1232
}
1233
closedir(dir);
1234
report_pack_garbage(&garbage);
1235
string_list_clear(&garbage, 0);
1236
+ strbuf_release(&path);
1237
}
1238
1239
static int sort_pack(const void *a_, const void *b_)