append_signoff: use size_t for string offsets

The append_signoff() function takes an "int" to specify the number of bytes to ignore. Most callers just pass 0, and the remainder use ignore_non_trailer() to skip over cruft. That function also returns an int, and uses them internally. On systems where size_t is larger than an int (i.e., most 64-bit systems), dealing with a ridiculously large commit message could end up overflowing an int, producing surprising results (e.g., returning a negative offset, which would cause us to look outside the original string). Let's consistently use size_t for these offsets through this whole stack. As a bonus, this makes the meaning of "ignore_footer" as an offset (and not a boolean) more clear. But while we're here, let's also document the interface. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 22, 2018 at 20:50 UTC 66e83d9b41f7438cb167b9bb54093ebbf0532437
4 files changed +14 -7
commit.c
+3 -3
@@ -1687,10 +1687,10 @@ const char *find_commit_header(const char *msg, const char *key, size_t *out_len
1687 * Returns the number of bytes from the tail to ignore, to be fed as
1688 * the second parameter to append_signoff().
1689 */
1690 -int ignore_non_trailer(const char *buf, size_t len)
1690 +size_t ignore_non_trailer(const char *buf, size_t len)
1691 {
1692 - int boc = 0;
1693 - int bol = 0;
1692 + size_t boc = 0;
1693 + size_t bol = 0;
1694 int in_old_conflicts_block = 0;
1695 size_t cutoff = wt_status_locate_end(buf, len);
1696
commit.h
+1 -1
@@ -301,7 +301,7 @@ extern const char *find_commit_header(const char *msg, const char *key,
301 size_t *out_len);
302
303 /* Find the end of the log message, the right place for a new trailer. */
304 -extern int ignore_non_trailer(const char *buf, size_t len);
304 +extern size_t ignore_non_trailer(const char *buf, size_t len);
305
306 typedef int (*each_mergetag_fn)(struct commit *commit, struct commit_extra_header *extra,
307 void *cb_data);
sequencer.c
+2 -2
@@ -222,7 +222,7 @@ static const char *get_todo_path(const struct replay_opts *opts)
222 * Returns 3 when sob exists within conforming footer as last entry
223 */
224 static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
225 - int ignore_footer)
225 + size_t ignore_footer)
226 {
227 struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT;
228 struct trailer_info info;
@@ -3660,7 +3660,7 @@ int sequencer_pick_revisions(struct replay_opts *opts)
3660 return res;
3661 }
3662
3663 -void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
3663 +void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag)
3664 {
3665 unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP;
3666 struct strbuf sob = STRBUF_INIT;
sequencer.h
+8 -1
@@ -85,7 +85,14 @@ int rearrange_squash(void);
85
86 extern const char sign_off_header[];
87
88 -void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag);
88 +/*
89 + * Append a signoff to the commit message in "msgbuf". The ignore_footer
90 + * parameter specifies the number of bytes at the end of msgbuf that should
91 + * not be considered at all. I.e., they are not checked for existing trailers,
92 + * and the new signoff will be spliced into the buffer before those bytes.
93 + */
94 +void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag);
95 +
96 void append_conflicts_hint(struct strbuf *msgbuf);
97 int message_is_empty(const struct strbuf *sb,
98 enum commit_msg_cleanup_mode cleanup_mode);