use get_commit_buffer everywhere
Each of these sites assumes that commit->buffer is valid. Since they would segfault if this was not the case, they are likely to be correct in practice. However, we can future-proof them by using get_commit_buffer. And as a side effect, we abstract away the final bare uses of commit->buffer. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jun 10, 2014 at 17:41 UTC
bc6b8fc1300ef79c4b4c3c2a79bb3c1e2e032963
7 files changed
+33
-9
builtin/fast-export.c
+4
-1
@@ -279,6 +279,7 @@ static const char *find_encoding(const char *begin, const char *end)
279
static void handle_commit(struct commit *commit, struct rev_info *rev)
280
{
281
int saved_output_format = rev->diffopt.output_format;
282
+ const char *commit_buffer;
283
const char *author, *author_end, *committer, *committer_end;
284
const char *encoding, *message;
285
char *reencoded = NULL;
@@ -288,7 +289,8 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
289
rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
290
291
parse_commit_or_die(commit);
291
- author = strstr(commit->buffer, "\nauthor ");
292
+ commit_buffer = get_commit_buffer(commit);
293
+ author = strstr(commit_buffer, "\nauthor ");
294
if (!author)
295
die ("Could not find author in commit %s",
296
sha1_to_hex(commit->object.sha1));
@@ -335,6 +337,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
337
? strlen(message) : 0),
338
reencoded ? reencoded : message ? message : "");
339
free(reencoded);
340
+ unuse_commit_buffer(commit, commit_buffer);
341
342
for (i = 0, p = commit->parents; p; p = p->next) {
343
int mark = get_object_mark(&p->item->object);
builtin/fmt-merge-msg.c
+4
-1
@@ -230,12 +230,14 @@ static void add_branch_desc(struct strbuf *out, const char *name)
230
static void record_person(int which, struct string_list *people,
231
struct commit *commit)
232
{
233
+ const char *buffer;
234
char *name_buf, *name, *name_end;
235
struct string_list_item *elem;
236
const char *field;
237
238
field = (which == 'a') ? "\nauthor " : "\ncommitter ";
238
- name = strstr(commit->buffer, field);
239
+ buffer = get_commit_buffer(commit);
240
+ name = strstr(buffer, field);
241
if (!name)
242
return;
243
name += strlen(field);
@@ -247,6 +249,7 @@ static void record_person(int which, struct string_list *people,
249
if (name_end < name)
250
return;
251
name_buf = xmemdupz(name, name_end - name + 1);
252
+ unuse_commit_buffer(commit, buffer);
253
254
elem = string_list_lookup(people, name_buf);
255
if (!elem) {
builtin/log.c
+5
-2
@@ -918,9 +918,12 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
918
log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
919
&need_8bit_cte);
920
921
- for (i = 0; !need_8bit_cte && i < nr; i++)
922
- if (has_non_ascii(list[i]->buffer))
921
+ for (i = 0; !need_8bit_cte && i < nr; i++) {
922
+ const char *buf = get_commit_buffer(list[i]);
923
+ if (has_non_ascii(buf))
924
need_8bit_cte = 1;
925
+ unuse_commit_buffer(list[i], buf);
926
+ }
927
928
if (!branch_name)
929
branch_name = find_branch_name(rev);
fsck.c
+11
-2
@@ -276,9 +276,10 @@ static int fsck_ident(const char **ident, struct object *obj, fsck_error error_f
276
return 0;
277
}
278
279
-static int fsck_commit(struct commit *commit, fsck_error error_func)
279
+static int fsck_commit_buffer(struct commit *commit, const char *buffer,
280
+ fsck_error error_func)
281
{
281
- const char *buffer = commit->buffer, *tmp;
282
+ const char *tmp;
283
unsigned char tree_sha1[20], sha1[20];
284
struct commit_graft *graft;
285
int parents = 0;
@@ -336,6 +337,14 @@ static int fsck_commit(struct commit *commit, fsck_error error_func)
337
return 0;
338
}
339
340
+static int fsck_commit(struct commit *commit, fsck_error error_func)
341
+{
342
+ const char *buffer = get_commit_buffer(commit);
343
+ int ret = fsck_commit_buffer(commit, buffer, error_func);
344
+ unuse_commit_buffer(commit, buffer);
345
+ return ret;
346
+}
347
+
348
static int fsck_tag(struct tag *tag, fsck_error error_func)
349
{
350
struct object *tagged = tag->tagged;
merge-recursive.c
+3
-1
@@ -190,9 +190,11 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
190
printf(_("(bad commit)\n"));
191
else {
192
const char *title;
193
- int len = find_commit_subject(commit->buffer, &title);
193
+ const char *msg = get_commit_buffer(commit);
194
+ int len = find_commit_subject(msg, &title);
195
if (len)
196
printf("%.*s\n", len, title);
197
+ unuse_commit_buffer(commit, msg);
198
}
199
}
200
}
notes-merge.c
+3
-1
@@ -672,7 +672,8 @@ int notes_merge_commit(struct notes_merge_options *o,
672
DIR *dir;
673
struct dirent *e;
674
struct strbuf path = STRBUF_INIT;
675
- char *msg = strstr(partial_commit->buffer, "\n\n");
675
+ const char *buffer = get_commit_buffer(partial_commit);
676
+ const char *msg = strstr(buffer, "\n\n");
677
int baselen;
678
679
strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
@@ -721,6 +722,7 @@ int notes_merge_commit(struct notes_merge_options *o,
722
723
create_notes_commit(partial_tree, partial_commit->parents,
724
msg, strlen(msg), result_sha1);
725
+ unuse_commit_buffer(partial_commit, buffer);
726
if (o->verbosity >= 4)
727
printf("Finalized notes merge commit: %s\n",
728
sha1_to_hex(result_sha1));
sequencer.c
+3
-1
@@ -662,10 +662,12 @@ static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
662
int subject_len;
663
664
for (cur = todo_list; cur; cur = cur->next) {
665
+ const char *commit_buffer = get_commit_buffer(cur->item);
666
sha1_abbrev = find_unique_abbrev(cur->item->object.sha1, DEFAULT_ABBREV);
666
- subject_len = find_commit_subject(cur->item->buffer, &subject);
667
+ subject_len = find_commit_subject(commit_buffer, &subject);
668
strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev,
669
subject_len, subject);
670
+ unuse_commit_buffer(cur->item, commit_buffer);
671
}
672
return 0;
673
}