Skip to content

Commit cb054eb

Browse files
committed
Merge branch 'jk/snprintf-cleanups'
Code clean-up. * jk/snprintf-cleanups: daemon: use an argv_array to exec children gc: replace local buffer with git_path transport-helper: replace checked snprintf with xsnprintf convert unchecked snprintf into xsnprintf combine-diff: replace malloc/snprintf with xstrfmt replace unchecked snprintf calls with heap buffers receive-pack: print --pack-header directly into argv array name-rev: replace static buffer with strbuf create_branch: use xstrfmt for reflog message create_branch: move msg setup closer to point of use avoid using mksnpath for refs avoid using fixed PATH_MAX buffers for refs fetch: use heap buffer to format reflog tag: use strbuf to format tag header diff: avoid fixed-size buffer for patch-ids odb_mkstemp: use git_path_buf odb_mkstemp: write filename into strbuf do not check odb_mkstemp return value for errors
2 parents cf11a67 + 6a97da3 commit cb054eb

28 files changed

+239
-224
lines changed

bisect.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
200200
{
201201
struct commit_list *p;
202202
struct commit_dist *array = xcalloc(nr, sizeof(*array));
203+
struct strbuf buf = STRBUF_INIT;
203204
int cnt, i;
204205

205206
for (p = list, cnt = 0; p; p = p->next) {
@@ -217,17 +218,18 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
217218
}
218219
QSORT(array, cnt, compare_commit_dist);
219220
for (p = list, i = 0; i < cnt; i++) {
220-
char buf[100]; /* enough for dist=%d */
221221
struct object *obj = &(array[i].commit->object);
222222

223-
snprintf(buf, sizeof(buf), "dist=%d", array[i].distance);
224-
add_name_decoration(DECORATION_NONE, buf, obj);
223+
strbuf_reset(&buf);
224+
strbuf_addf(&buf, "dist=%d", array[i].distance);
225+
add_name_decoration(DECORATION_NONE, buf.buf, obj);
225226

226227
p->item = array[i].commit;
227228
p = p->next;
228229
}
229230
if (p)
230231
p->next = NULL;
232+
strbuf_release(&buf);
231233
free(array);
232234
return list;
233235
}

