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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 24, 2015 at 17:06 UTC
0cc41428596ec1cd3862918ef781793ef7346ba5
1 file changed
+4
-4
http-push.c
+4
-4
@@ -786,21 +786,21 @@ xml_start_tag(void *userData, const char *name, const char **atts)
786
{
787
struct xml_ctx *ctx = (struct xml_ctx *)userData;
788
const char *c = strchr(name, ':');
789
- int new_len;
789
+ int old_namelen, new_len;
790
791
if (c == NULL)
792
c = name;
793
else
794
c++;
795
796
- new_len = strlen(ctx->name) + strlen(c) + 2;
796
+ old_namelen = strlen(ctx->name);
797
+ new_len = old_namelen + strlen(c) + 2;
798
799
if (new_len > ctx->len) {
800
ctx->name = xrealloc(ctx->name, new_len);
801
ctx->len = new_len;
802
}
802
- strcat(ctx->name, ".");
803
- strcat(ctx->name, c);
803
+ xsnprintf(ctx->name + old_namelen, ctx->len - old_namelen, ".%s", c);
804
805
free(ctx->cdata);
806
ctx->cdata = NULL;