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>
Linus Arver committed
Oct 20, 2023 at 19:01 UTC
de7c27a1869953158436e60542ea556d78c3f4c2
3 files changed
+20
-21
sequencer.c
+1
-1
@@ -345,7 +345,7 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
345
if (ignore_footer)
346
sb->buf[sb->len - ignore_footer] = saved_char;
347
348
- if (info.trailer_start == info.trailer_end)
348
+ if (info.trailer_block_start == info.trailer_block_end)
349
return 0;
350
351
for (i = 0; i < info.trailer_nr; i++)
trailer.c
+14
-15
@@ -858,7 +858,7 @@ static size_t find_end_of_log_message(const char *input, int no_divider)
858
* Return the position of the first trailer line or len if there are no
859
* trailers.
860
*/
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)
862
{
863
const char *s;
864
ssize_t end_of_title, l;
@@ -1074,7 +1074,6 @@ void process_trailers(const char *file,
1074
LIST_HEAD(head);
1075
struct strbuf sb = STRBUF_INIT;
1076
struct trailer_info info;
1077
- size_t trailer_end;
1077
FILE *outfile = stdout;
1078
1079
ensure_configured();
@@ -1085,11 +1084,10 @@ void process_trailers(const char *file,
1084
outfile = create_in_place_tempfile(file);
1085
1086
parse_trailers(&info, sb.buf, &head, opts);
1088
- trailer_end = info.trailer_end - sb.buf;
1087
1088
/* Print the lines before the trailers */
1089
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);
1091
1092
if (!opts->only_trailers && !info.blank_line_before_trailer)
1093
fprintf(outfile, "\n");
@@ -1111,7 +1109,7 @@ void process_trailers(const char *file,
1109
1110
/* Print the lines after the trailers as is */
1111
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);
1113
1114
if (opts->in_place)
1115
if (rename_tempfile(&trailers_tempfile, file))
@@ -1123,7 +1121,7 @@ void process_trailers(const char *file,
1121
void trailer_info_get(struct trailer_info *info, const char *str,
1122
const struct process_trailer_options *opts)
1123
{
1126
- int end_of_log_message, trailer_start;
1124
+ size_t end_of_log_message = 0, trailer_block_start = 0;
1125
struct strbuf **trailer_lines, **ptr;
1126
char **trailer_strings = NULL;
1127
size_t nr = 0, alloc = 0;
@@ -1132,10 +1130,10 @@ void trailer_info_get(struct trailer_info *info, const char *str,
1130
ensure_configured();
1131
1132
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);
1134
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,
1137
'\n',
1138
0);
1139
for (ptr = trailer_lines; *ptr; ptr++) {
@@ -1156,9 +1154,9 @@ void trailer_info_get(struct trailer_info *info, const char *str,
1154
strbuf_list_free(trailer_lines);
1155
1156
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;
1160
info->trailers = trailer_strings;
1161
info->trailer_nr = nr;
1162
}
@@ -1173,6 +1171,7 @@ void trailer_info_release(struct trailer_info *info)
1171
1172
static void format_trailer_info(struct strbuf *out,
1173
const struct trailer_info *info,
1174
+ const char *msg,
1175
const struct process_trailer_options *opts)
1176
{
1177
size_t origlen = out->len;
@@ -1182,8 +1181,8 @@ static void format_trailer_info(struct strbuf *out,
1181
if (!opts->only_trailers && !opts->unfold && !opts->filter &&
1182
!opts->separator && !opts->key_only && !opts->value_only &&
1183
!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);
1186
return;
1187
}
1188
@@ -1237,7 +1236,7 @@ void format_trailers_from_commit(struct strbuf *out, const char *msg,
1236
struct trailer_info info;
1237
1238
trailer_info_get(&info, msg, opts);
1240
- format_trailer_info(out, &info, opts);
1239
+ format_trailer_info(out, &info, msg, opts);
1240
trailer_info_release(&info);
1241
}
1242
trailer.h
+5
-5
@@ -32,16 +32,16 @@ int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
32
struct trailer_info {
33
/*
34
* True if there is a blank line before the location pointed to by
35
- * trailer_start.
35
+ * trailer_block_start.
36
*/
37
int blank_line_before_trailer;
38
39
/*
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()).
43
*/
44
- const char *trailer_start, *trailer_end;
44
+ size_t trailer_block_start, trailer_block_end;
45
46
/*
47
* Array of trailers found.