]> git.ipfire.org Git - thirdparty/git.git/commitdiff
trailer: use offsets for trailer_start/trailer_end
authorLinus Arver <linusa@google.com>
Fri, 20 Oct 2023 19:01:35 +0000 (19:01 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 20 Dec 2023 19:55:04 +0000 (11:55 -0800)
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>
sequencer.c
trailer.c
trailer.h

index d584cac8ed9307caad0f8a9ede98d1f57ceab874..8707a92204f1d668a007c56c1d47186bf8c95b6c 100644 (file)
@@ -345,7 +345,7 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
        if (ignore_footer)
                sb->buf[sb->len - ignore_footer] = saved_char;
 
-       if (info.trailer_start == info.trailer_end)
+       if (info.trailer_block_start == info.trailer_block_end)
                return 0;
 
        for (i = 0; i < info.trailer_nr; i++)
index ca426c44c56d980c71d09a87eb6b97aae89178d4..816f8b28a9e9aa7b841cc3b22dbe23ef11190081 100644 (file)
--- a/trailer.c
+++ b/trailer.c
@@ -858,7 +858,7 @@ static size_t find_end_of_log_message(const char *input, int no_divider)
  * Return the position of the first trailer line or len if there are no
  * trailers.
  */
-static size_t find_trailer_start(const char *buf, size_t len)
+static size_t find_trailer_block_start(const char *buf, size_t len)
 {
        const char *s;
        ssize_t end_of_title, l;
@@ -1074,7 +1074,6 @@ void process_trailers(const char *file,
        LIST_HEAD(head);
        struct strbuf sb = STRBUF_INIT;
        struct trailer_info info;
-       size_t trailer_end;
        FILE *outfile = stdout;
 
        ensure_configured();
@@ -1085,11 +1084,10 @@ void process_trailers(const char *file,
                outfile = create_in_place_tempfile(file);
 
        parse_trailers(&info, sb.buf, &head, opts);
-       trailer_end = info.trailer_end - sb.buf;
 
        /* Print the lines before the trailers */
        if (!opts->only_trailers)
-               fwrite(sb.buf, 1, info.trailer_start - sb.buf, outfile);
+               fwrite(sb.buf, 1, info.trailer_block_start, outfile);
 
        if (!opts->only_trailers && !info.blank_line_before_trailer)
                fprintf(outfile, "\n");
@@ -1111,7 +1109,7 @@ void process_trailers(const char *file,
 
        /* Print the lines after the trailers as is */
        if (!opts->only_trailers)
-               fwrite(sb.buf + trailer_end, 1, sb.len - trailer_end, outfile);
+               fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile);
 
        if (opts->in_place)
                if (rename_tempfile(&trailers_tempfile, file))
@@ -1123,7 +1121,7 @@ void process_trailers(const char *file,
 void trailer_info_get(struct trailer_info *info, const char *str,
                      const struct process_trailer_options *opts)
 {
-       int end_of_log_message, trailer_start;
+       size_t end_of_log_message = 0, trailer_block_start = 0;
        struct strbuf **trailer_lines, **ptr;
        char **trailer_strings = NULL;
        size_t nr = 0, alloc = 0;
@@ -1132,10 +1130,10 @@ void trailer_info_get(struct trailer_info *info, const char *str,
        ensure_configured();
 
        end_of_log_message = find_end_of_log_message(str, opts->no_divider);
-       trailer_start = find_trailer_start(str, end_of_log_message);
+       trailer_block_start = find_trailer_block_start(str, end_of_log_message);
 
-       trailer_lines = strbuf_split_buf(str + trailer_start,
-                                        end_of_log_message - trailer_start,
+       trailer_lines = strbuf_split_buf(str + trailer_block_start,
+                                        end_of_log_message - trailer_block_start,
                                         '\n',
                                         0);
        for (ptr = trailer_lines; *ptr; ptr++) {
@@ -1156,9 +1154,9 @@ void trailer_info_get(struct trailer_info *info, const char *str,
        strbuf_list_free(trailer_lines);
 
        info->blank_line_before_trailer = ends_with_blank_line(str,
-                                                              trailer_start);
-       info->trailer_start = str + trailer_start;
-       info->trailer_end = str + end_of_log_message;
+                                                              trailer_block_start);
+       info->trailer_block_start = trailer_block_start;
+       info->trailer_block_end = end_of_log_message;
        info->trailers = trailer_strings;
        info->trailer_nr = nr;
 }
@@ -1173,6 +1171,7 @@ void trailer_info_release(struct trailer_info *info)
 
 static void format_trailer_info(struct strbuf *out,
                                const struct trailer_info *info,
+                               const char *msg,
                                const struct process_trailer_options *opts)
 {
        size_t origlen = out->len;
@@ -1182,8 +1181,8 @@ static void format_trailer_info(struct strbuf *out,
        if (!opts->only_trailers && !opts->unfold && !opts->filter &&
            !opts->separator && !opts->key_only && !opts->value_only &&
            !opts->key_value_separator) {
-               strbuf_add(out, info->trailer_start,
-                          info->trailer_end - info->trailer_start);
+               strbuf_add(out, msg + info->trailer_block_start,
+                          info->trailer_block_end - info->trailer_block_start);
                return;
        }
 
@@ -1237,7 +1236,7 @@ void format_trailers_from_commit(struct strbuf *out, const char *msg,
        struct trailer_info info;
 
        trailer_info_get(&info, msg, opts);
-       format_trailer_info(out, &info, opts);
+       format_trailer_info(out, &info, msg, opts);
        trailer_info_release(&info);
 }
 
index ab2cd017567b3f782fa9b342ad03e7e93bc17a37..1644cd05f60d9f3ceb89ffa66fb5c5b3da9e5cd4 100644 (file)
--- a/trailer.h
+++ b/trailer.h
@@ -32,16 +32,16 @@ int trailer_set_if_missing(enum trailer_if_missing *item, const char *value);
 struct trailer_info {
        /*
         * True if there is a blank line before the location pointed to by
-        * trailer_start.
+        * trailer_block_start.
         */
        int blank_line_before_trailer;
 
        /*
-        * Pointers to the start and end of the trailer block found. If there
-        * is no trailer block found, these 2 pointers point to the end of the
-        * input string.
+        * Offsets to the trailer block start and end positions in the input
+        * string. If no trailer block is found, these are both set to the
+        * "true" end of the input (find_end_of_log_message()).
         */
-       const char *trailer_start, *trailer_end;
+       size_t trailer_block_start, trailer_block_end;
 
        /*
         * Array of trailers found.