branch.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ void create_branch(const char *name, const char *start_name,
234234
{
235235
struct commit *commit;
236236
unsigned char sha1[20];
237-
char *real_ref, msg[PATH_MAX + 20];
237+
char *real_ref;
238238
struct strbuf ref = STRBUF_INIT;
239239
int forcing = 0;
240240
int dont_change_ref = 0;
@@ -290,19 +290,18 @@ void create_branch(const char *name, const char *start_name,
290290
die(_("Not a valid branch point: '%s'."), start_name);
291291
hashcpy(sha1, commit->object.oid.hash);
292292

293-
if (forcing)
294-
snprintf(msg, sizeof msg, "branch: Reset to %s",
295-
start_name);
296-
else if (!dont_change_ref)
297-
snprintf(msg, sizeof msg, "branch: Created from %s",
298-
start_name);
299-
300293
if (reflog)
301294
log_all_ref_updates = LOG_REFS_NORMAL;
302295

303296
if (!dont_change_ref) {
304297
struct ref_transaction *transaction;
305298
struct strbuf err = STRBUF_INIT;
299+
char *msg;
300+
301+
if (forcing)
302+
msg = xstrfmt("branch: Reset to %s", start_name);
303+
else
304+
msg = xstrfmt("branch: Created from %s", start_name);
306305

307306
transaction = ref_transaction_begin(&err);
308307
if (!transaction ||
@@ -313,6 +312,7 @@ void create_branch(const char *name, const char *start_name,
313312
die("%s", err.buf);
314313
ref_transaction_free(transaction);
315314
strbuf_release(&err);
315+
free(msg);
316316
}
317317

318318
if (real_ref && track)

builtin/checkout.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,11 +908,10 @@ static int check_tracking_name(struct remote *remote, void *cb_data)
908908
static const char *unique_tracking_name(const char *name, struct object_id *oid)
909909
{
910910
struct tracking_name_data cb_data = { NULL, NULL, NULL, 1 };
911-
char src_ref[PATH_MAX];
912-
snprintf(src_ref, PATH_MAX, "refs/heads/%s", name);
913-
cb_data.src_ref = src_ref;
911+
cb_data.src_ref = xstrfmt("refs/heads/%s", name);
914912
cb_data.dst_oid = oid;
915913
for_each_remote(check_tracking_name, &cb_data);
914+
free(cb_data.src_ref);
916915
if (cb_data.unique)
917916
return cb_data.dst_ref;
918917
free(cb_data.dst_ref);

builtin/fetch.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ static int s_update_ref(const char *action,
421421
struct ref *ref,
422422
int check_old)
423423
{
424-
char msg[1024];
424+
char *msg;
425425
char *rla = getenv("GIT_REFLOG_ACTION");
426426
struct ref_transaction *transaction;
427427
struct strbuf err = STRBUF_INIT;
@@ -431,7 +431,7 @@ static int s_update_ref(const char *action,
431431
return 0;
432432
if (!rla)
433433
rla = default_rla.buf;
434-
snprintf(msg, sizeof(msg), "%s: %s", rla, action);
434+
msg = xstrfmt("%s: %s", rla, action);
435435

436436
transaction = ref_transaction_begin(&err);
437437
if (!transaction ||
@@ -449,11 +449,13 @@ static int s_update_ref(const char *action,
449449

450450
ref_transaction_free(transaction);
451451
strbuf_release(&err);
452+
free(msg);
452453
return 0;
453454
fail:
454455
ref_transaction_free(transaction);
455456
error("%s", err.buf);
456457
strbuf_release(&err);
458+
free(msg);
457459
return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
458460
: STORE_REF_ERROR_OTHER;
459461
}

builtin/gc.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ static int too_many_loose_objects(void)
135135
* distributed, we can check only one and get a reasonable
136136
* estimate.
137137
*/
138-
char path[PATH_MAX];
139-
const char *objdir = get_object_directory();
140138
DIR *dir;
141139
struct dirent *ent;
142140
int auto_threshold;
@@ -146,11 +144,7 @@ static int too_many_loose_objects(void)
146144
if (gc_auto_threshold <= 0)
147145
return 0;
148146

149-
if (sizeof(path) <= snprintf(path, sizeof(path), "%s/17", objdir)) {
150-
warning(_("insanely long object directory %.*s"), 50, objdir);
151-
return 0;
152-
}
153-
dir = opendir(path);
147+
dir = opendir(git_path("objects/17"));
154148
if (!dir)
155149
return 0;
156150

builtin/index-pack.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,14 +307,15 @@ static const char *open_pack_file(const char *pack_name)
307307
if (from_stdin) {
308308
input_fd = 0;
309309
if (!pack_name) {
310-
static char tmp_file[PATH_MAX];
311-
output_fd = odb_mkstemp(tmp_file, sizeof(tmp_file),
310+
struct strbuf tmp_file = STRBUF_INIT;
311+
output_fd = odb_mkstemp(&tmp_file,
312312
"pack/tmp_pack_XXXXXX");
313-
pack_name = xstrdup(tmp_file);
314-
} else
313+
pack_name = strbuf_detach(&tmp_file, NULL);
314+
} else {
315315
output_fd = open(pack_name, O_CREAT|O_EXCL|O_RDWR, 0600);
316-
if (output_fd < 0)
317-
die_errno(_("unable to create '%s'"), pack_name);
316+
if (output_fd < 0)
317+
die_errno(_("unable to create '%s'"), pack_name);
318+
}
318319
nothread_data.pack_fd = output_fd;
319320
} else {
320321
input_fd = open(pack_name, O_RDONLY);
@@ -1442,10 +1443,11 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14421443
if (!from_stdin) {
14431444
printf("%s\n", sha1_to_hex(sha1));
14441445
} else {
1445-
char buf[48];
1446-
int len = snprintf(buf, sizeof(buf), "%s\t%s\n",
1447-
report, sha1_to_hex(sha1));
1448-
write_or_die(1, buf, len);
1446+
struct strbuf buf = STRBUF_INIT;
1447+
1448+
strbuf_addf(&buf, "%s\t%s\n", report, sha1_to_hex(sha1));
1449+
write_or_die(1, buf.buf, buf.len);
1450+
strbuf_release(&buf);
14491451

14501452
/*
14511453
* Let's just mimic git-unpack-objects here and write

builtin/ls-remote.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ static const char * const ls_remote_usage[] = {
1717
static int tail_match(const char **pattern, const char *path)
1818
{
1919
const char *p;
20-
char pathbuf[PATH_MAX];
20+
char *pathbuf;
2121

2222
if (!pattern)
2323
return 1; /* no restriction */
2424

25-
if (snprintf(pathbuf, sizeof(pathbuf), "/%s", path) > sizeof(pathbuf))
26-
return error("insanely long ref %.*s...", 20, path);
25+
pathbuf = xstrfmt("/%s", path);
2726
while ((p = *(pattern++)) != NULL) {
28-
if (!wildmatch(p, pathbuf, 0, NULL))
27+
if (!wildmatch(p, pathbuf, 0, NULL)) {
28+
free(pathbuf);
2929
return 1;
30+
}
3031
}
32+
free(pathbuf);
3133
return 0;
3234
}
3335

builtin/name-rev.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,9 @@ static const char *get_exact_ref_match(const struct object *o)
238238
return NULL;
239239
}
240240

241-
/* returns a static buffer */
242-
static const char *get_rev_name(const struct object *o)
241+
/* may return a constant string or use "buf" as scratch space */
242+
static const char *get_rev_name(const struct object *o, struct strbuf *buf)
243243
{
244-
static char buffer[1024];
245244
struct rev_name *n;
246245
struct commit *c;
247246

@@ -258,10 +257,9 @@ static const char *get_rev_name(const struct object *o)
258257
int len = strlen(n->tip_name);
259258
if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
260259
len -= 2;
261-
snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
262-
n->generation);
263-
264-
return buffer;
260+
strbuf_reset(buf);
261+
strbuf_addf(buf, "%.*s~%d", len, n->tip_name, n->generation);
262+
return buf->buf;
265263
}
266264
}
267265

@@ -271,10 +269,11 @@ static void show_name(const struct object *obj,
271269
{
272270
const char *name;
273271
const struct object_id *oid = &obj->oid;
272+
struct strbuf buf = STRBUF_INIT;
274273

275274
if (!name_only)
276275
printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
277-
name = get_rev_name(obj);
276+
name = get_rev_name(obj, &buf);
278277
if (name)
279278
printf("%s\n", name);
280279
else if (allow_undefined)
@@ -283,6 +282,7 @@ static void show_name(const struct object *obj,
283282
printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
284283
else
285284
die("cannot describe '%s'", oid_to_hex(oid));
285+
strbuf_release(&buf);
286286
}
287287

288288
static char const * const name_rev_usage[] = {
@@ -294,6 +294,7 @@ static char const * const name_rev_usage[] = {
294294

295295
static void name_rev_line(char *p, struct name_ref_data *data)
296296
{
297+
struct strbuf buf = STRBUF_INIT;
297298
int forty = 0;
298299
char *p_start;
299300
for (p_start = p; *p; p++) {
@@ -314,7 +315,7 @@ static void name_rev_line(char *p, struct name_ref_data *data)
314315
struct object *o =
315316
lookup_object(sha1);
316317
if (o)
317-
name = get_rev_name(o);
318+
name = get_rev_name(o, &buf);
318319
}
319320
*(p+1) = c;
320321

@@ -332,6 +333,8 @@ static void name_rev_line(char *p, struct name_ref_data *data)
332333
/* flush */
333334
if (p_start != p)
334335
fwrite(p_start, p - p_start, 1, stdout);
336+
337+
strbuf_release(&buf);
335338
}
336339

337340
int cmd_name_rev(int argc, const char **argv, const char *prefix)

builtin/notes.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
554554
struct notes_tree *t;
555555
unsigned char object[20], new_note[20];
556556
const unsigned char *note;
557-
char logmsg[100];
557+
char *logmsg;
558558
const char * const *usage;
559559
struct note_data d = { 0, 0, NULL, STRBUF_INIT };
560560
struct option options[] = {
@@ -618,17 +618,16 @@ static int append_edit(int argc, const char **argv, const char *prefix)
618618
write_note_data(&d, new_note);
619619
if (add_note(t, object, new_note, combine_notes_overwrite))
620620
die("BUG: combine_notes_overwrite failed");
621-
snprintf(logmsg, sizeof(logmsg), "Notes added by 'git notes %s'",
622-
argv[0]);
621+
logmsg = xstrfmt("Notes added by 'git notes %s'", argv[0]);
623622
} else {
624623
fprintf(stderr, _("Removing note for object %s\n"),
625624
sha1_to_hex(object));
626625
remove_note(t, object);
627-
snprintf(logmsg, sizeof(logmsg), "Notes removed by 'git notes %s'",
628-
argv[0]);
626+
logmsg = xstrfmt("Notes removed by 'git notes %s'", argv[0]);
629627
}
630628
commit_notes(t, logmsg);
631629

630+
free(logmsg);
632631
free_note_data(&d);
633632
free_notes(t);
634633
return 0;

builtin/receive-pack.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,12 +1634,17 @@ static const char *parse_pack_header(struct pack_header *hdr)
16341634

16351635
static const char *pack_lockfile;
16361636

1637+
static void push_header_arg(struct argv_array *args, struct pack_header *hdr)
1638+
{
1639+
argv_array_pushf(args, "--pack_header=%"PRIu32",%"PRIu32,
1640+
ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries));
1641+
}
1642+
16371643
static const char *unpack(int err_fd, struct shallow_info *si)
16381644
{
16391645
struct pack_header hdr;
16401646
const char *hdr_err;
16411647
int status;
1642-
char hdr_arg[38];
16431648
struct child_process child = CHILD_PROCESS_INIT;
16441649
int fsck_objects = (receive_fsck_objects >= 0
16451650
? receive_fsck_objects
@@ -1653,9 +1658,6 @@ static const char *unpack(int err_fd, struct shallow_info *si)
16531658
close(err_fd);
16541659
return hdr_err;
16551660
}
1656-
snprintf(hdr_arg, sizeof(hdr_arg),
1657-
"--pack_header=%"PRIu32",%"PRIu32,
1658-
ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
16591661

16601662
if (si->nr_ours || si->nr_theirs) {
16611663
alt_shallow_file = setup_temporary_shallow(si->shallow);
@@ -1679,7 +1681,8 @@ static const char *unpack(int err_fd, struct shallow_info *si)
16791681
tmp_objdir_add_as_alternate(tmp_objdir);
16801682

16811683
if (ntohl(hdr.hdr_entries) < unpack_limit) {
1682-
argv_array_pushl(&child.args, "unpack-objects", hdr_arg, NULL);
1684+
argv_array_push(&child.args, "unpack-objects");
1685+
push_header_arg(&child.args, &hdr);
16831686
if (quiet)
16841687
argv_array_push(&child.args, "-q");
16851688
if (fsck_objects)
@@ -1697,8 +1700,8 @@ static const char *unpack(int err_fd, struct shallow_info *si)
16971700
} else {
16981701
char hostname[256];
16991702

1700-
argv_array_pushl(&child.args, "index-pack",
1701-
"--stdin", hdr_arg, NULL);
1703+
argv_array_pushl(&child.args, "index-pack", "--stdin", NULL);
1704+
push_header_arg(&child.args, &hdr);
17021705

17031706
if (gethostname(hostname, sizeof(hostname)))
17041707
xsnprintf(hostname, sizeof(hostname), "localhost");

0 commit comments

Comments
 (0)