Skip to content

Commit 2831018

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 95244ae commit 2831018

File tree

6 files changed

+18
-41
lines changed

6 files changed

+18
-41
lines changed

builtin/fmt-merge-msg.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,8 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
178178
int len = strlen(origin);
179179
if (origin[0] == '\'' && origin[len - 1] == '\'')
180180
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);
186183
if (strcmp(".", src))
187184
origin_data->is_local_branch = 0;
188185
string_list_append(&origins, origin)->util = origin_data;

builtin/show-branch.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
755755
}
756756

757757
for (i = 0; i < reflog; i++) {
758-
char *logmsg, *m;
758+
char *logmsg;
759759
const char *msg;
760760
unsigned long timestamp;
761761
int tz;
@@ -770,11 +770,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
770770
msg = "(none)";
771771
else
772772
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);
778776
free(logmsg);
779777
sprintf(nth_desc, "%s@{%d}", *av, base+i);
780778
append_ref(nth_desc, sha1, 1);

http-push.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
854854
struct xml_ctx ctx;
855855
char *escaped;
856856

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);
859858

860859
/* Make sure leading directories exist for the remote ref */
861860
ep = strchr(url + strlen(repo->url) + 1, '/');
@@ -1115,7 +1114,7 @@ static void remote_ls(const char *path, int flags,
11151114
void (*userFunc)(struct remote_ls_ctx *ls),
11161115
void *userData)
11171116
{
1118-
char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1117+
char *url = xstrfmt("%s%s", repo->url, path);
11191118
struct active_request_slot *slot;
11201119
struct slot_results results;
11211120
struct strbuf in_buffer = STRBUF_INIT;
@@ -1131,8 +1130,6 @@ static void remote_ls(const char *path, int flags,
11311130
ls.userData = userData;
11321131
ls.userFunc = userFunc;
11331132

1134-
sprintf(url, "%s%s", repo->url, path);
1135-
11361133
strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);
11371134

11381135
dav_headers = curl_slist_append(dav_headers, "Depth: 1");
@@ -1534,10 +1531,9 @@ static void update_remote_info_refs(struct remote_lock *lock)
15341531

15351532
static int remote_exists(const char *path)
15361533
{
1537-
char *url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1534+
char *url = xstrfmt("%s%s", repo->url, path);
15381535
int ret;
15391536

1540-
sprintf(url, "%s%s", repo->url, path);
15411537

15421538
switch (http_get_strbuf(url, NULL, NULL)) {
15431539
case HTTP_OK:
@@ -1557,12 +1553,9 @@ static int remote_exists(const char *path)
15571553

15581554
static void fetch_symref(const char *path, char **symref, unsigned char *sha1)
15591555
{
1560-
char *url;
1556+
char *url = xstrfmt("%s%s", repo->url, path);
15611557
struct strbuf buffer = STRBUF_INIT;
15621558

1563-
url = xmalloc(strlen(repo->url) + strlen(path) + 1);
1564-
sprintf(url, "%s%s", repo->url, path);
1565-
15661559
if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
15671560
die("Couldn't get %s for remote symref\n%s", url,
15681561
curl_errorstr);
@@ -1671,8 +1664,7 @@ static int delete_remote_branch(const char *pattern, int force)
16711664
fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
16721665
if (dry_run)
16731666
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);
16761668
slot = get_active_slot();
16771669
slot->results = &results;
16781670
curl_setup_http_get(slot->curl, url, DAV_DELETE);

http-walker.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,7 @@ static void fetch_alternates(struct walker *walker, const char *base)
341341
if (walker->get_verbosely)
342342
fprintf(stderr, "Getting alternates list for %s\n", base);
343343

344-
url = xmalloc(strlen(base) + 31);
345-
sprintf(url, "%s/objects/info/http-alternates", base);
344+
url = xstrfmt("%s/objects/info/http-alternates", base);
346345

347346
/*
348347
* Use a callback to process the result, since another request

match-trees.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,12 @@ static void match_trees(const unsigned char *hash1,
140140
goto next;
141141
score = score_trees(elem, hash2);
142142
if (*best_score < score) {
143-
char *newpath;
144-
newpath = xmalloc(strlen(base) + strlen(path) + 1);
145-
sprintf(newpath, "%s%s", base, path);
146143
free(*best_match);
147-
*best_match = newpath;
144+
*best_match = xstrfmt("%s%s", base, path);
148145
*best_score = score;
149146
}
150147
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);
154149
match_trees(elem, hash2, best_score, best_match,
155150
newbase, recurse_limit - 1);
156151
free(newbase);

merge-recursive.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -969,14 +969,10 @@ merge_file_special_markers(struct merge_options *o,
969969
char *side2 = NULL;
970970
struct merge_file_info mfi;
971971

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);
980976

981977
mfi = merge_file_1(o, one, a, b,
982978
side1 ? side1 : branch1, side2 ? side2 : branch2);

0 commit comments

Comments
 (0)