Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit bc6b8fc

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b66103c commit bc6b8fc

File tree

7 files changed

+33
-9
lines changed

7 files changed

+33
-9
lines changed

builtin/fast-export.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static const char *find_encoding(const char *begin, const char *end)
279279
static void handle_commit(struct commit *commit, struct rev_info *rev)
280280
{
281281
int saved_output_format = rev->diffopt.output_format;
282+
const char *commit_buffer;
282283
const char *author, *author_end, *committer, *committer_end;
283284
const char *encoding, *message;
284285
char *reencoded = NULL;
@@ -288,7 +289,8 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
288289
rev->diffopt.output_format = DIFF_FORMAT_CALLBACK;
289290

290291
parse_commit_or_die(commit);
291-
author = strstr(commit->buffer, "\nauthor ");
292+
commit_buffer = get_commit_buffer(commit);
293+
author = strstr(commit_buffer, "\nauthor ");
292294
if (!author)
293295
die ("Could not find author in commit %s",
294296
sha1_to_hex(commit->object.sha1));
@@ -335,6 +337,7 @@ static void handle_commit(struct commit *commit, struct rev_info *rev)
335337
? strlen(message) : 0),
336338
reencoded ? reencoded : message ? message : "");
337339
free(reencoded);
340+
unuse_commit_buffer(commit, commit_buffer);
338341

339342
for (i = 0, p = commit->parents; p; p = p->next) {
340343
int mark = get_object_mark(&p->item->object);

builtin/fmt-merge-msg.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,14 @@ static void add_branch_desc(struct strbuf *out, const char *name)
230230
static void record_person(int which, struct string_list *people,
231231
struct commit *commit)
232232
{
233+
const char *buffer;
233234
char *name_buf, *name, *name_end;
234235
struct string_list_item *elem;
235236
const char *field;
236237

237238
field = (which == 'a') ? "\nauthor " : "\ncommitter ";
238-
name = strstr(commit->buffer, field);
239+
buffer = get_commit_buffer(commit);
240+
name = strstr(buffer, field);
239241
if (!name)
240242
return;
241243
name += strlen(field);
@@ -247,6 +249,7 @@ static void record_person(int which, struct string_list *people,
247249
if (name_end < name)
248250
return;
249251
name_buf = xmemdupz(name, name_end - name + 1);
252+
unuse_commit_buffer(commit, buffer);
250253

251254
elem = string_list_lookup(people, name_buf);
252255
if (!elem) {

builtin/log.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,12 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
918918
log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
919919
&need_8bit_cte);
920920

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))
923924
need_8bit_cte = 1;
925+
unuse_commit_buffer(list[i], buf);
926+
}
924927

925928
if (!branch_name)
926929
branch_name = find_branch_name(rev);

fsck.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,10 @@ static int fsck_ident(const char **ident, struct object *obj, fsck_error error_f
276276
return 0;
277277
}
278278

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)
280281
{
281-
const char *buffer = commit->buffer, *tmp;
282+
const char *tmp;
282283
unsigned char tree_sha1[20], sha1[20];
283284
struct commit_graft *graft;
284285
int parents = 0;
@@ -336,6 +337,14 @@ static int fsck_commit(struct commit *commit, fsck_error error_func)
336337
return 0;
337338
}
338339

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+
339348
static int fsck_tag(struct tag *tag, fsck_error error_func)
340349
{
341350
struct object *tagged = tag->tagged;

merge-recursive.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
190190
printf(_("(bad commit)\n"));
191191
else {
192192
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);
194195
if (len)
195196
printf("%.*s\n", len, title);
197+
unuse_commit_buffer(commit, msg);
196198
}
197199
}
198200
}

notes-merge.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,8 @@ int notes_merge_commit(struct notes_merge_options *o,
672672
DIR *dir;
673673
struct dirent *e;
674674
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");
676677
int baselen;
677678

678679
strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
@@ -721,6 +722,7 @@ int notes_merge_commit(struct notes_merge_options *o,
721722

722723
create_notes_commit(partial_tree, partial_commit->parents,
723724
msg, strlen(msg), result_sha1);
725+
unuse_commit_buffer(partial_commit, buffer);
724726
if (o->verbosity >= 4)
725727
printf("Finalized notes merge commit: %s\n",
726728
sha1_to_hex(result_sha1));

sequencer.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,12 @@ static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
662662
int subject_len;
663663

664664
for (cur = todo_list; cur; cur = cur->next) {
665+
const char *commit_buffer = get_commit_buffer(cur->item);
665666
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);
667668
strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev,
668669
subject_len, subject);
670+
unuse_commit_buffer(cur->item, commit_buffer);
669671
}
670672
return 0;
671673
}

0 commit comments

Comments
 (0)