{"thread":{"id":"37170","subject":"[PATCH] use xmemdupz() to allocate copies of strings given by start and length","startedAt":"2014-07-19T15:35:34Z","lastAt":"2014-07-19T15:35:34Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"246372","messageId":"53CA9046.1090709@web.de","threadId":"37170","inReplyTo":null,"subject":"[PATCH] use xmemdupz() to allocate copies of strings given by start and length","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2014-07-19T15:35:34Z","receivedAt":"2014-07-19T15:35:34Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Use xmemdupz() to allocate the memory, copy the data and make sure to\nNUL-terminate the result, all in one step.  The resulting code is\nshorter, doesn't contain the constants 1 and '\\0', and avoids\nduplicating function parameters.\n\nFor blame, the last copied byte (o->file.ptr[o->file.size]) is always\nset to NUL by fake_working_tree_commit() or read_sha1_file(), so no\ninformation is lost by the conversion to using xmemdupz().\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n builtin/apply.c     | 4 +---\n builtin/blame.c     | 5 +----\n connect.c           | 4 +---\n http-backend.c      | 4 +---\n path.c              | 4 +---\n sh-i18n--envsubst.c | 4 +---\n 6 files changed, 6 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5fd099e..9f8f5ba 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -2867,9 +2867,7 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)\n \tcase BINARY_LITERAL_DEFLATED:\n \t\tclear_image(img);\n \t\timg->len = fragment->size;\n-\t\timg->buf = xmalloc(img->len+1);\n-\t\tmemcpy(img->buf, fragment->patch, img->len);\n-\t\timg->buf[img->len] = '\\0';\n+\t\timg->buf = xmemdupz(fragment->patch, img->len);\n \t\treturn 0;\n \t}\n \treturn -1;\ndiff --git a/builtin/blame.c b/builtin/blame.c\nindex c59e702..32ce05f 100644\n--- a/builtin/blame.c\n+++ b/builtin/blame.c\n@@ -2707,11 +2707,8 @@ parse_done:\n \t\tdie(\"revision walk setup failed\");\n \n \tif (is_null_sha1(sb.final->object.sha1)) {\n-\t\tchar *buf;\n \t\to = sb.final->util;\n-\t\tbuf = xmalloc(o->file.size + 1);\n-\t\tmemcpy(buf, o->file.ptr, o->file.size + 1);\n-\t\tsb.final_buf = buf;\n+\t\tsb.final_buf = xmemdupz(o->file.ptr, o->file.size);\n \t\tsb.final_buf_size = o->file.size;\n \t}\n \telse {\ndiff --git a/connect.c b/connect.c\nindex 37ff018..5047402 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -64,9 +64,7 @@ static void parse_one_symref_info(struct string_list *symref, const char *val, i\n \tif (!len)\n \t\treturn; /* just \"symref\" */\n \t/* e.g. \"symref=HEAD:refs/heads/master\" */\n-\tsym = xmalloc(len + 1);\n-\tmemcpy(sym, val, len);\n-\tsym[len] = '\\0';\n+\tsym = xmemdupz(val, len);\n \ttarget = strchr(sym, ':');\n \tif (!target)\n \t\t/* just \"symref=something\" */\ndiff --git a/http-backend.c b/http-backend.c\nindex 57290d9..80790bb 100644\n--- a/http-backend.c\n+++ b/http-backend.c\n@@ -610,9 +610,7 @@ int main(int argc, char **argv)\n \n \t\t\tcmd = c;\n \t\t\tn = out[0].rm_eo - out[0].rm_so;\n-\t\t\tcmd_arg = xmalloc(n);\n-\t\t\tmemcpy(cmd_arg, dir + out[0].rm_so + 1, n-1);\n-\t\t\tcmd_arg[n-1] = '\\0';\n+\t\t\tcmd_arg = xmemdupz(dir + out[0].rm_so + 1, n - 1);\n \t\t\tdir[out[0].rm_so] = 0;\n \t\t\tbreak;\n \t\t}\ndiff --git a/path.c b/path.c\nindex bc804a3..25c3b8b 100644\n--- a/path.c\n+++ b/path.c\n@@ -249,9 +249,7 @@ int validate_headref(const char *path)\n static struct passwd *getpw_str(const char *username, size_t len)\n {\n \tstruct passwd *pw;\n-\tchar *username_z = xmalloc(len + 1);\n-\tmemcpy(username_z, username, len);\n-\tusername_z[len] = '\\0';\n+\tchar *username_z = xmemdupz(username, len);\n \tpw = getpwnam(username_z);\n \tfree(username_z);\n \treturn pw;\ndiff --git a/sh-i18n--envsubst.c b/sh-i18n--envsubst.c\nindex 855d28c..6dd03a9 100644\n--- a/sh-i18n--envsubst.c\n+++ b/sh-i18n--envsubst.c\n@@ -278,9 +278,7 @@ static string_list_ty variables_set;\n static void\n note_variable (const char *var_ptr, size_t var_len)\n {\n-  char *string = xmalloc (var_len + 1);\n-  memcpy (string, var_ptr, var_len);\n-  string[var_len] = '\\0';\n+  char *string = xmemdupz (var_ptr, var_len);\n \n   string_list_append (&variables_set, string);\n }\n-- \n2.0.2\n"}]}