use xstrfmt to replace xmalloc + sprintf

This is one line shorter, and makes sure the length in the malloc and sprintf steps match. These conversions are very straightforward; we can drop the malloc entirely, and replace the sprintf with xstrfmt. 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:24 UTC 283101869bea8feb5d58f6ea1b568e9b197526d3
6 files changed +18 -41
builtin/fmt-merge-msg.c
+2 -5
@@ -178,11 +178,8 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
178 int len = strlen(origin);
179 if (origin[0] == '\'' && origin[len - 1] == '\'')
180 origin = xmemdupz(origin + 1, len - 2);
181 - } else {
182 - char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5);
183 - sprintf(new_origin, "%s of %s", origin, src);
184 - origin = new_origin;
185 - }
181 + } else
182 + origin = xstrfmt("%s of %s", origin, src);
183 if (strcmp(".", src))
184 origin_data->is_local_branch = 0;
185 string_list_append(&origins, origin)->util = origin_data;
builtin/show-branch.c
+4 -6
@@ -755,7 +755,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
755 }
756
757 for (i = 0; i < reflog; i++) {
758 - char *logmsg, *m;
758 + char *logmsg;
759 const char *msg;
760 unsigned long timestamp;
761 int tz;
@@ -770,11 +770,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
770 msg = "(none)";
771 else
772 msg++;
773 - m = xmalloc(strlen(msg) + 200);
774 - sprintf(m, "(%s) %s",
775 - show_date(timestamp, tz, 1),
776 - msg);
777 - reflog_msg[i] = m;
773 + reflog_msg[i] = xstrfmt("(%s) %s",
774 + show_date(timestamp, tz, 1),
775 + msg);
776 free(logmsg);
777 sprintf(nth_desc, "%s@{%d}", *av, base+i);
778 append_ref(nth_desc, sha1, 1);
http-push.c
+5 -13
@@ -854,8 +854,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
854 struct xml_ctx ctx;
855 char *escaped;
856
857 - url = xmalloc(strlen(repo->url) + strlen(path) + 1);
858 - sprintf(url, "%s%s", repo->url, path);
857 + url = xstrfmt("%s%s", repo->url, path);
858
859 /* Make sure leading directories exist for the remote ref */
860 ep = strchr(url + strlen(repo->url) + 1, '/');
@@ -1115,7 +1114,7 @@ static void remote_ls(const char *path, int flags,
1114 void (*userFunc)(struct remote_ls_ctx *ls),
1115 void *userData)
1116 {
1118 - char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1117 + char *url = xstrfmt("%s%s", repo->url, path);
1118 struct active_request_slot *slot;
1119 struct slot_results results;
1120 struct strbuf in_buffer = STRBUF_INIT;
@@ -1131,8 +1130,6 @@ static void remote_ls(const char *path, int flags,
1130 ls.userData = userData;
1131 ls.userFunc = userFunc;
1132
1134 - sprintf(url, "%s%s", repo->url, path);
1135 -
1133 strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
1134
1135 dav_headers = curl_slist_append(dav_headers, "Depth: 1");
@@ -1534,10 +1531,9 @@ static void update_remote_info_refs(struct remote_lock *lock)
1531
1532 static int remote_exists(const char *path)
1533 {
1537 - char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1534 + char *url = xstrfmt("%s%s", repo->url, path);
1535 int ret;
1536
1540 - sprintf(url, "%s%s", repo->url, path);
1537
1538 switch (http_get_strbuf(url, NULL, NULL)) {
1539 case HTTP_OK:
@@ -1557,12 +1553,9 @@ static int remote_exists(const char *path)
1553
1554 static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
1555 {
1560 - char *url;
1556 + char *url = xstrfmt("%s%s", repo->url, path);
1557 struct strbuf buffer = STRBUF_INIT;
1558
1563 - url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1564 - sprintf(url, "%s%s", repo->url, path);
1565 -
1559 if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
1560 die("Couldn't get %s for remote symref\n%s", url,
1561 curl_errorstr);
@@ -1671,8 +1664,7 @@ static int delete_remote_branch(const char *pattern, int force)
1664 fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
1665 if (dry_run)
1666 return 0;
1674 - url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1);
1675 - sprintf(url, "%s%s", repo->url, remote_ref->name);
1667 + url = xstrfmt("%s%s", repo->url, remote_ref->name);
1668 slot = get_active_slot();
1669 slot->results = &results;
1670 curl_setup_http_get(slot->curl, url, DAV_DELETE);
http-walker.c
+1 -2
@@ -341,8 +341,7 @@ static void fetch_alternates(struct walker *walker, const char *base)
341 if (walker->get_verbosely)
342 fprintf(stderr, "Getting alternates list for %s\n", base);
343
344 - url = xmalloc(strlen(base) + 31);
345 - sprintf(url, "%s/objects/info/http-alternates", base);
344 + url = xstrfmt("%s/objects/info/http-alternates", base);
345
346 /*
347 * Use a callback to process the result, since another request
match-trees.c
+2 -7
@@ -140,17 +140,12 @@ static void match_trees(const unsigned char *hash1,
140 goto next;
141 score = score_trees(elem, hash2);
142 if (*best_score < score) {
143 - char *newpath;
144 - newpath = xmalloc(strlen(base) + strlen(path) + 1);
145 - sprintf(newpath, "%s%s", base, path);
143 free(*best_match);
147 - *best_match = newpath;
144 + *best_match = xstrfmt("%s%s", base, path);
145 *best_score = score;
146 }
147 if (recurse_limit) {
151 - char *newbase;
152 - newbase = xmalloc(strlen(base) + strlen(path) + 2);
153 - sprintf(newbase, "%s%s/", base, path);
148 + char *newbase = xstrfmt("%s%s/", base, path);
149 match_trees(elem, hash2, best_score, best_match,
150 newbase, recurse_limit - 1);
151 free(newbase);
merge-recursive.c
+4 -8
@@ -969,14 +969,10 @@ merge_file_special_markers(struct merge_options *o,
969 char *side2 = NULL;
970 struct merge_file_info mfi;
971
972 - if (filename1) {
973 - side1 = xmalloc(strlen(branch1) + strlen(filename1) + 2);
974 - sprintf(side1, "%s:%s", branch1, filename1);
975 - }
976 - if (filename2) {
977 - side2 = xmalloc(strlen(branch2) + strlen(filename2) + 2);
978 - sprintf(side2, "%s:%s", branch2, filename2);
979 - }
972 + if (filename1)
973 + side1 = xstrfmt("%s:%s", branch1, filename1);
974 + if (filename2)
975 + side2 = xstrfmt("%s:%s", branch2, filename2);
976
977 mfi = merge_file_1(o, one, a, b,
978 side1 ? side1 : branch1, side2 ? side2 : branch2);