receive-pack: use find_commit_header() in check_cert_push_options()

Use the public function find_commit_header() instead of find_header() to simplify the code. This is possible and safe because we're operating on a strbuf, which is always NUL-terminated, so there is no risk of running over the end of the buffer. It cannot contain NUL within the buffer, as it is built using strbuf_addstr(), only. The string comparison becomes more complicated because we need to check for NUL explicitly after comparing the length-limited option, but on the flip side we don't need to clean up allocations or track the remaining buffer length. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Feb 9, 2024 at 21:36 UTC f51d790b67255dd40b55654527fc4f59a53f44b0
1 file changed +9 -15
builtin/receive-pack.c
+9 -15
@@ -718,35 +718,29 @@ leave:
718 static int check_cert_push_options(const struct string_list *push_options)
719 {
720 const char *buf = push_cert.buf;
721 - int len = push_cert.len;
721
723 - char *option;
724 - const char *next_line;
722 + const char *option;
723 + size_t optionlen;
724 int options_seen = 0;
725
726 int retval = 1;
727
729 - if (!len)
728 + if (!*buf)
729 return 1;
730
732 - while ((option = find_header(buf, len, "push-option", &next_line))) {
733 - len -= (next_line - buf);
734 - buf = next_line;
731 + while ((option = find_commit_header(buf, "push-option", &optionlen))) {
732 + buf = option + optionlen + 1;
733 options_seen++;
734 if (options_seen > push_options->nr
737 - || strcmp(option,
738 - push_options->items[options_seen - 1].string)) {
739 - retval = 0;
740 - goto leave;
741 - }
742 - free(option);
735 + || strncmp(push_options->items[options_seen - 1].string,
736 + option, optionlen)
737 + || push_options->items[options_seen - 1].string[optionlen])
738 + return 0;
739 }
740
741 if (options_seen != push_options->nr)
742 retval = 0;
743
748 -leave:
749 - free(option);
744 return retval;
745 }
746