Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 5c0b13f

Browse files
rscharfegitster
authored andcommitted
use xmemdupz() to allocate copies of strings given by start and length
Use xmemdupz() to allocate the memory, copy the data and make sure to NUL-terminate the result, all in one step. The resulting code is shorter, doesn't contain the constants 1 and '\0', and avoids duplicating function parameters. For blame, the last copied byte (o->file.ptr[o->file.size]) is always set to NUL by fake_working_tree_commit() or read_sha1_file(), so no information is lost by the conversion to using xmemdupz(). Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 51a60f5 commit 5c0b13f

File tree

6 files changed

+6
-19
lines changed

6 files changed

+6
-19
lines changed

builtin/apply.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,9 +2869,7 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)
28692869
case BINARY_LITERAL_DEFLATED:
28702870
clear_image(img);
28712871
img->len = fragment->size;
2872-
img->buf = xmalloc(img->len+1);
2873-
memcpy(img->buf, fragment->patch, img->len);
2874-
img->buf[img->len] = '\0';
2872+
img->buf = xmemdupz(fragment->patch, img->len);
28752873
return 0;
28762874
}
28772875
return -1;

builtin/blame.c

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

24602460
if (is_null_sha1(sb.final->object.sha1)) {
2461-
char *buf;
24622461
o = sb.final->util;
2463-
buf = xmalloc(o->file.size + 1);
2464-
memcpy(buf, o->file.ptr, o->file.size + 1);
2465-
sb.final_buf = buf;
2462+
sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
24662463
sb.final_buf_size = o->file.size;
24672464
}
24682465
else {

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
@@ -607,9 +607,7 @@ int main(int argc, char **argv)
607607

608608
cmd = c;
609609
n = out[0].rm_eo - out[0].rm_so;
610-
cmd_arg = xmalloc(n);
611-
memcpy(cmd_arg, dir + out[0].rm_so + 1, n-1);
612-
cmd_arg[n-1] = '\0';
610+
cmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1);
613611
dir[out[0].rm_so] = 0;
614612
break;
615613
}

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;

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)