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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Jul 19, 2014 at 17:35 UTC
5c0b13f85ab3a5326508b854768eb70c8829cda4
6 files changed
+6
-19
builtin/apply.c
+1
-3
@@ -2869,9 +2869,7 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)
2869
case BINARY_LITERAL_DEFLATED:
2870
clear_image(img);
2871
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);
2873
return 0;
2874
}
2875
return -1;
builtin/blame.c
+1
-4
@@ -2458,11 +2458,8 @@ parse_done:
2458
die("revision walk setup failed");
2459
2460
if (is_null_sha1(sb.final->object.sha1)) {
2461
- char *buf;
2461
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);
2463
sb.final_buf_size = o->file.size;
2464
}
2465
else {
connect.c
+1
-3
@@ -64,9 +64,7 @@ static void parse_one_symref_info(struct string_list *symref, const char *val, i
64
if (!len)
65
return; /* just "symref" */
66
/* 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);
68
target = strchr(sym, ':');
69
if (!target)
70
/* just "symref=something" */
http-backend.c
+1
-3
@@ -607,9 +607,7 @@ int main(int argc, char **argv)
607
608
cmd = c;
609
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);
611
dir[out[0].rm_so] = 0;
612
break;
613
}
path.c
+1
-3
@@ -249,9 +249,7 @@ int validate_headref(const char *path)
249
static struct passwd *getpw_str(const char *username, size_t len)
250
{
251
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);
253
pw = getpwnam(username_z);
254
free(username_z);
255
return pw;
sh-i18n--envsubst.c
+1
-3
@@ -278,9 +278,7 @@ static string_list_ty variables_set;
278
static void
279
note_variable (const char *var_ptr, size_t var_len)
280
{
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);
282
283
string_list_append (&variables_set, string);
284
}