finish_tmp_packfile():use strbuf for pathname construction
The old version fixes a maximum length on the buffer, which could be a problem if one is not certain of the length of get_object_directory(). Using strbuf can avoid the protential bug. Helped-by: Michael Haggerty <mhagger@alum.mit.edu> Helped-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Sun He <sunheehnus@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Sun He committed
Mar 3, 2014 at 17:24 UTC
5889271114a25b6750bb6137784ae5a93df22b39
4 files changed
+22
-21
builtin/pack-objects.c
+6
-9
@@ -803,7 +803,7 @@ static void write_pack_file(void)
803
804
if (!pack_to_stdout) {
805
struct stat st;
806
- char tmpname[PATH_MAX];
806
+ struct strbuf tmpname = STRBUF_INIT;
807
808
/*
809
* Packs are runtime accessed in their mtime
@@ -826,23 +826,19 @@ static void write_pack_file(void)
826
pack_tmp_name, strerror(errno));
827
}
828
829
- /* Enough space for "-<sha-1>.pack"? */
830
- if (sizeof(tmpname) <= strlen(base_name) + 50)
831
- die("pack base name '%s' too long", base_name);
832
- snprintf(tmpname, sizeof(tmpname), "%s-", base_name);
829
+ strbuf_addf(&tmpname, "%s-", base_name);
830
831
if (write_bitmap_index) {
832
bitmap_writer_set_checksum(sha1);
833
bitmap_writer_build_type_index(written_list, nr_written);
834
}
835
839
- finish_tmp_packfile(tmpname, pack_tmp_name,
836
+ finish_tmp_packfile(&tmpname, pack_tmp_name,
837
written_list, nr_written,
838
&pack_idx_opts, sha1);
839
840
if (write_bitmap_index) {
844
- char *end_of_name_prefix = strrchr(tmpname, 0);
845
- sprintf(end_of_name_prefix, "%s.bitmap", sha1_to_hex(sha1));
841
+ strbuf_addf(&tmpname, "%s.bitmap", sha1_to_hex(sha1));
842
843
stop_progress(&progress_state);
844
@@ -851,10 +847,11 @@ static void write_pack_file(void)
847
bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);
848
bitmap_writer_build(&to_pack);
849
bitmap_writer_finish(written_list, nr_written,
854
- tmpname, write_bitmap_options);
850
+ tmpname.buf, write_bitmap_options);
851
write_bitmap_index = 0;
852
}
853
854
+ strbuf_release(&tmpname);
855
free(pack_tmp_name);
856
puts(sha1_to_hex(sha1));
857
}
bulk-checkin.c
+5
-3
@@ -4,6 +4,7 @@
4
#include "bulk-checkin.h"
5
#include "csum-file.h"
6
#include "pack.h"
7
+#include "strbuf.h"
8
9
static int pack_compression_level = Z_DEFAULT_COMPRESSION;
10
@@ -23,7 +24,7 @@ static struct bulk_checkin_state {
24
static void finish_bulk_checkin(struct bulk_checkin_state *state)
25
{
26
unsigned char sha1[20];
26
- char packname[PATH_MAX];
27
+ struct strbuf packname = STRBUF_INIT;
28
int i;
29
30
if (!state->f)
@@ -43,8 +44,8 @@ static void finish_bulk_checkin(struct bulk_checkin_state *state)
44
close(fd);
45
}
46
46
- sprintf(packname, "%s/pack/pack-", get_object_directory());
47
- finish_tmp_packfile(packname, state->pack_tmp_name,
47
+ strbuf_addf(&packname, "%s/pack/pack-", get_object_directory());
48
+ finish_tmp_packfile(&packname, state->pack_tmp_name,
49
state->written, state->nr_written,
50
&state->pack_idx_opts, sha1);
51
for (i = 0; i < state->nr_written; i++)
@@ -54,6 +55,7 @@ clear_exit:
55
free(state->written);
56
memset(state, 0, sizeof(*state));
57
58
+ strbuf_release(&packname);
59
/* Make objects we just wrote available to ourselves */
60
reprepare_packed_git();
61
}
pack-write.c
+10
-8
@@ -336,7 +336,7 @@ struct sha1file *create_tmp_packfile(char **pack_tmp_name)
336
return sha1fd(fd, *pack_tmp_name);
337
}
338
339
-void finish_tmp_packfile(char *name_buffer,
339
+void finish_tmp_packfile(struct strbuf *name_buffer,
340
const char *pack_tmp_name,
341
struct pack_idx_entry **written_list,
342
uint32_t nr_written,
@@ -344,7 +344,7 @@ void finish_tmp_packfile(char *name_buffer,
344
unsigned char sha1[])
345
{
346
const char *idx_tmp_name;
347
- char *end_of_name_prefix = strrchr(name_buffer, 0);
347
+ int basename_len = name_buffer->len;
348
349
if (adjust_shared_perm(pack_tmp_name))
350
die_errno("unable to make temporary pack file readable");
@@ -354,17 +354,19 @@ void finish_tmp_packfile(char *name_buffer,
354
if (adjust_shared_perm(idx_tmp_name))
355
die_errno("unable to make temporary index file readable");
356
357
- sprintf(end_of_name_prefix, "%s.pack", sha1_to_hex(sha1));
358
- free_pack_by_name(name_buffer);
357
+ strbuf_addf(name_buffer, "%s.pack", sha1_to_hex(sha1));
358
+ free_pack_by_name(name_buffer->buf);
359
360
- if (rename(pack_tmp_name, name_buffer))
360
+ if (rename(pack_tmp_name, name_buffer->buf))
361
die_errno("unable to rename temporary pack file");
362
363
- sprintf(end_of_name_prefix, "%s.idx", sha1_to_hex(sha1));
364
- if (rename(idx_tmp_name, name_buffer))
363
+ strbuf_setlen(name_buffer, basename_len);
364
+
365
+ strbuf_addf(name_buffer, "%s.idx", sha1_to_hex(sha1));
366
+ if (rename(idx_tmp_name, name_buffer->buf))
367
die_errno("unable to rename temporary index file");
368
367
- *end_of_name_prefix = '\0';
369
+ strbuf_setlen(name_buffer, basename_len);
370
371
free((void *)idx_tmp_name);
372
}
pack.h
+1
-1
@@ -91,6 +91,6 @@ extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned ch
91
extern int read_pack_header(int fd, struct pack_header *);
92
93
extern struct sha1file *create_tmp_packfile(char **pack_tmp_name);
94
-extern void finish_tmp_packfile(char *name_buffer, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, struct pack_idx_option *pack_idx_opts, unsigned char sha1[]);
94
+extern void finish_tmp_packfile(struct strbuf *name_buffer, const char *pack_tmp_name, struct pack_idx_entry **written_list, uint32_t nr_written, struct pack_idx_option *pack_idx_opts, unsigned char sha1[]);
95
96
#endif