Skip to content

Commit de7c27a

Browse files
Linus Arvergitster
Linus Arver
authored andcommitted
trailer: use offsets for trailer_start/trailer_end
Previously these fields in the trailer_info struct were of type "const char *" and pointed to positions in the input string directly (to the start and end positions of the trailer block). Use offsets to make the intended usage less ambiguous. We only need to reference the input string in format_trailer_info(), so update that function to take a pointer to the input. While we're at it, rename trailer_start to trailer_block_start to be more explicit about these offsets (that they are for the entire trailer block including other trailers). Ditto for trailer_end. Reported-by: Glen Choo <glencbz@gmail.com> Signed-off-by: Linus Arver <linusa@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 97e9d0b commit de7c27a

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

sequencer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
345345
if (ignore_footer)
346346
sb->buf[sb->len - ignore_footer] = saved_char;
347347

348-
if (info.trailer_start == info.trailer_end)
348+
if (info.trailer_block_start == info.trailer_block_end)
349349
return 0;
350350

351351
for (i = 0; i < info.trailer_nr; i++)

trailer.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ static size_t find_end_of_log_message(const char *input, int no_divider)
858858
* Return the position of the first trailer line or len if there are no
859859
* trailers.
860860
*/
861-
static size_t find_trailer_start(const char *buf, size_t len)
861+
static size_t find_trailer_block_start(const char *buf, size_t len)
862862
{
863863
const char *s;
864864
ssize_t end_of_title, l;
@@ -1074,7 +1074,6 @@ void process_trailers(const char *file,
10741074
LIST_HEAD(head);
10751075
struct strbuf sb = STRBUF_INIT;
10761076
struct trailer_info info;
1077-
size_t trailer_end;
10781077
FILE *outfile = stdout;
10791078

10801079
ensure_configured();
@@ -1085,11 +1084,10 @@ void process_trailers(const char *file,
10851084
outfile = create_in_place_tempfile(file);
10861085

10871086
parse_trailers(&info, sb.buf, &head, opts);
1088-
trailer_end = info.trailer_end - sb.buf;
10891087

10901088
/* Print the lines before the trailers */
10911089
if (!opts->only_trailers)
1092-
fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);
1090+
fwrite(sb.buf, 1, info.trailer_block_start, outfile);
10931091

10941092
if (!opts->only_trailers && !info.blank_line_before_trailer)
10951093
fprintf(outfile, "\n");
@@ -1111,7 +1109,7 @@ void process_trailers(const char *file,
11111109

11121110
/* Print the lines after the trailers as is */
11131111
if (!opts->only_trailers)
1114-
fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
1112+
fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile);
11151113

11161114
if (opts->in_place)
11171115
if (rename_tempfile(&trailers_tempfile, file))
@@ -1123,7 +1121,7 @@ void process_trailers(const char *file,
11231121
void trailer_info_get(struct trailer_info *info, const char *str,
11241122
const struct process_trailer_options *opts)
11251123
{
1126-
int end_of_log_message, trailer_start;
1124+
size_t end_of_log_message = 0, trailer_block_start = 0;
11271125
struct strbuf **trailer_lines, **ptr;
11281126
char **trailer_strings = NULL;
11291127
size_t nr = 0, alloc = 0;
@@ -1132,10 +1130,10 @@ void trailer_info_get(struct trailer_info *info, const char *str,
11321130
ensure_configured();
11331131

11341132
end_of_log_message = find_end_of_log_message(str, opts->no_divider);
1135-
trailer_start = find_trailer_start(str, end_of_log_message);
1133+
trailer_block_start = find_trailer_block_start(str, end_of_log_message);
11361134

1137-
trailer_lines = strbuf_split_buf(str + trailer_start,
1138-
end_of_log_message - trailer_start,
1135+
trailer_lines = strbuf_split_buf(str + trailer_block_start,
1136+
end_of_log_message - trailer_block_start,
11391137
'\n',
11401138
0);
11411139
for (ptr = trailer_lines; *ptr; ptr++) {
@@ -1156,9 +1154,9 @@ void trailer_info_get(struct trailer_info *info, const char *str,
11561154
strbuf_list_free(trailer_lines);
11571155

11581156
info->blank_line_before_trailer = ends_with_blank_line(str,
1159-
trailer_start);
1160-
info->trailer_start = str + trailer_start;
1161-
info->trailer_end = str + end_of_log_message;
1157+
trailer_block_start);
1158+
info->trailer_block_start = trailer_block_start;
1159+
info->trailer_block_end = end_of_log_message;
11621160
info->trailers = trailer_strings;
11631161
info->trailer_nr = nr;
11641162
}
@@ -1173,6 +1171,7 @@ void trailer_info_release(struct trailer_info *info)
11731171

11741172
static void format_trailer_info(struct strbuf *out,
11751173
const struct trailer_info *info,
1174+
const char *msg,
11761175
const struct process_trailer_options *opts)
11771176
{
11781177
size_t origlen = out->len;
@@ -1182,8 +1181,8 @@ static void format_trailer_info(struct strbuf *out,
11821181
if (!opts->only_trailers && !opts->unfold && !opts->filter &&
11831182
!opts->separator && !opts->key_only && !opts->value_only &&
11841183
!opts->key_value_separator) {
1185-
strbuf_add(out, info->trailer_start,
1186-
info->trailer_end - info->trailer_start);
1184+
strbuf_add(out, msg + info->trailer_block_start,
1185+
info->trailer_block_end - info->trailer_block_start);
11871186
return;
11881187
}
11891188

@@ -1237,7 +1236,7 @@ void format_trailers_from_commit(struct strbuf *out, const char *msg,
12371236
struct trailer_info info;
12381237

12391238
trailer_info_get(&info, msg, opts);
1240-
format_trailer_info(out, &info, opts);
1239+
format_trailer_info(out, &info, msg, opts);
12411240
trailer_info_release(&info);
12421241
}
12431242

trailer.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
3232
struct trailer_info {
3333
/*
3434
* True if there is a blank line before the location pointed to by
35-
* trailer_start.
35+
* trailer_block_start.
3636
*/
3737
int blank_line_before_trailer;
3838

3939
/*
40-
* Pointers to the start and end of the trailer block found. If there
41-
* is no trailer block found, these 2 pointers point to the end of the
42-
* input string.
40+
* Offsets to the trailer block start and end positions in the input
41+
* string. If no trailer block is found, these are both set to the
42+
* "true" end of the input (find_end_of_log_message()).
4343
*/
44-
const char *trailer_start, *trailer_end;
44+
size_t trailer_block_start, trailer_block_end;
4545

4646
/*
4747
* Array of trailers found.

0 commit comments

Comments
 (0)