Skip to content

Commit 9ab0882

Browse files
committed
Merge branch 'maint'
* maint: use xmemdupz() to allocate copies of strings given by start and length use xcalloc() to allocate zero-initialized memory
2 parents 0eff86e + 5c0b13f commit 9ab0882

File tree

10 files changed

+10
-27
lines changed

10 files changed

+10
-27
lines changed

builtin/apply.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,9 +2867,7 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)
28672867
case BINARY_LITERAL_DEFLATED:
28682868
clear_image(img);
28692869
img->len = fragment->size;
2870-
img->buf = xmalloc(img->len+1);
2871-
memcpy(img->buf, fragment->patch, img->len);
2872-
img->buf[img->len] = '\0';
2870+
img->buf = xmemdupz(fragment->patch, img->len);
28732871
return 0;
28742872
}
28752873
return -1;

builtin/blame.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,11 +2707,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
27072707
die("revision walk setup failed");
27082708

27092709
if (is_null_sha1(sb.final->object.sha1)) {
2710-
char *buf;
27112710
o = sb.final->util;
2712-
buf = xmalloc(o->file.size + 1);
2713-
memcpy(buf, o->file.ptr, o->file.size + 1);
2714-
sb.final_buf = buf;
2711+
sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
27152712
sb.final_buf_size = o->file.size;
27162713
}
27172714
else {

builtin/clean.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
621621
nr += chosen[i];
622622
}
623623

624-
result = xmalloc(sizeof(int) * (nr + 1));
625-
memset(result, 0, sizeof(int) * (nr + 1));
624+
result = xcalloc(nr + 1, sizeof(int));
626625
for (i = 0; i < stuff->nr && j < nr; i++) {
627626
if (chosen[i])
628627
result[j++] = i;

builtin/index-pack.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,7 @@ static void set_thread_data(struct thread_local *data)
362362

363363
static struct base_data *alloc_base_data(void)
364364
{
365-
struct base_data *base = xmalloc(sizeof(struct base_data));
366-
memset(base, 0, sizeof(*base));
365+
struct base_data *base = xcalloc(1, sizeof(struct base_data));
367366
base->ref_last = -1;
368367
base->ofs_last = -1;
369368
return base;

compat/mingw.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,7 @@ static int WSAAPI getaddrinfo_stub(const char *node, const char *service,
13161316
else
13171317
ai->ai_canonname = NULL;
13181318

1319-
sin = xmalloc(ai->ai_addrlen);
1320-
memset(sin, 0, ai->ai_addrlen);
1319+
sin = xcalloc(1, ai->ai_addrlen);
13211320
sin->sin_family = AF_INET;
13221321
/* Note: getaddrinfo is supposed to allow service to be a string,
13231322
* which should be looked up using getservbyname. This is

connect.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ static void parse_one_symref_info(struct string_list *symref, const char *val, i
6464
if (!len)
6565
return; /* just "symref" */
6666
/* e.g. "symref=HEAD:refs/heads/master" */
67-
sym = xmalloc(len + 1);
68-
memcpy(sym, val, len);
69-
sym[len] = '\0';
67+
sym = xmemdupz(val, len);
7068
target = strchr(sym, ':');
7169
if (!target)
7270
/* just "symref=something" */

http-backend.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,9 +610,7 @@ int main(int argc, char **argv)
610610

611611
cmd = c;
612612
n = out[0].rm_eo - out[0].rm_so;
613-
cmd_arg = xmalloc(n);
614-
memcpy(cmd_arg, dir + out[0].rm_so + 1, n-1);
615-
cmd_arg[n-1] = '\0';
613+
cmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1);
616614
dir[out[0].rm_so] = 0;
617615
break;
618616
}

path.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,7 @@ int validate_headref(const char *path)
249249
static struct passwd *getpw_str(const char *username, size_t len)
250250
{
251251
struct passwd *pw;
252-
char *username_z = xmalloc(len + 1);
253-
memcpy(username_z, username, len);
254-
username_z[len] = '\0';
252+
char *username_z = xmemdupz(username, len);
255253
pw = getpwnam(username_z);
256254
free(username_z);
257255
return pw;

pathspec.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,8 +389,7 @@ void parse_pathspec(struct pathspec *pathspec,
389389
if (!(flags & PATHSPEC_PREFER_CWD))
390390
die("BUG: PATHSPEC_PREFER_CWD requires arguments");
391391

392-
pathspec->items = item = xmalloc(sizeof(*item));
393-
memset(item, 0, sizeof(*item));
392+
pathspec->items = item = xcalloc(1, sizeof(*item));
394393
item->match = prefix;
395394
item->original = prefix;
396395
item->nowildcard_len = item->len = strlen(prefix);

sh-i18n--envsubst.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ static string_list_ty variables_set;
278278
static void
279279
note_variable (const char *var_ptr, size_t var_len)
280280
{
281-
char *string = xmalloc (var_len + 1);
282-
memcpy (string, var_ptr, var_len);
283-
string[var_len] = '\0';
281+
char *string = xmemdupz (var_ptr, var_len);
284282

285283
string_list_append (&variables_set, string);
286284
}

0 commit comments

Comments
 (0)