Skip to content

Commit d50b69b

Browse files
peffgitster
authored andcommitted
log_write_email_headers: use strbufs
When we write a MIME attachment, we write the mime headers into fixed-size buffers. These are likely to be big enough in practice, but technically the input could be arbitrarily large (e.g., if the caller provided a lot of content in the extra_headers string), in which case we'd quietly truncate it and generate bogus output. Let's convert these buffers to strbufs. The memory ownership here is a bit funny. The original fixed buffers were static, and we merely pass out pointers to them to be used by the caller (and in one case, we even just stuff our value into the opt->diffopt.stat_sep value). Ideally we'd actually pass back heap buffers, and the caller would be responsible for freeing them. This patch punts on that cleanup for now, and instead just marks the strbufs as static. That means we keep ownership in this function, making it not a complete leak. This also takes us one step closer to fixing it in the long term (since we can eventually use strbuf_detach() to hand ownership to the caller, once it's ready). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 390c6cb commit d50b69b

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

log-tree.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,15 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
386386
graph_show_oneline(opt->graph);
387387
}
388388
if (opt->mime_boundary) {
389-
static char subject_buffer[1024];
390-
static char buffer[1024];
389+
static struct strbuf subject_buffer = STRBUF_INIT;
390+
static struct strbuf buffer = STRBUF_INIT;
391391
struct strbuf filename = STRBUF_INIT;
392392
*need_8bit_cte_p = -1; /* NEVER */
393-
snprintf(subject_buffer, sizeof(subject_buffer) - 1,
393+
394+
strbuf_reset(&subject_buffer);
395+
strbuf_reset(&buffer);
396+
397+
strbuf_addf(&subject_buffer,
394398
"%s"
395399
"MIME-Version: 1.0\n"
396400
"Content-Type: multipart/mixed;"
@@ -405,13 +409,13 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
405409
extra_headers ? extra_headers : "",
406410
mime_boundary_leader, opt->mime_boundary,
407411
mime_boundary_leader, opt->mime_boundary);
408-
extra_headers = subject_buffer;
412+
extra_headers = subject_buffer.buf;
409413

410414
if (opt->numbered_files)
411415
strbuf_addf(&filename, "%d", opt->nr);
412416
else
413417
fmt_output_commit(&filename, commit, opt);
414-
snprintf(buffer, sizeof(buffer) - 1,
418+
strbuf_addf(&buffer,
415419
"\n--%s%s\n"
416420
"Content-Type: text/x-patch;"
417421
" name=\"%s\"\n"
@@ -422,7 +426,7 @@ void log_write_email_headers(struct rev_info *opt, struct commit *commit,
422426
filename.buf,
423427
opt->no_inline ? "attachment" : "inline",
424428
filename.buf);
425-
opt->diffopt.stat_sep = buffer;
429+
opt->diffopt.stat_sep = buffer.buf;
426430
strbuf_release(&filename);
427431
}
428432
*extra_headers_p = extra_headers;

0 commit comments

Comments
 (0)