replace snprintf with odb_pack_name()
In several places we write the name of the pack filename into a fixed-size buffer using snprintf(), but do not check the return value. As a result, a very long object directory could cause us to quietly truncate the pack filename (potentially leading to a corrupted repository, as a newly written packfile could be missing its .pack extension). We can use odb_pack_name() to do this with a strbuf (and shorten the code, as well). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Mar 16, 2017 at 10:27 UTC
ba47a3088f04ac3d2833bea56ee366be1054db8d
2 files changed
+24
-31
builtin/index-pack.c
+11
-16
@@ -1386,7 +1386,7 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1386
unsigned char *sha1)
1387
{
1388
const char *report = "pack";
1389
- char name[PATH_MAX];
1389
+ struct strbuf name = STRBUF_INIT;
1390
int err;
1391
1392
if (!from_stdin) {
@@ -1402,14 +1402,13 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1402
int keep_fd, keep_msg_len = strlen(keep_msg);
1403
1404
if (!keep_name)
1405
- snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
1406
- get_object_directory(), sha1_to_hex(sha1));
1405
+ odb_pack_name(&name, sha1, "keep");
1406
1408
- keep_fd = odb_pack_keep(keep_name ? keep_name : name);
1407
+ keep_fd = odb_pack_keep(keep_name ? keep_name : name.buf);
1408
if (keep_fd < 0) {
1409
if (errno != EEXIST)
1410
die_errno(_("cannot write keep file '%s'"),
1412
- keep_name ? keep_name : name);
1411
+ keep_name ? keep_name : name.buf);
1412
} else {
1413
if (keep_msg_len > 0) {
1414
write_or_die(keep_fd, keep_msg, keep_msg_len);
@@ -1417,28 +1416,22 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1416
}
1417
if (close(keep_fd) != 0)
1418
die_errno(_("cannot close written keep file '%s'"),
1420
- keep_name ? keep_name : name);
1419
+ keep_name ? keep_name : name.buf);
1420
report = "keep";
1421
}
1422
}
1423
1424
if (final_pack_name != curr_pack_name) {
1426
- if (!final_pack_name) {
1427
- snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
1428
- get_object_directory(), sha1_to_hex(sha1));
1429
- final_pack_name = name;
1430
- }
1425
+ if (!final_pack_name)
1426
+ final_pack_name = odb_pack_name(&name, sha1, "pack");
1427
if (finalize_object_file(curr_pack_name, final_pack_name))
1428
die(_("cannot store pack file"));
1429
} else if (from_stdin)
1430
chmod(final_pack_name, 0444);
1431
1432
if (final_index_name != curr_index_name) {
1437
- if (!final_index_name) {
1438
- snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
1439
- get_object_directory(), sha1_to_hex(sha1));
1440
- final_index_name = name;
1441
- }
1433
+ if (!final_index_name)
1434
+ final_index_name = odb_pack_name(&name, sha1, "idx");
1435
if (finalize_object_file(curr_index_name, final_index_name))
1436
die(_("cannot store index file"));
1437
} else
@@ -1464,6 +1457,8 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
1457
input_offset += err;
1458
}
1459
}
1460
+
1461
+ strbuf_release(&name);
1462
}
1463
1464
static int git_index_pack_config(const char *k, const char *v, void *cb)
fast-import.c
+13
-15
@@ -940,43 +940,40 @@ static const char *create_index(void)
940
941
static char *keep_pack(const char *curr_index_name)
942
{
943
- static char name[PATH_MAX];
943
static const char *keep_msg = "fast-import";
944
+ struct strbuf name = STRBUF_INIT;
945
int keep_fd;
946
947
- snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
948
- get_object_directory(), sha1_to_hex(pack_data->sha1));
949
- keep_fd = odb_pack_keep(name);
947
+ odb_pack_name(&name, pack_data->sha1, "keep");
948
+ keep_fd = odb_pack_keep(name.buf);
949
if (keep_fd < 0)
950
die_errno("cannot create keep file");
951
write_or_die(keep_fd, keep_msg, strlen(keep_msg));
952
if (close(keep_fd))
953
die_errno("failed to write keep file");
954
956
- snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
957
- get_object_directory(), sha1_to_hex(pack_data->sha1));
958
- if (finalize_object_file(pack_data->pack_name, name))
955
+ odb_pack_name(&name, pack_data->sha1, "pack");
956
+ if (finalize_object_file(pack_data->pack_name, name.buf))
957
die("cannot store pack file");
958
961
- snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
962
- get_object_directory(), sha1_to_hex(pack_data->sha1));
963
- if (finalize_object_file(curr_index_name, name))
959
+ odb_pack_name(&name, pack_data->sha1, "idx");
960
+ if (finalize_object_file(curr_index_name, name.buf))
961
die("cannot store index file");
962
free((void *)curr_index_name);
966
- return name;
963
+ return strbuf_detach(&name, NULL);
964
}
965
966
static void unkeep_all_packs(void)
967
{
971
- static char name[PATH_MAX];
968
+ struct strbuf name = STRBUF_INIT;
969
int k;
970
971
for (k = 0; k < pack_id; k++) {
972
struct packed_git *p = all_packs[k];
976
- snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
977
- get_object_directory(), sha1_to_hex(p->sha1));
978
- unlink_or_warn(name);
973
+ odb_pack_name(&name, p->sha1, "keep");
974
+ unlink_or_warn(name.buf);
975
}
976
+ strbuf_release(&name);
977
}
978
979
static int loosen_small_pack(const struct packed_git *p)
@@ -1035,6 +1032,7 @@ static void end_packfile(void)
1032
die("core git rejected index %s", idx_name);
1033
all_packs[pack_id] = new_p;
1034
install_packed_git(new_p);
1035
+ free(idx_name);
1036
1037
/* Print the boundary */
1038
if (pack_edges) {