Skip to content

Commit

Permalink
use xmemdupz() to allocate copies of strings given by start and length
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
René Scharfe authored and Junio C Hamano committed Jul 21, 2014
1 parent 51a60f5 commit 5c0b13f
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 19 deletions.
4 changes: 1 addition & 3 deletions builtin/apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2869,9 +2869,7 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)
case BINARY_LITERAL_DEFLATED:
clear_image(img);
img->len = fragment->size;
img->buf = xmalloc(img->len+1);
memcpy(img->buf, fragment->patch, img->len);
img->buf[img->len] = '\0';
img->buf = xmemdupz(fragment->patch, img->len);
return 0;
}
return -1;
Expand Down
5 changes: 1 addition & 4 deletions builtin/blame.c
Original file line number Diff line number Diff line change
Expand Up @@ -2458,11 +2458,8 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
die("revision walk setup failed");

if (is_null_sha1(sb.final->object.sha1)) {
char *buf;
o = sb.final->util;
buf = xmalloc(o->file.size + 1);
memcpy(buf, o->file.ptr, o->file.size + 1);
sb.final_buf = buf;
sb.final_buf = xmemdupz(o->file.ptr, o->file.size);
sb.final_buf_size = o->file.size;
}
else {
Expand Down
4 changes: 1 addition & 3 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ static void parse_one_symref_info(struct string_list *symref, const char *val, i
if (!len)
return; /* just "symref" */
/* e.g. "symref=HEAD:refs/heads/master" */
sym = xmalloc(len + 1);
memcpy(sym, val, len);
sym[len] = '\0';
sym = xmemdupz(val, len);
target = strchr(sym, ':');
if (!target)
/* just "symref=something" */
Expand Down
4 changes: 1 addition & 3 deletions http-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,7 @@ int main(int argc, char **argv)

cmd = c;
n = out[0].rm_eo - out[0].rm_so;
cmd_arg = xmalloc(n);
memcpy(cmd_arg, dir + out[0].rm_so + 1, n-1);
cmd_arg[n-1] = '\0';
cmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1);
dir[out[0].rm_so] = 0;
break;
}
Expand Down
4 changes: 1 addition & 3 deletions path.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,7 @@ int validate_headref(const char *path)
static struct passwd *getpw_str(const char *username, size_t len)
{
struct passwd *pw;
char *username_z = xmalloc(len + 1);
memcpy(username_z, username, len);
username_z[len] = '\0';
char *username_z = xmemdupz(username, len);
pw = getpwnam(username_z);
free(username_z);
return pw;
Expand Down
4 changes: 1 addition & 3 deletions sh-i18n--envsubst.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ static string_list_ty variables_set;
static void
note_variable (const char *var_ptr, size_t var_len)
{
char *string = xmalloc (var_len + 1);
memcpy (string, var_ptr, var_len);
string[var_len] = '\0';
char *string = xmemdupz (var_ptr, var_len);

string_list_append (&variables_set, string);
}
Expand Down

0 comments on commit 5c0b13f

Please sign in to comment.