Skip to content

Commit a08f23a

Browse files
MadCodergitster
authored andcommitted
Refactor replace_encoding_header.
* Be more clever in how we search for "encoding ...\n": parse for real instead of the sloppy strstr's. * use strbuf_splice to do the substring replacements. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c7f9cb1 commit a08f23a

File tree

1 file changed

+23
-36
lines changed

1 file changed

+23
-36
lines changed

commit.c

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -648,47 +648,34 @@ static char *get_header(const struct commit *commit, const char *key)
648648

649649
static char *replace_encoding_header(char *buf, const char *encoding)
650650
{
651-
char *encoding_header = strstr(buf, "\nencoding ");
652-
char *header_end = strstr(buf, "\n\n");
653-
char *end_of_encoding_header;
654-
int encoding_header_pos;
655-
int encoding_header_len;
656-
int new_len;
657-
int need_len;
658-
int buflen = strlen(buf) + 1;
659-
660-
if (!header_end)
661-
header_end = buf + buflen;
662-
if (!encoding_header || encoding_header >= header_end)
663-
return buf;
664-
encoding_header++;
665-
end_of_encoding_header = strchr(encoding_header, '\n');
666-
if (!end_of_encoding_header)
651+
struct strbuf tmp;
652+
size_t start, len;
653+
char *cp = buf;
654+
655+
/* guess if there is an encoding header before a \n\n */
656+
while (strncmp(cp, "encoding ", strlen("encoding "))) {
657+
cp = strchr(cp, '\n');
658+
if (!cp || *++cp == '\n')
659+
return buf;
660+
}
661+
start = cp - buf;
662+
cp = strchr(cp, '\n');
663+
if (!cp)
667664
return buf; /* should not happen but be defensive */
668-
end_of_encoding_header++;
669-
670-
encoding_header_len = end_of_encoding_header - encoding_header;
671-
encoding_header_pos = encoding_header - buf;
665+
len = cp + 1 - (buf + start);
672666

667+
strbuf_init(&tmp, 0);
668+
strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
673669
if (is_encoding_utf8(encoding)) {
674670
/* we have re-coded to UTF-8; drop the header */
675-
memmove(encoding_header, end_of_encoding_header,
676-
buflen - (encoding_header_pos + encoding_header_len));
677-
return buf;
678-
}
679-
new_len = strlen(encoding);
680-
need_len = new_len + strlen("encoding \n");
681-
if (encoding_header_len < need_len) {
682-
buf = xrealloc(buf, buflen + (need_len - encoding_header_len));
683-
encoding_header = buf + encoding_header_pos;
684-
end_of_encoding_header = encoding_header + encoding_header_len;
671+
strbuf_splice(&tmp, start, len, NULL, 0);
672+
} else {
673+
/* just replaces XXXX in 'encoding XXXX\n' */
674+
strbuf_splice(&tmp, start + strlen("encoding "),
675+
len - strlen("encoding \n"),
676+
encoding, strlen(encoding));
685677
}
686-
memmove(end_of_encoding_header + (need_len - encoding_header_len),
687-
end_of_encoding_header,
688-
buflen - (encoding_header_pos + encoding_header_len));
689-
memcpy(encoding_header + 9, encoding, strlen(encoding));
690-
encoding_header[9 + new_len] = '\n';
691-
return buf;
678+
return tmp.buf;
692679
}
693680

694681
static char *logmsg_reencode(const struct commit *commit,

0 commit comments

Comments
 (0)