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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jun 19, 2014 at 17:26 UTC
b2724c87872aaec55dd7e5529aa029c3108b43a5
5 files changed
+6
-23
builtin/apply.c
+1
-3
@@ -1281,9 +1281,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
1281
*/
1282
patch->def_name = git_header_name(line, len);
1283
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);
1285
free(patch->def_name);
1286
patch->def_name = s;
1287
}
builtin/fetch.c
+2
-7
@@ -1053,16 +1053,11 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
1053
refs = xcalloc(argc + 1, sizeof(const char *));
1054
for (i = 0; i < argc; i++) {
1055
if (!strcmp(argv[i], "tag")) {
1056
- char *ref;
1056
i++;
1057
if (i >= argc)
1058
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]);
1061
} else
1062
refs[j++] = argv[i];
1063
}
builtin/name-rev.c
+1
-4
@@ -33,10 +33,7 @@ static void name_rev(struct commit *commit,
33
return;
34
35
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);
37
38
if (generation)
39
die("generation: %d, but deref?", generation);
sha1_name.c
+1
-4
@@ -1252,10 +1252,7 @@ static void diagnose_invalid_sha1_path(const char *prefix,
1252
die("Path '%s' exists on disk, but not in '%.*s'.",
1253
filename, object_name_len, object_name);
1254
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);
1256
1257
if (!get_tree_entry(tree_sha1, fullname,
1258
sha1, &mode)) {
shell.c
+1
-5
@@ -46,11 +46,7 @@ static int is_valid_cmd_name(const char *cmd)
46
47
static char *make_cmd(const char *prog)
48
{
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);
50
}
51
52
static void cd_to_homedir(void)