Skip to content

Commit 95244ae

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fa3f60b commit 95244ae

File tree

3 files changed

+4
-10
lines changed

3 files changed

+4
-10
lines changed

builtin/receive-pack.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,9 @@ static void run_update_post_hook(struct command *commands)
614614
argv[0] = hook;
615615

616616
for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
617-
char *p;
618617
if (cmd->error_string || cmd->did_not_exist)
619618
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);
623620
argc++;
624621
}
625622
argv[argc] = NULL;

http-push.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -767,15 +767,13 @@ static void handle_new_lock_ctx(struct xml_ctx *ctx, int tag_closed)
767767

768768
if (tag_closed && ctx->cdata) {
769769
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);
772771
} else if (!strcmp(ctx->name, DAV_ACTIVELOCK_TIMEOUT)) {
773772
if (starts_with(ctx->cdata, "Second-"))
774773
lock->timeout =
775774
strtol(ctx->cdata + 7, NULL, 10);
776775
} 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);
779777

780778
git_SHA1_Init(&sha_ctx);
781779
git_SHA1_Update(&sha_ctx, lock->token, strlen(lock->token));

http-walker.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,7 @@ struct walker *get_http_walker(const char *url)
566566
struct walker *walker = xmalloc(sizeof(struct walker));
567567

568568
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);
571570
for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
572571
*s = 0;
573572

0 commit comments

Comments
 (0)