Browse Source

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>
maint
Jeff King 11 years ago committed by Junio C Hamano
parent
commit
283101869b
  1. 7
      builtin/fmt-merge-msg.c
  2. 10
      builtin/show-branch.c
  3. 18
      http-push.c
  4. 3
      http-walker.c
  5. 9
      match-trees.c
  6. 12
      merge-recursive.c

7
builtin/fmt-merge-msg.c

@ -178,11 +178,8 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
int len = strlen(origin); int len = strlen(origin);
if (origin[0] == '\'' && origin[len - 1] == '\'') if (origin[0] == '\'' && origin[len - 1] == '\'')
origin = xmemdupz(origin + 1, len - 2); origin = xmemdupz(origin + 1, len - 2);
} else { } else
char *new_origin = xmalloc(strlen(origin) + strlen(src) + 5); origin = xstrfmt("%s of %s", origin, src);
sprintf(new_origin, "%s of %s", origin, src);
origin = new_origin;
}
if (strcmp(".", src)) if (strcmp(".", src))
origin_data->is_local_branch = 0; origin_data->is_local_branch = 0;
string_list_append(&origins, origin)->util = origin_data; string_list_append(&origins, origin)->util = origin_data;

10
builtin/show-branch.c

@ -755,7 +755,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
} }


for (i = 0; i < reflog; i++) { for (i = 0; i < reflog; i++) {
char *logmsg, *m; char *logmsg;
const char *msg; const char *msg;
unsigned long timestamp; unsigned long timestamp;
int tz; int tz;
@ -770,11 +770,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
msg = "(none)"; msg = "(none)";
else else
msg++; msg++;
m = xmalloc(strlen(msg) + 200); reflog_msg[i] = xstrfmt("(%s) %s",
sprintf(m, "(%s) %s", show_date(timestamp, tz, 1),
show_date(timestamp, tz, 1), msg);
msg);
reflog_msg[i] = m;
free(logmsg); free(logmsg);
sprintf(nth_desc, "%s@{%d}", *av, base+i); sprintf(nth_desc, "%s@{%d}", *av, base+i);
append_ref(nth_desc, sha1, 1); append_ref(nth_desc, sha1, 1);

18
http-push.c

@ -854,8 +854,7 @@ static struct remote_lock *lock_remote(const char *path, long timeout)
struct xml_ctx ctx; struct xml_ctx ctx;
char *escaped; char *escaped;


url = xmalloc(strlen(repo->url) + strlen(path) + 1); url = xstrfmt("%s%s", repo->url, path);
sprintf(url, "%s%s", repo->url, path);


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


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

strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST); strbuf_addf(&out_buffer.buf, PROPFIND_ALL_REQUEST);


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


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


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


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


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


url = xmalloc(strlen(repo->url) + strlen(path) + 1);
sprintf(url, "%s%s", repo->url, path);

if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK) if (http_get_strbuf(url, &buffer, NULL) != HTTP_OK)
die("Couldn't get %s for remote symref\n%s", url, die("Couldn't get %s for remote symref\n%s", url,
curl_errorstr); curl_errorstr);
@ -1671,8 +1664,7 @@ static int delete_remote_branch(const char *pattern, int force)
fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name); fprintf(stderr, "Removing remote branch '%s'\n", remote_ref->name);
if (dry_run) if (dry_run)
return 0; return 0;
url = xmalloc(strlen(repo->url) + strlen(remote_ref->name) + 1); url = xstrfmt("%s%s", repo->url, remote_ref->name);
sprintf(url, "%s%s", repo->url, remote_ref->name);
slot = get_active_slot(); slot = get_active_slot();
slot->results = &results; slot->results = &results;
curl_setup_http_get(slot->curl, url, DAV_DELETE); curl_setup_http_get(slot->curl, url, DAV_DELETE);

3
http-walker.c

@ -341,8 +341,7 @@ static void fetch_alternates(struct walker *walker, const char *base)
if (walker->get_verbosely) if (walker->get_verbosely)
fprintf(stderr, "Getting alternates list for %s\n", base); fprintf(stderr, "Getting alternates list for %s\n", base);


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


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

9
match-trees.c

@ -140,17 +140,12 @@ static void match_trees(const unsigned char *hash1,
goto next; goto next;
score = score_trees(elem, hash2); score = score_trees(elem, hash2);
if (*best_score < score) { if (*best_score < score) {
char *newpath;
newpath = xmalloc(strlen(base) + strlen(path) + 1);
sprintf(newpath, "%s%s", base, path);
free(*best_match); free(*best_match);
*best_match = newpath; *best_match = xstrfmt("%s%s", base, path);
*best_score = score; *best_score = score;
} }
if (recurse_limit) { if (recurse_limit) {
char *newbase; char *newbase = xstrfmt("%s%s/", base, path);
newbase = xmalloc(strlen(base) + strlen(path) + 2);
sprintf(newbase, "%s%s/", base, path);
match_trees(elem, hash2, best_score, best_match, match_trees(elem, hash2, best_score, best_match,
newbase, recurse_limit - 1); newbase, recurse_limit - 1);
free(newbase); free(newbase);

12
merge-recursive.c

@ -969,14 +969,10 @@ merge_file_special_markers(struct merge_options *o,
char *side2 = NULL; char *side2 = NULL;
struct merge_file_info mfi; struct merge_file_info mfi;


if (filename1) { if (filename1)
side1 = xmalloc(strlen(branch1) + strlen(filename1) + 2); side1 = xstrfmt("%s:%s", branch1, filename1);
sprintf(side1, "%s:%s", branch1, filename1); if (filename2)
} side2 = xstrfmt("%s:%s", branch2, filename2);
if (filename2) {
side2 = xmalloc(strlen(branch2) + strlen(filename2) + 2);
sprintf(side2, "%s:%s", branch2, filename2);
}


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

Loading…
Cancel
Save