Skip to content

Commit

Permalink
http-push: replace strcat with xsnprintf
Browse files Browse the repository at this point in the history
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Jeff King authored and Junio C Hamano committed Sep 25, 2015
1 parent 48bcc1c commit 0cc4142
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions http-push.c
Original file line number Diff line number Diff line change
Expand Up @@ -786,21 +786,21 @@ xml_start_tag(void *userData, const char *name, const char **atts)
{
struct xml_ctx *ctx = (struct xml_ctx *)userData;
const char *c = strchr(name, ':');
int new_len;
int old_namelen, new_len;

if (c == NULL)
c = name;
else
c++;

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

if (new_len > ctx->len) {
ctx->name = xrealloc(ctx->name, new_len);
ctx->len = new_len;
}
strcat(ctx->name, ".");
strcat(ctx->name, c);
xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);

free(ctx->cdata);
ctx->cdata = NULL;
Expand Down

0 comments on commit 0cc4142

Please sign in to comment.