Skip to content

Commit b2724c8

Browse files
peffgitster
authored andcommitted
use xstrfmt to replace xmalloc + strcpy/strcat
It's easy to get manual allocation calculations wrong, and the use of strcpy/strcat raise red flags for people looking for buffer overflows (though in this case each site was fine). It's also shorter to use xstrfmt, and the printf-format tends to be easier for a reader to see what the final string will look like. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2831018 commit b2724c8

File tree

5 files changed

+6
-23
lines changed

5 files changed

+6
-23
lines changed

builtin/apply.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,9 +1281,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
12811281
*/
12821282
patch->def_name = git_header_name(line, len);
12831283
if (patch->def_name && root) {
1284-
char *s = xmalloc(root_len + strlen(patch->def_name) + 1);
1285-
strcpy(s, root);
1286-
strcpy(s + root_len, patch->def_name);
1284+
char *s = xstrfmt("%s%s", root, patch->def_name);
12871285
free(patch->def_name);
12881286
patch->def_name = s;
12891287
}

builtin/fetch.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,16 +1053,11 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
10531053
refs = xcalloc(argc + 1, sizeof(const char *));
10541054
for (i = 0; i < argc; i++) {
10551055
if (!strcmp(argv[i], "tag")) {
1056-
char *ref;
10571056
i++;
10581057
if (i >= argc)
10591058
die(_("You need to specify a tag name."));
1060-
ref = xmalloc(strlen(argv[i]) * 2 + 22);
1061-
strcpy(ref, "refs/tags/");
1062-
strcat(ref, argv[i]);
1063-
strcat(ref, ":refs/tags/");
1064-
strcat(ref, argv[i]);
1065-
refs[j++] = ref;
1059+
refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1060+
argv[i], argv[i]);
10661061
} else
10671062
refs[j++] = argv[i];
10681063
}

builtin/name-rev.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ static void name_rev(struct commit *commit,
3333
return;
3434

3535
if (deref) {
36-
char *new_name = xmalloc(strlen(tip_name)+3);
37-
strcpy(new_name, tip_name);
38-
strcat(new_name, "^0");
39-
tip_name = new_name;
36+
tip_name = xstrfmt("%s^0", tip_name);
4037

4138
if (generation)
4239
die("generation: %d, but deref?", generation);

sha1_name.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,10 +1252,7 @@ static void diagnose_invalid_sha1_path(const char *prefix,
12521252
die("Path '%s' exists on disk, but not in '%.*s'.",
12531253
filename, object_name_len, object_name);
12541254
if (errno == ENOENT || errno == ENOTDIR) {
1255-
char *fullname = xmalloc(strlen(filename)
1256-
+ strlen(prefix) + 1);
1257-
strcpy(fullname, prefix);
1258-
strcat(fullname, filename);
1255+
char *fullname = xstrfmt("%s%s", prefix, filename);
12591256

12601257
if (!get_tree_entry(tree_sha1, fullname,
12611258
sha1, &mode)) {

shell.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,7 @@ static int is_valid_cmd_name(const char *cmd)
4646

4747
static char *make_cmd(const char *prog)
4848
{
49-
char *prefix = xmalloc((strlen(prog) + strlen(COMMAND_DIR) + 2));
50-
strcpy(prefix, COMMAND_DIR);
51-
strcat(prefix, "/");
52-
strcat(prefix, prog);
53-
return prefix;
49+
return xstrfmt("%s/%s", COMMAND_DIR, prog);
5450
}
5551

5652
static void cd_to_homedir(void)

0 commit comments

Comments
 (0)