use xstrdup instead of xmalloc + strcpy
This is one line shorter, and makes sure the length in the malloc and copy steps match. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jun 19, 2014 at 17:19 UTC
95244ae3dd49b3eed4dbfe09299b9d8847622218
3 files changed
+4
-10
builtin/receive-pack.c
+1
-4
@@ -614,12 +614,9 @@ static void run_update_post_hook(struct command *commands)
614
argv[0] = hook;
615
616
for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
617
- char *p;
617
if (cmd->error_string || cmd->did_not_exist)
618
continue;
620
- p = xmalloc(strlen(cmd->ref_name) + 1);
621
- strcpy(p, cmd->ref_name);
622
- argv[argc] = p;
619
+ argv[argc] = xstrdup(cmd->ref_name);
620
argc++;
621
}
622
argv[argc] = NULL;
http-push.c
+2
-4
@@ -767,15 +767,13 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
767
768
if (tag_closed && ctx->cdata) {
769
if (!strcmp(ctx->name, DAV_ACTIVELOCK_OWNER)) {
770
- lock->owner = xmalloc(strlen(ctx->cdata) + 1);
771
- strcpy(lock->owner, ctx->cdata);
770
+ lock->owner = xstrdup(ctx->cdata);
771
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
772
if (starts_with(ctx->cdata, "Second-"))
773
lock->timeout =
774
strtol(ctx->cdata + 7, NULL, 10);
775
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TOKEN)) {
777
- lock->token = xmalloc(strlen(ctx->cdata) + 1);
778
- strcpy(lock->token, ctx->cdata);
776
+ lock->token = xstrdup(ctx->cdata);
777
778
git_SHA1_Init(&sha_ctx);
779
git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));
http-walker.c
+1
-2
@@ -566,8 +566,7 @@ struct walker *get_http_walker(const char *url)
566
struct walker *walker = xmalloc(sizeof(struct walker));
567
568
data->alt = xmalloc(sizeof(*data->alt));
569
- data->alt->base = xmalloc(strlen(url) + 1);
570
- strcpy(data->alt->base, url);
569
+ data->alt->base = xstrdup(url);
570
for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
571
*s = 0;
572