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>
This commit is contained in:
parent
283101869b
commit
b2724c8787
@ -1281,9 +1281,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
|
|||||||
*/
|
*/
|
||||||
patch->def_name = git_header_name(line, len);
|
patch->def_name = git_header_name(line, len);
|
||||||
if (patch->def_name && root) {
|
if (patch->def_name && root) {
|
||||||
char *s = xmalloc(root_len + strlen(patch->def_name) + 1);
|
char *s = xstrfmt("%s%s", root, patch->def_name);
|
||||||
strcpy(s, root);
|
|
||||||
strcpy(s + root_len, patch->def_name);
|
|
||||||
free(patch->def_name);
|
free(patch->def_name);
|
||||||
patch->def_name = s;
|
patch->def_name = s;
|
||||||
}
|
}
|
||||||
|
@ -1053,16 +1053,11 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
|
|||||||
refs = xcalloc(argc + 1, sizeof(const char *));
|
refs = xcalloc(argc + 1, sizeof(const char *));
|
||||||
for (i = 0; i < argc; i++) {
|
for (i = 0; i < argc; i++) {
|
||||||
if (!strcmp(argv[i], "tag")) {
|
if (!strcmp(argv[i], "tag")) {
|
||||||
char *ref;
|
|
||||||
i++;
|
i++;
|
||||||
if (i >= argc)
|
if (i >= argc)
|
||||||
die(_("You need to specify a tag name."));
|
die(_("You need to specify a tag name."));
|
||||||
ref = xmalloc(strlen(argv[i]) * 2 + 22);
|
refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
|
||||||
strcpy(ref, "refs/tags/");
|
argv[i], argv[i]);
|
||||||
strcat(ref, argv[i]);
|
|
||||||
strcat(ref, ":refs/tags/");
|
|
||||||
strcat(ref, argv[i]);
|
|
||||||
refs[j++] = ref;
|
|
||||||
} else
|
} else
|
||||||
refs[j++] = argv[i];
|
refs[j++] = argv[i];
|
||||||
}
|
}
|
||||||
|
@ -33,10 +33,7 @@ static void name_rev(struct commit *commit,
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
if (deref) {
|
if (deref) {
|
||||||
char *new_name = xmalloc(strlen(tip_name)+3);
|
tip_name = xstrfmt("%s^0", tip_name);
|
||||||
strcpy(new_name, tip_name);
|
|
||||||
strcat(new_name, "^0");
|
|
||||||
tip_name = new_name;
|
|
||||||
|
|
||||||
if (generation)
|
if (generation)
|
||||||
die("generation: %d, but deref?", generation);
|
die("generation: %d, but deref?", generation);
|
||||||
|
@ -1252,10 +1252,7 @@ static void diagnose_invalid_sha1_path(const char *prefix,
|
|||||||
die("Path '%s' exists on disk, but not in '%.*s'.",
|
die("Path '%s' exists on disk, but not in '%.*s'.",
|
||||||
filename, object_name_len, object_name);
|
filename, object_name_len, object_name);
|
||||||
if (errno == ENOENT || errno == ENOTDIR) {
|
if (errno == ENOENT || errno == ENOTDIR) {
|
||||||
char *fullname = xmalloc(strlen(filename)
|
char *fullname = xstrfmt("%s%s", prefix, filename);
|
||||||
+ strlen(prefix) + 1);
|
|
||||||
strcpy(fullname, prefix);
|
|
||||||
strcat(fullname, filename);
|
|
||||||
|
|
||||||
if (!get_tree_entry(tree_sha1, fullname,
|
if (!get_tree_entry(tree_sha1, fullname,
|
||||||
sha1, &mode)) {
|
sha1, &mode)) {
|
||||||
|
6
shell.c
6
shell.c
@ -46,11 +46,7 @@ static int is_valid_cmd_name(const char *cmd)
|
|||||||
|
|
||||||
static char *make_cmd(const char *prog)
|
static char *make_cmd(const char *prog)
|
||||||
{
|
{
|
||||||
char *prefix = xmalloc((strlen(prog) + strlen(COMMAND_DIR) + 2));
|
return xstrfmt("%s/%s", COMMAND_DIR, prog);
|
||||||
strcpy(prefix, COMMAND_DIR);
|
|
||||||
strcat(prefix, "/");
|
|
||||||
strcat(prefix, prog);
|
|
||||||
return prefix;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cd_to_homedir(void)
|
static void cd_to_homedir(void)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user