Skip to content

Commit 0cc4142

Browse files
peffgitster
authored andcommitted
http-push: replace strcat with xsnprintf
We account for these strcats in our initial allocation, but the code is confusing to follow and verify. Let's remember our original allocation length, and then xsnprintf can verify that we don't exceed it. Note that we can't just use xstrfmt here (which would be even cleaner) because the code tries to grow the buffer only when necessary. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 48bcc1c commit 0cc4142

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

http-push.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -786,21 +786,21 @@ xml_start_tag(void *userData, const char *name, const char **atts)
786786
{
787787
struct xml_ctx *ctx = (struct xml_ctx *)userData;
788788
const char *c = strchr(name, ':');
789-
int new_len;
789+
int old_namelen, new_len;
790790

791791
if (c == NULL)
792792
c = name;
793793
else
794794
c++;
795795

796-
new_len = strlen(ctx->name) + strlen(c) + 2;
796+
old_namelen = strlen(ctx->name);
797+
new_len = old_namelen + strlen(c) + 2;
797798

798799
if (new_len > ctx->len) {
799800
ctx->name = xrealloc(ctx->name, new_len);
800801
ctx->len = new_len;
801802
}
802-
strcat(ctx->name, ".");
803-
strcat(ctx->name, c);
803+
xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
804804

805805
free(ctx->cdata);
806806
ctx->cdata = NULL;

0 commit comments

Comments
 (0)