use xstrfmt to replace xmalloc + strcpy/strcat
authorJeff King <peff@peff.net>
Thu, 19 Jun 2014 21:26:56 +0000 (17:26 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 19 Jun 2014 22:20:54 +0000 (15:20 -0700)
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>
builtin/apply.c
builtin/fetch.c
builtin/name-rev.c
sha1_name.c
shell.c
index 9c5724eaccfaee62ff10eb097adc39ace351cade..b79691053ff26c853da7884b6d0484285801c6e7 100644 (file)
@@ -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);
        if (patch->def_name && root) {
         */
        patch->def_name = git_header_name(line, len);
        if (patch->def_name && root) {
-               char *s = xmalloc(root_len + strlen(patch->def_name) + 1);
-               strcpy(s, root);
-               strcpy(s + root_len, patch->def_name);
+               char *s = xstrfmt("%s%s", root, patch->def_name);
                free(patch->def_name);
                patch->def_name = s;
        }
                free(patch->def_name);
                patch->def_name = s;
        }
index 55f457c04f5c72ca31ab4fbc28e62ea48b6cb471..40d989f9ffa17b3f9c5b59ad225b5dedbea73ba6 100644 (file)
@@ -1053,16 +1053,11 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)
                refs = xcalloc(argc + 1, sizeof(const char *));
                for (i = 0; i < argc; i++) {
                        if (!strcmp(argv[i], "tag")) {
                refs = xcalloc(argc + 1, sizeof(const char *));
                for (i = 0; i < argc; i++) {
                        if (!strcmp(argv[i], "tag")) {
-                               char *ref;
                                i++;
                                if (i >= argc)
                                        die(_("You need to specify a tag name."));
                                i++;
                                if (i >= argc)
                                        die(_("You need to specify a tag name."));
-                               ref = xmalloc(strlen(argv[i]) * 2 + 22);
-                               strcpy(ref, "refs/tags/");
-                               strcat(ref, argv[i]);
-                               strcat(ref, ":refs/tags/");
-                               strcat(ref, argv[i]);
-                               refs[j++] = ref;
+                               refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
+                                                   argv[i], argv[i]);
                        } else
                                refs[j++] = argv[i];
                }
                        } else
                                refs[j++] = argv[i];
                }
index c824d4ec5f1c076e4833c690dad378125dde6da9..3c8f319be675d14a6ee60d303335190b7bdce9c8 100644 (file)
@@ -33,10 +33,7 @@ static void name_rev(struct commit *commit,
                return;
 
        if (deref) {
                return;
 
        if (deref) {
-               char *new_name = xmalloc(strlen(tip_name)+3);
-               strcpy(new_name, tip_name);
-               strcat(new_name, "^0");
-               tip_name = new_name;
+               tip_name = xstrfmt("%s^0", tip_name);
 
                if (generation)
                        die("generation: %d, but deref?", generation);
 
                if (generation)
                        die("generation: %d, but deref?", generation);
index 2b6322fad064845f4c782286fe9764f6b958f30d..5e956904b6684bca0cca4e50c71e2d94ce1b8ba3 100644 (file)
@@ -1252,10 +1252,7 @@ static void diagnose_invalid_sha1_path(const char *prefix,
                die("Path '%s' exists on disk, but not in '%.*s'.",
                    filename, object_name_len, object_name);
        if (errno == ENOENT || errno == ENOTDIR) {
                die("Path '%s' exists on disk, but not in '%.*s'.",
                    filename, object_name_len, object_name);
        if (errno == ENOENT || errno == ENOTDIR) {
-               char *fullname = xmalloc(strlen(filename)
-                                            + strlen(prefix) + 1);
-               strcpy(fullname, prefix);
-               strcat(fullname, filename);
+               char *fullname = xstrfmt("%s%s", prefix, filename);
 
                if (!get_tree_entry(tree_sha1, fullname,
                                    sha1, &mode)) {
 
                if (!get_tree_entry(tree_sha1, fullname,
                                    sha1, &mode)) {
diff --git a/shell.c b/shell.c
index 5c0d47a5cc1ae85a03e40321e97ddfa859ff7e1a..ace62e4b6503d821962242f9cf3bd9af22bc39b9 100644 (file)
--- a/shell.c
+++ b/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));
-       strcpy(prefix, COMMAND_DIR);
-       strcat(prefix, "/");
-       strcat(prefix, prog);
-       return prefix;
+       return xstrfmt("%s/%s", COMMAND_DIR, prog);
 }
 
 static void cd_to_homedir(void)
 }
 
 static void cd_to_homedir(void)