hardening allocations against integer overflow
cover hardening allocations against integer overflow
About 6 months or so ago, I did an audit of git's code base for uses of strcpy and sprintf that could overflow, fixing any bugs and cleaning up any suspect spots to make further audits simpler. This is a continuation of that work, for size computations which can overflow and cause us to allocate a too-small buffer. E.g., something like:
char *concat(const char *a, const char *b)
{
unsigned len_a = strlen(a);
unsigned len_b = strlen(b);
char *ret = xmalloc(len_a + len_b);
memcpy(ret, a, len_a);
memcpy(ret, b, len_b);}
will behave badly if the sum of "a" and "b" overflows "unsigned". There are other variants, too (we are also truncating the return value from strlen, and we'd frequently use "int" here, so the lengths can actually be negative!). It also varies based on platform. If the sites use size_t instead of int, then 64-bit systems are typically hard to trigger in practice (just because you'd need petabytes to store "a" and "b" in the first place).
The only bug I have actually confirmed in practice here is fixed by patch 2 (which is why it's at the front). There's another one in path_name(), but that function is already dropped by the nearby jk/lose-name-path topic.
The rest are cleanups of spots which _might_ be buggy, but I didn't dig too far to find out. As with the earlier audit, I tried to refactor using helpers that make the code clearer and less error-prone. So maybe they're fixing bugs or not, but they certainly make it easier to audit the result for problems.
[01/18]: add helpers for detecting size_t overflow
[02/18]: tree-diff: catch integer overflow in combine_diff_path allocation [03/18]: harden REALLOC_ARRAY and xcalloc against size_t overflow [04/18]: add helpers for allocating flex-array structs [05/18]: convert trivial cases to ALLOC_ARRAY
[06/18]: use xmallocz to avoid size arithmetic
[07/18]: convert trivial cases to FLEX_ARRAY macros
[08/18]: use st_add and st_mult for allocation size computation [09/18]: write_untracked_extension: use FLEX_ALLOC helper [10/18]: fast-import: simplify allocation in start_packfile [11/18]: fetch-pack: simplify add_sought_entry
[12/18]: test-path-utils: fix normalize_path_copy output buffer size [13/18]: sequencer: simplify memory allocation of get_message [14/18]: git-compat-util: drop mempcpy compat code
[15/18]: transport_anonymize_url: use xstrfmt
[16/18]: diff_populate_gitlink: use a strbuf
[17/18]: convert ewah/bitmap code to use xmalloc
[18/18]: ewah: convert to REALLOC_ARRAY, etc
-Peff
PATCH 1/18 add helpers for detecting size_t overflow
Performing computations on size_t variables that we feed to xmalloc and friends can be dangerous, as an integer overflow can cause us to allocate a much smaller chunk than we realized.
We already have unsigned_add_overflows(), but let's add unsigned_mult_overflows() to that. Furthermore, rather than have each site manually check and die on overflow, we can provide some helpers that will:
- promote the arguments to size_t, so that we know we are doing our computation in the same size of integer that will ultimately be fed to xmalloc
- check and die on overflow
- return the result so that computations can be done in the parameter list of xmalloc.
These functions are a lot uglier to use than normal arithmetic operators (you have to do "st_add(foo, bar)" instead of "foo + bar"). To at least limit the damage, we also provide multi-valued versions. So rather than:
st_add(st_add(a, b), st_add(c, d));
you can write:
st_add4(a, b, c, d);
This isn't nearly as elegant as a varargs function, but it's a lot harder to get it wrong. You don't have to remember to add a sentinel value at the end, and the compiler will complain if you get the number of arguments wrong. This patch adds only the numbered variants required to convert the current code base; we can easily add more later if needed.
---
The st_* names aren't amazing, but I think they mostly work. Suggestions welcome, but please keep bikeshedding to a minimum. :)
I almost went with checked_add(), checked_mult(), etc. But these inherently promote their arguments to size_t, so:
int x = checked_add(a, b);
is buggy; the user _should_ be reminded of the result type in each call.
These could also build on compiler intrinsics for extra speed. I'm happy to do that as a follow-up, but I doubt it really matters in practice. We're about to call malloc() in all cases, so an extra integer computation is almost certainly irrelevant. So I went for simplicity to start with.
git-compat-util.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
git-compat-util.h +34 -0
@@ -96,6 +96,14 @@#define unsigned_add_overflows (a, b) \ ((b) > maximum_unsigned_value_of_type(a) - (a)) +/* +* Returns true if the multiplication of "a" and "b" will +* overflow. The types of "a" and "b" must match and must be unsigned. +* Note that this macro evaluates "a" twice! +*/ +#define unsigned_mult_overflows (a, b) \ + ((a) && (b) > maximum_unsigned_value_of_type(a) / (a)) +#ifdef __GNUC__#define TYPEOF (x) (__typeof__(x))#else
@@ -713,6 +721,32 @@ extern void release_pack_memory(size_t);typedef void (*try_to_free_t )(size_t );extern try_to_free_t set_try_to_free_routine (try_to_free_t ); +static inline size_t st_add (size_t a,size_t b) +{ +if (unsigned_add_overflows (a, b)) +die ("size_t overflow: %" PRIuMAX" + %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a + b; +} +#define st_add3 (a,b,c) st_add((a),st_add((b),(c))) +#define st_add4 (a,b,c,d) st_add((a),st_add3((b),(c),(d))) + +static inline size_t st_mult (size_t a,size_t b) +{ +if (unsigned_mult_overflows (a, b)) +die ("size_t overflow: %" PRIuMAX" * %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a * b; +} + +static inline size_t st_sub (size_t a,size_t b) +{ +if (a < b) +die ("size_t underflow: %" PRIuMAX" - %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a - b; +} +#ifdef HAVE_ALLOCA_H # include <alloca.h> # define xalloca (size) (alloca(size))
Signature · 1 line
2.7.1.572.gf718037
PATCH 2/18 tree-diff: catch integer overflow in combine_diff_path allocation
In reply to Jeff KingA combine_diff_path struct has two "flex" members allocated alongside the struct: a string to hold the pathname, and an array of parent pointers. We use an "int" to compute this, meaning we may easily overflow it if the pathname is extremely long.
We can fix this by using size_t, and checking for overflow with the st_add helper.
--- diff.h | 4 ++-- tree-diff.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
diff.h +2 -2
@@ -222,8 +222,8 @@ struct combine_diff_path { } parent[FLEX_ARRAY ]; };#define combine_diff_path_size (n, l) \ - (sizeof(struct combine_diff_path) + \ - sizeof(struct combine_diff_parent) * (n) + (l) + 1) + st_add4(sizeof(struct combine_diff_path), (l), 1, \ + st_mult(sizeof(struct combine_diff_parent), (n)))extern void show_combined_diff (struct combine_diff_path *elem,int num_parent,int dense,struct rev_info *);
tree-diff.c +2 -2
@@ -124,8 +124,8 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last, unsigned mode, const unsigned char *sha1) { struct combine_diff_path *p; -int len = base->len + pathlen; -int alloclen =combine_diff_path_size (nparent, len); +size_t len =st_add (base->len, pathlen); +size_t alloclen =combine_diff_path_size (nparent, len);/* if last->next is !NULL - it is a pre-allocated memory, we can reuse */ p = last->next;
Signature · 1 line
2.7.1.572.gf718037
PATCH 3/18 harden REALLOC_ARRAY and xcalloc against size_t overflow
In reply to Jeff KingREALLOC_ARRAY inherently involves a multiplication which can overflow size_t, resulting in a much smaller buffer than we think we've allocated. We can easily harden it by using st_mult() to check for overflow. Likewise, we can add ALLOC_ARRAY to do the same thing for xmalloc calls.
xcalloc() should already be fine, because it takes the two factors separately, assuming the system calloc actually checks for overflow. However, before we even hit the system calloc(), we do our memory_limit_check, which involves a multiplication. Let's check for overflow ourselves so that this limit cannot be bypassed.
--- git-compat-util.h | 3 ++- wrapper.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-)
git-compat-util.h +2 -1
@@ -779,7 +779,8 @@ extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);extern char *xgetcwd (void );extern FILE *fopen_for_writing (const char *path); -#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) +#define ALLOC_ARRAY (x, alloc) (x) = xmalloc(st_mult((alloc), sizeof(*(x)))) +#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), st_mult((alloc), sizeof(*(x))))static inline char *xstrdup_or_null (const char *str) {
wrapper.c +3 -0
@@ -152,6 +152,9 @@ void *xcalloc(size_t nmemb, size_t size) {void *ret; +if (unsigned_mult_overflows (nmemb, size)) +die ("data too large to fit into virtual memory space" ); +memory_limit_check (size * nmemb,0 ); ret =calloc (nmemb, size);if (!ret && (!nmemb || !size))
Signature · 1 line
2.7.1.572.gf718037
PATCH 4/18 add helpers for allocating flex-array structs
In reply to Jeff KingAllocating a struct with a flex array is pretty simple in
practice: you over-allocate the struct, then copy some data into the over-allocation. But it can be a slight pain to make sure you're allocating and copying the right amounts.
This patch adds a few helpers to turn simple cases of into a one-liner that properly checks for overflow. See the embedded documentation for details.
Ideally we could provide a more flexible version that could handle multiple strings, like:
FLEX_ALLOC_FMT(ref, name, "%s%s", prefix, name);
But we have to implement this as a macro (because of the offset calculation of the flex member), which means we would need all compilers to support variadic macros.
--- git-compat-util.h | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
git-compat-util.h +62 -0
@@ -782,6 +782,68 @@ extern FILE *fopen_for_writing(const char *path);#define ALLOC_ARRAY (x, alloc) (x) = xmalloc(st_mult((alloc), sizeof(*(x))))#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), st_mult((alloc), sizeof(*(x)))) +/* +* These functions help you allocate structs with flex arrays, and copy +* the data directly into the array. For example, if you had: +* +* struct foo { +* int bar; +* char name[FLEX_ARRAY]; +* }; +* +* you can do: +* +* struct foo *f; +* FLEX_ALLOC_MEM(f, name, src, len); +* +* to allocate a "foo" with the contents of "src" in the "name" field. +* The resulting struct is automatically zero'd, and the flex-array field +* is NUL-terminated (whether the incoming src buffer was or not). +* +* The FLEXPTR_* variants operate on structs that don't use flex-arrays, +* but do want to store a pointer to some extra data in the same allocated +* block. For example, if you have: +* +* struct foo { +* char *name; +* int bar; +* }; +* +* you can do: +* +* struct foo *f; +* FLEX_ALLOC_STR(f, name, src); +* +* and "name" will point to a block of memory after the struct, which will be +* freed along with the struct (but the pointer can be repoined anywhere). +* +* The *_STR variants accept a string parameter rather than a ptr/len +* combination. +* +* Note that these macros will evaluate the first parameter multiple +* times, and it must be assignable as an lvalue. +*/ +#define FLEX_ALLOC_MEM (x, flexname, buf, len) do { \ + (x) = NULL;/* silence -Wuninitialized for offset calculation */ \ + (x) =xalloc_flex (sizeof (*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \ +} while (0) +#define FLEXPTR_ALLOC_MEM (x, ptrname, buf, len) do { \ + (x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \ + (x)->ptrname = (void *)((x)+1); \ +} while(0) +#define FLEX_ALLOC_STR (x, flexname, str) \ + FLEX_ALLOC_MEM((x), flexname, (str), strlen(str)) +#define FLEXPTR_ALLOC_STR (x, ptrname, str) \ + FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str)) + +static inline void *xalloc_flex (size_t base_len,size_t offset, +const void *src,size_t src_len) +{ +unsigned char *ret =xcalloc (1 ,st_add3 (base_len, src_len,1 )); +memcpy (ret + offset, src, src_len); +return ret; +} +static inline char *xstrdup_or_null (const char *str) {return str ?xstrdup (str) :NULL ;
Signature · 1 line
2.7.1.572.gf718037
PATCH 5/18 convert trivial cases to ALLOC_ARRAY
In reply to Jeff KingEach of these cases can be converted to use ALLOC_ARRAY or REALLOC_ARRAY, which has two advantages:
1. It automatically checks the array-size multiplication for overflow.
2. It always uses sizeof(*array) for the element-size, so that it can never go out of sync with the declared type of the array.
--- alias.c | 2 +- attr.c | 2 +- bisect.c | 4 ++-- builtin/blame.c | 3 ++- builtin/clean.c | 2 +- builtin/fast-export.c | 2 +- builtin/grep.c | 3 ++- builtin/index-pack.c | 4 ++-- builtin/merge-base.c | 2 +- builtin/mv.c | 3 ++- builtin/pack-objects.c | 7 ++++--- builtin/pack-redundant.c | 2 +- builtin/receive-pack.c | 7 +++---- builtin/remote-ext.c | 2 +- column.c | 2 +- combine-diff.c | 4 ++-- commit.c | 2 +- compat/mingw.c | 6 +++--- daemon.c | 2 +- diffcore-order.c | 4 ++-- dir.c | 6 +++--- exec_cmd.c | 2 +- fast-import.c | 5 +++-- fsck.c | 3 ++- git.c | 2 +- graph.c | 10 ++++------ khash.h | 2 +- levenshtein.c | 8 +++++--- line-log.c | 10 +++++----- notes.c | 2 +- pack-check.c | 2 +- pack-revindex.c | 12 ++++++++---- pathspec.c | 5 +++-- remote-curl.c | 6 ++++-- run-command.c | 2 +- sha1_file.c | 4 ++-- shallow.c | 6 +++--- show-index.c | 3 ++- transport.c | 2 +- xdiff-interface.c | 2 +- 40 files changed, 86 insertions(+), 73 deletions(-)
alias.c +1 -1
@@ -23,7 +23,7 @@ int split_cmdline(char *cmdline, const char ***argv)int src, dst, count =0 , size =16 ;char quoted =0 ; - *argv =xmalloc (sizeof (**argv) * size); +ALLOC_ARRAY (*argv, size);/* split alias_string */ (*argv)[count++] = cmdline;
attr.c +1 -1
@@ -799,7 +799,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check) ++count; } *num = count; - *check =xmalloc (sizeof (**check) * count); +ALLOC_ARRAY (*check, count); j =0 ;for (i =0 ; i < attr_nr; i++) {const char *value = check_all_attr[i].value;
bisect.c +2 -2
@@ -708,10 +708,10 @@ static struct commit *get_commit_reference(const unsigned char *sha1)static struct commit **get_bad_and_good_commits (int *rev_nr) { -int len =1 + good_revs.nr; -struct commit **rev =xmalloc (len *sizeof (*rev)); +struct commit **rev;int i, n =0 ; +ALLOC_ARRAY (rev,1 + good_revs.nr); rev[n++] =get_commit_reference (current_bad_oid->hash);for (i =0 ; i < good_revs.nr; i++) rev[n++] =get_commit_reference (good_revs.sha1[i]);
builtin/blame.c +2 -1
@@ -2059,7 +2059,8 @@ static int prepare_lines(struct scoreboard *sb)for (p = buf; p < end; p =get_next_line (p, end)) num++; - sb->lineno = lineno =xmalloc (sizeof (*sb->lineno) * (num +1 )); +ALLOC_ARRAY (sb->lineno, num +1 ); + lineno = sb->lineno;for (p = buf; p < end; p =get_next_line (p, end)) *lineno++ = p - buf;
builtin/clean.c +1 -1
@@ -543,7 +543,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)int eof =0 ;int i; - chosen =xmalloc (sizeof (int ) * stuff->nr); +ALLOC_ARRAY (chosen, stuff->nr);/* set chosen as uninitialized */ for (i =0 ; i < stuff->nr; i++) chosen[i] =-1 ;
builtin/fast-export.c +1 -1
@@ -1021,7 +1021,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)const char **refspecs_str;int i; - refspecs_str =xmalloc (sizeof (*refspecs_str) * refspecs_list.nr); +ALLOC_ARRAY (refspecs_str, refspecs_list.nr);for (i =0 ; i < refspecs_list.nr; i++) refspecs_str[i] = refspecs_list.items[i].string;
builtin/grep.c +2 -1
@@ -365,9 +365,10 @@ static void append_path(struct grep_opt *opt, const void *data, size_t len)static void run_pager (struct grep_opt *opt,const char *prefix) {struct string_list *path_list = opt->output_priv; -const char **argv =xmalloc (sizeof (const char *) * (path_list->nr +1 )); +const char **argv;int i, status; +ALLOC_ARRAY (argv, path_list->nr +1 );for (i =0 ; i < path_list->nr; i++) argv[i] = path_list->items[i].string; argv[path_list->nr] =NULL ;
builtin/index-pack.c +2 -2
@@ -1346,7 +1346,7 @@ static void fix_unresolved_deltas(struct sha1file *f)* before deltas depending on them, a good heuristic is to start * resolving deltas in the same order as their position in the pack. */ - sorted_by_pos =xmalloc (nr_ref_deltas *sizeof (*sorted_by_pos)); +ALLOC_ARRAY (sorted_by_pos, nr_ref_deltas);for (i =0 ; i < nr_ref_deltas; i++) sorted_by_pos[i] = &ref_deltas[i];qsort (sorted_by_pos, nr_ref_deltas,sizeof (*sorted_by_pos), delta_pos_compare);
@@ -1759,7 +1759,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)if (show_stat)show_pack_info (stat_only); - idx_objects =xmalloc ((nr_objects) *sizeof (struct pack_idx_entry *)); +ALLOC_ARRAY (idx_objects, nr_objects);for (i =0 ; i < nr_objects; i++) idx_objects[i] = &objects[i].idx; curr_index =write_idx_file (index_name, idx_objects, nr_objects, &opts, pack_sha1);
builtin/merge-base.c +1 -1
@@ -252,7 +252,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)if (argc <2 )usage_with_options (merge_base_usage, options); - rev =xmalloc (argc *sizeof (*rev)); +ALLOC_ARRAY (rev, argc);while (argc-- >0 ) rev[rev_nr++] =get_commit_reference (*argv++);return show_merge_base (rev, rev_nr, show_all);
builtin/mv.c +2 -1
@@ -24,7 +24,8 @@ static const char **internal_copy_pathspec(const char *prefix, int count, unsigned flags) { int i; -const char **result =xmalloc ((count +1 ) *sizeof (const char *)); +const char **result; +ALLOC_ARRAY (result, count +1 );memcpy (result, pathspec, count *sizeof (const char *)); result[count] =NULL ;for (i =0 ; i < count; i++) {
builtin/pack-objects.c +4 -3
@@ -624,7 +624,7 @@ static struct object_entry **compute_write_order(void) {unsigned int i, wo_end, last_untagged; -struct object_entry **wo =xmalloc (to_pack.nr_objects *sizeof (*wo)); +struct object_entry **wo;struct object_entry *objects = to_pack.objects;for (i =0 ; i < to_pack.nr_objects; i++) {
@@ -657,6 +657,7 @@ static struct object_entry **compute_write_order(void)* Give the objects in the original recency order until * we see a tagged tip. */ +ALLOC_ARRAY (wo, to_pack.nr_objects);for (i = wo_end =0 ; i < to_pack.nr_objects; i++) {if (objects[i].tagged)break ;
@@ -769,7 +770,7 @@ static void write_pack_file(void)if (progress > pack_to_stdout) progress_state =start_progress (_ ("Writing objects" ), nr_result); - written_list =xmalloc (to_pack.nr_objects *sizeof (*written_list)); +ALLOC_ARRAY (written_list, to_pack.nr_objects); write_order =compute_write_order (); do {
@@ -2129,7 +2130,7 @@ static void prepare_pack(int window, int depth)if (!to_pack.nr_objects || !window || !depth)return ; - delta_list =xmalloc (to_pack.nr_objects *sizeof (*delta_list)); +ALLOC_ARRAY (delta_list, to_pack.nr_objects); nr_deltas = n =0 ;for (i =0 ; i < to_pack.nr_objects; i++) {
builtin/pack-redundant.c +1 -1
@@ -53,7 +53,7 @@ static inline struct llist_item *llist_item_get(void) free_nodes = free_nodes->next; }else {int i =1 ; - new =xmalloc (sizeof (struct llist_item ) *BLKSIZE ); +ALLOC_ARRAY (new,BLKSIZE );for (; i <BLKSIZE ; i++)llist_item_put (&new[i]); }
builtin/receive-pack.c +3 -4
@@ -1044,7 +1044,7 @@ static void run_update_post_hook(struct command *commands)if (!argc || !hook)return ; - argv =xmalloc (sizeof (*argv) * (2 + argc)); +ALLOC_ARRAY (argv,2 + argc); argv[0 ] = hook;for (argc =1 , cmd = commands; cmd; cmd = cmd->next) {
@@ -1597,8 +1597,7 @@ static void prepare_shallow_update(struct command *commands, {int i, j, k, bitmap_size = (si->ref->nr +31 ) /32 ; - si->used_shallow =xmalloc (sizeof (*si->used_shallow) * - si->shallow->nr); +ALLOC_ARRAY (si->used_shallow, si->shallow->nr);assign_shallow_commits_to_refs (si, si->used_shallow,NULL ); si->need_reachability_test =
@@ -1664,7 +1663,7 @@ static void update_shallow_info(struct command *commands,return ; } - ref_status =xmalloc (sizeof (*ref_status) * ref->nr); +ALLOC_ARRAY (ref_status, ref->nr);assign_shallow_commits_to_refs (si,NULL , ref_status);for (cmd = commands; cmd; cmd = cmd->next) {if (is_null_sha1 (cmd->new_sha1))
builtin/remote-ext.c +1 -1
@@ -133,7 +133,7 @@ static const char **parse_argv(const char *arg, const char *service) temparray[arguments++] = expanded; } - ret =xmalloc ((arguments +1 ) *sizeof (char *)); +ALLOC_ARRAY (ret, arguments +1 );for (i =0 ; i < arguments; i++) ret[i] = temparray[i]; ret[arguments] =NULL ;
column.c +1 -1
@@ -164,7 +164,7 @@ static void display_table(const struct string_list *list, data.colopts = colopts; data.opts = *opts; - data.len =xmalloc (sizeof (*data.len) * list->nr); +ALLOC_ARRAY (data.len, list->nr);for (i =0 ; i < list->nr; i++) data.len[i] =item_length (colopts, list->items[i].string);
combine-diff.c +2 -2
@@ -1372,7 +1372,7 @@ static struct combine_diff_path *find_paths_multitree(struct combine_diff_path paths_head;struct strbuf base; - parents_sha1 =xmalloc (nparent *sizeof (parents_sha1[0 ])); +ALLOC_ARRAY (parents_sha1, nparent);for (i =0 ; i < nparent; i++) parents_sha1[i] = parents->sha1[i];
@@ -1483,7 +1483,7 @@ void diff_tree_combined(const unsigned char *sha1,if (opt->orderfile && num_paths) {struct obj_order *o; - o =xmalloc (sizeof (*o) * num_paths); +ALLOC_ARRAY (o, num_paths);for (i =0 , p = paths; p; p = p->next, i++) o[i].obj = p;order_objects (opt->orderfile, path_path, o, num_paths);
commit.c +1 -1
@@ -903,7 +903,7 @@ static int remove_redundant(struct commit **array, int cnt) work =xcalloc (cnt,sizeof (*work)); redundant =xcalloc (cnt,1 ); - filled_index =xmalloc (sizeof (*filled_index) * (cnt -1 )); +ALLOC_ARRAY (filled_index, cnt -1 );for (i =0 ; i < cnt; i++)parse_commit (array[i]);
compat/mingw.c +3 -3
@@ -854,7 +854,7 @@ static char **get_path_split(void)if (!n)return NULL ; - path =xmalloc ((n+1 )*sizeof (char *)); +ALLOC_ARRAY (path, n+1 ); p = envpath; i =0 ; do {
@@ -939,7 +939,7 @@ static wchar_t *make_environment_block(char **deltaenv) i++;/* copy the environment, leaving space for changes */ - tmpenv =xmalloc ((size + i) *sizeof (char *)); +ALLOC_ARRAY (tmpenv, size + i);memcpy (tmpenv, environ, size *sizeof (char *));/* merge supplied environment changes into the temporary environment */
@@ -1129,7 +1129,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)int argc =0 ;const char **argv2;while (argv[argc]) argc++; - argv2 =xmalloc (sizeof (*argv) * (argc+1 )); +ALLOC_ARRAY (argv2, argc +1 ); argv2[0 ] = (char *)cmd;/* full path to the script file */ memcpy (&argv2[1 ], &argv[1 ],sizeof (*argv) * argc); pid =mingw_spawnv (prog, argv2,1 );
daemon.c +1 -1
@@ -1374,7 +1374,7 @@ int main(int argc, char **argv)write_file (pid_file,"%" PRIuMAX, (uintmax_t )getpid ());/* prepare argv for serving-processes */ - cld_argv =xmalloc (sizeof (char *) * (argc +2 )); +ALLOC_ARRAY (cld_argv, argc +2 ); cld_argv[0 ] = argv[0 ];/* git-daemon */ cld_argv[1 ] ="--serve" ;for (i =1 ; i < argc; ++i)
diffcore-order.c +2 -2
@@ -52,7 +52,7 @@ static void prepare_order(const char *orderfile) }if (pass ==0 ) { order_cnt = cnt; - order =xmalloc (sizeof (*order) * cnt); +ALLOC_ARRAY (order, cnt); } } }
@@ -120,7 +120,7 @@ void diffcore_order(const char *orderfile)if (!q->nr)return ; - o =xmalloc (sizeof (*o) * q->nr); +ALLOC_ARRAY (o, q->nr);for (i =0 ; i < q->nr; i++) o[i].obj = q->queue[i];order_objects (orderfile, pair_pathtwo, o, q->nr);
dir.c +3 -3
@@ -2484,14 +2484,14 @@ static int read_one_dir(struct untracked_cache_dir **untracked_, ud.untracked_alloc = value; ud.untracked_nr = value;if (ud.untracked_nr) - ud.untracked =xmalloc (sizeof (*ud.untracked) * ud.untracked_nr); +ALLOC_ARRAY (ud.untracked, ud.untracked_nr); data = next; next = data; ud.dirs_alloc = ud.dirs_nr =decode_varint (&next);if (next > end)return -1 ; - ud.dirs =xmalloc (sizeof (*ud.dirs) * ud.dirs_nr); +ALLOC_ARRAY (ud.dirs, ud.dirs_nr); data = next; len =strlen ((const char *)data);
@@ -2611,7 +2611,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long rd.data = next; rd.end = end; rd.index =0 ; - rd.ucd =xmalloc (sizeof (*rd.ucd) * len); +ALLOC_ARRAY (rd.ucd, len);if (read_one_dir (&uc->root, &rd) || rd.index != len) goto done;
exec_cmd.c +1 -1
@@ -114,7 +114,7 @@ const char **prepare_git_cmd(const char **argv)for (argc =0 ; argv[argc]; argc++) ;/* just counting */ - nargv =xmalloc (sizeof (*nargv) * (argc +2 )); +ALLOC_ARRAY (nargv, argc +2 ); nargv[0 ] ="git" ;for (argc =0 ; argv[argc]; argc++)
fast-import.c +3 -2
@@ -814,7 +814,8 @@ static struct tree_entry *new_tree_entry(void)if (!avail_tree_entry) {unsigned int n = tree_entry_alloc; total_allocd += n *sizeof (struct tree_entry ); - avail_tree_entry = e =xmalloc (n *sizeof (struct tree_entry )); +ALLOC_ARRAY (e, n); + avail_tree_entry = e;while (n-- >1 ) { *((void **)e) = e +1 ; e++;
@@ -898,7 +899,7 @@ static const char *create_index(void)struct object_entry_pool *o;/* Build the table of object IDs. */ - idx =xmalloc (object_count *sizeof (*idx)); +ALLOC_ARRAY (idx, object_count); c = idx;for (o = blocks; o; o = o->next_pool)for (e = o->next_free; e-- != o->entries;)
fsck.c +2 -1
@@ -199,7 +199,8 @@ void fsck_set_msg_type(struct fsck_options *options,if (!options->msg_type) {int i; -int *msg_type =xmalloc (sizeof (int ) *FSCK_MSG_MAX ); +int *msg_type; +ALLOC_ARRAY (msg_type,FSCK_MSG_MAX );for (i =0 ; i <FSCK_MSG_MAX ; i++) msg_type[i] =fsck_msg_type (i, options); options->msg_type = msg_type;
git.c +1 -1
@@ -249,7 +249,7 @@ static int handle_alias(int *argcp, const char ***argv)restore_env (1 );/* build alias_argv */ - alias_argv =xmalloc (sizeof (*alias_argv) * (argc +1 )); +ALLOC_ARRAY (alias_argv, argc +1 ); alias_argv[0 ] = alias_string +1 ;for (i =1 ; i < argc; ++i) alias_argv[i] = (*argv)[i];
graph.c +4 -6
@@ -234,12 +234,10 @@ struct git_graph *graph_init(struct rev_info *opt)* We'll automatically grow columns later if we need more room. */ graph->column_capacity =30 ; - graph->columns =xmalloc (sizeof (struct column ) * - graph->column_capacity); - graph->new_columns =xmalloc (sizeof (struct column ) * - graph->column_capacity); - graph->mapping =xmalloc (sizeof (int ) *2 * graph->column_capacity); - graph->new_mapping =xmalloc (sizeof (int ) *2 * graph->column_capacity); +ALLOC_ARRAY (graph->columns, graph->column_capacity); +ALLOC_ARRAY (graph->new_columns, graph->column_capacity); +ALLOC_ARRAY (graph->mapping,2 * graph->column_capacity); +ALLOC_ARRAY (graph->new_mapping,2 * graph->column_capacity);/* * The diff output prefix callback, with this we can make
khash.h +1 -1
@@ -117,7 +117,7 @@ static const double __ac_HASH_UPPER = 0.77;if (new_n_buckets <4 ) new_n_buckets =4 ; \if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER +0.5 )) j =0 ;/* requested size is too small */ \else {/* hash table size to be changed (shrink or expand); rehash */ \ - new_flags = (khint32_t *)xmalloc (__ac_fsize (new_n_buckets) *sizeof (khint32_t)); \ +ALLOC_ARRAY (new_flags,__ac_fsize (new_n_buckets)); \if (!new_flags)return -1 ; \memset (new_flags,0xaa ,__ac_fsize (new_n_buckets) *sizeof (khint32_t)); \if (h->n_buckets < new_n_buckets) {/* expand */ \
levenshtein.c +5 -3
@@ -42,11 +42,13 @@ int levenshtein(const char *string1, const char *string2, int w, int s, int a, int d) { int len1 = strlen(string1), len2 = strlen(string2); -int *row0 =xmalloc (sizeof (int ) * (len2 +1 )); -int *row1 =xmalloc (sizeof (int ) * (len2 +1 )); -int *row2 =xmalloc (sizeof (int ) * (len2 +1 )); +int *row0, *row1, *row2;int i, j; +ALLOC_ARRAY (row0, len2 +1 ); +ALLOC_ARRAY (row1, len2 +1 ); +ALLOC_ARRAY (row2, len2 +1 ); +for (j =0 ; j <= len2; j++) row1[j] = j * a;for (i =0 ; i < len1; i++) {
line-log.c +5 -5
@@ -521,7 +521,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,if (diff_populate_filespec (spec,0 ))die ("Cannot read blob %s" ,sha1_to_hex (spec->sha1)); - ends =xmalloc (size *sizeof (*ends)); +ALLOC_ARRAY (ends, size); ends[cur++] =0 ; data = spec->data;while (num < spec->size) {
@@ -753,7 +753,7 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_list count++; r = r->next; } - paths =xmalloc ((count+1 )*sizeof (char *)); +ALLOC_ARRAY (paths, count +1 ); r = range;for (i =0 ; i < count; i++) { paths[i] =xstrdup (r->path);
@@ -1146,9 +1146,9 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *commif (nparents >1 && rev->first_parent_only) nparents =1 ; - diffqueues =xmalloc (nparents *sizeof (*diffqueues)); - cand =xmalloc (nparents *sizeof (*cand)); - parents =xmalloc (nparents *sizeof (*parents)); +ALLOC_ARRAY (diffqueues, nparents); +ALLOC_ARRAY (cand, nparents); +ALLOC_ARRAY (parents, nparents); p = commit->parents;for (i =0 ; i < nparents; i++) {
notes.c +1 -1
@@ -1035,7 +1035,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs, int flags)struct string_list_item *item;int counter =0 ;struct notes_tree **trees; - trees =xmalloc ((refs->nr+1 ) *sizeof (struct notes_tree *)); +ALLOC_ARRAY (trees, refs->nr +1 );for_each_string_list_item (item, refs) {struct notes_tree *t =xcalloc (1 ,sizeof (struct notes_tree ));init_notes (t, item->string, combine_notes_ignore, flags);
pack-check.c +1 -1
@@ -89,7 +89,7 @@ static int verify_packfile(struct packed_git *p,* we do not do scan-streaming check on the pack file. */ nr_objects = p->num_objects; - entries =xmalloc ((nr_objects +1 ) *sizeof (*entries)); +ALLOC_ARRAY (entries, nr_objects +1 ); entries[nr_objects].offset = pack_sig_ofs;/* first sort entries by pack offset, since unpacking them is more efficient that way */ for (i =0 ; i < nr_objects; i++) {
pack-revindex.c +8 -4
@@ -44,10 +44,14 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)* keep track of them with alias pointers, always sorting from "from" * to "to". */ -struct revindex_entry *tmp =xmalloc (n *sizeof (*tmp)); -struct revindex_entry *from = entries, *to = tmp; +struct revindex_entry *tmp, *from, *to;int bits; -unsigned *pos =xmalloc (BUCKETS *sizeof (*pos)); +unsigned *pos; + +ALLOC_ARRAY (pos,BUCKETS ); +ALLOC_ARRAY (tmp, n); + from = entries; + to = tmp;/* * If (max >> bits) is zero, then we know that the radix digit we are
@@ -121,7 +125,7 @@ static void create_pack_revindex(struct packed_git *p)unsigned i;const char *index = p->index_data; - p->revindex =xmalloc (sizeof (*p->revindex) * (num_ent +1 )); +ALLOC_ARRAY (p->revindex, num_ent +1 ); index +=4 *256 ;if (p->index_version >1 ) {
pathspec.c +3 -2
@@ -406,7 +406,8 @@ void parse_pathspec(struct pathspec *pathspec, n++; pathspec->nr = n; - pathspec->items = item =xmalloc (sizeof (*item) * n); +ALLOC_ARRAY (pathspec->items, n); + item = pathspec->items; pathspec->_raw = argv; prefixlen = prefix ?strlen (prefix) :0 ;
@@ -483,7 +484,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)void copy_pathspec (struct pathspec *dst,const struct pathspec *src) { *dst = *src; - dst->items =xmalloc (sizeof (struct pathspec_item ) * dst->nr); +ALLOC_ARRAY (dst->items, dst->nr);memcpy (dst->items, src->items,sizeof (struct pathspec_item ) * dst->nr); }
remote-curl.c +4 -2
@@ -696,9 +696,10 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)static int fetch_dumb (int nr_heads,struct ref **to_fetch) {struct walker *walker; -char **targets =xmalloc (nr_heads *sizeof (char *)); +char **targets;int ret, i; +ALLOC_ARRAY (targets, nr_heads);if (options.depth)die ("dumb http transport does not support --depth" );for (i =0 ; i < nr_heads; i++)
@@ -845,9 +846,10 @@ static void parse_fetch(struct strbuf *buf)static int push_dav (int nr_spec,char **specs) { -const char **argv =xmalloc ((10 + nr_spec) *sizeof (char *)); +const char **argv;int argc =0 , i; +ALLOC_ARRAY (argv,10 + nr_spec); argv[argc++] ="http-push" ; argv[argc++] ="--helper-status" ;if (options.dry_run)
run-command.c +1 -1
@@ -168,7 +168,7 @@ static const char **prepare_shell_cmd(const char **argv)for (argc =0 ; argv[argc]; argc++) ;/* just counting */ /* +1 for NULL, +3 for "sh -c" plus extra $0 */ - nargv =xmalloc (sizeof (*nargv) * (argc +1 +3 )); +ALLOC_ARRAY (nargv, argc +1 +3 );if (argc <1 )die ("BUG: shell command is empty" );
sha1_file.c +2 -2
@@ -1942,7 +1942,7 @@ static enum object_type packed_to_object_type(struct packed_git *p,/* Push the object we're going to leave behind */ if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) { poi_stack_alloc =alloc_nr (poi_stack_nr); - poi_stack =xmalloc (sizeof (off_t)*poi_stack_alloc); +ALLOC_ARRAY (poi_stack, poi_stack_alloc);memcpy (poi_stack, small_poi_stack,sizeof (off_t)*poi_stack_nr); }else {ALLOC_GROW (poi_stack, poi_stack_nr+1 , poi_stack_alloc);
@@ -2308,7 +2308,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,if (delta_stack_nr >= delta_stack_alloc && delta_stack == small_delta_stack) { delta_stack_alloc =alloc_nr (delta_stack_nr); - delta_stack =xmalloc (sizeof (*delta_stack)*delta_stack_alloc); +ALLOC_ARRAY (delta_stack, delta_stack_alloc);memcpy (delta_stack, small_delta_stack,sizeof (*delta_stack)*delta_stack_nr); }else {
shallow.c +3 -3
@@ -315,8 +315,8 @@ void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa) info->shallow = sa;if (!sa)return ; - info->ours =xmalloc (sizeof (*info->ours) * sa->nr); - info->theirs =xmalloc (sizeof (*info->theirs) * sa->nr); +ALLOC_ARRAY (info->ours, sa->nr); +ALLOC_ARRAY (info->theirs, sa->nr);for (i =0 ; i < sa->nr; i++) {if (has_sha1_file (sa->sha1[i])) {struct commit_graft *graft;
@@ -487,7 +487,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,struct paint_info pi;trace_printf_key (&trace_shallow,"shallow: assign_shallow_commits_to_refs\n" ); - shallow =xmalloc (sizeof (*shallow) * (info->nr_ours + info->nr_theirs)); +ALLOC_ARRAY (shallow, info->nr_ours + info->nr_theirs);for (i =0 ; i < info->nr_ours; i++) shallow[nr_shallow++] = info->ours[i];for (i =0 ; i < info->nr_theirs; i++)
show-index.c +2 -1
@@ -50,7 +50,8 @@ int main(int argc, char **argv)unsigned char sha1[20 ];uint32_t crc;uint32_t off; - } *entries =xmalloc (nr *sizeof (entries[0 ])); + } *entries; +ALLOC_ARRAY (entries, nr);for (i =0 ; i < nr; i++)if (fread (entries[i].sha1,20 ,1 , stdin) !=1 )die ("unable to read sha1 %u/%u" , i, nr);
transport.c +1 -1
@@ -1307,7 +1307,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)* This condition shouldn't be met in a non-deepening fetch * (see builtin/fetch.c:quickfetch()). */ - heads =xmalloc (nr_refs *sizeof (*heads)); +ALLOC_ARRAY (heads, nr_refs);for (rm = refs; rm; rm = rm->next) heads[nr_heads++] = rm; }
xdiff-interface.c +1 -1
@@ -265,7 +265,7 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)for (i =0 , regs->nr =1 ; value[i]; i++)if (value[i] =='\n' ) regs->nr++; - regs->array =xmalloc (regs->nr *sizeof (struct ff_reg )); +ALLOC_ARRAY (regs->array, regs->nr);for (i =0 ; i < regs->nr; i++) {struct ff_reg *reg = regs->array + i;const char *ep =strchr (value,'\n' ), *expression;
Signature · 1 line
2.7.1.572.gf718037
PATCH 6/18 use xmallocz to avoid size arithmetic
In reply to Jeff KingWe frequently allocate strings as xmalloc(len + 1), where the extra 1 is for the NUL terminator. This can be done more simply with xmallocz, which also checks for integer overflow.
There's no case where switching xmalloc(n+1) to xmallocz(n) is wrong; the result is the same length, and malloc made no guarantees about what was in the buffer anyway. But in some cases, we can stop manually placing NUL at the end of the allocated buffer. But that's only safe if it's clear that the contents will always fill the buffer.
In each case where this patch does so, I manually examined the control flow, and I tried to err on the side of caution.
--- builtin/check-ref-format.c | 2 +- builtin/merge-tree.c | 2 +- builtin/worktree.c | 2 +- column.c | 3 +-- combine-diff.c | 4 +--- config.c | 4 +--- dir.c | 2 +- entry.c | 2 +- grep.c | 3 +-- imap-send.c | 5 ++--- ll-merge.c | 2 +- progress.c | 2 +- refs.c | 2 +- setup.c | 5 ++--- strbuf.c | 2 +- 15 files changed, 17 insertions(+), 25 deletions(-)
builtin/check-ref-format.c +1 -1
@@ -20,7 +20,7 @@ static const char builtin_check_ref_format_usage[] =*/ static char *collapse_slashes (const char *refname) { -char *ret =xmalloc (strlen (refname) +1 ); +char *ret =xmallocz (strlen (refname));char ch;char prev ='/' ;char *cp = ret;
builtin/merge-tree.c +1 -1
@@ -174,7 +174,7 @@ static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsistatic char *traverse_path (const struct traverse_info *info,const struct name_entry *n) { -char *path =xmalloc (traverse_path_len (info, n) +1 ); +char *path =xmallocz (traverse_path_len (info, n));return make_traverse_path (path, info, n); }
builtin/worktree.c +1 -1
@@ -52,7 +52,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)return 1 ; } len = st.st_size; - path =xmalloc (len +1 ); + path =xmallocz (len);read_in_full (fd, path, len);close (fd);while (len && (path[len -1 ] =='\n' || path[len -1 ] =='\r' ))
column.c +1 -2
@@ -173,9 +173,8 @@ static void display_table(const struct string_list *list,if (colopts &COL_DENSE )shrink_columns (&data); - empty_cell =xmalloc (initial_width +1 ); + empty_cell =xmallocz (initial_width);memset (empty_cell,' ' , initial_width); - empty_cell[initial_width] ='\0' ;for (y =0 ; y < data.rows; y++) {for (x =0 ; x < data.cols; x++)if (display_cell (&data, initial_width, empty_cell, x, y))
combine-diff.c +1 -3
@@ -1043,7 +1043,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, elem->mode =canon_mode (S_IFLNK ); result_size = len; - result =xmalloc (len +1 ); + result =xmallocz (len); done =read_in_full (fd, result, len);if (done <0 )
@@ -1051,8 +1051,6 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,else if (done < len)die ("early EOF '%s'" , elem->path); - result[len] =0 ; -/* If not a fake symlink, apply filters, e.g. autocrlf */ if (is_file) {struct strbuf buf =STRBUF_INIT ;
config.c +1 -3
@@ -1902,7 +1902,7 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *basele* Validate the key and while at it, lower case it for matching. */ if (store_key) - *store_key =xmalloc (strlen (key) +1 ); + *store_key =xmallocz (strlen (key)); dot =0 ;for (i =0 ; key[i]; i++) {
@@ -1926,8 +1926,6 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *baseleif (store_key) (*store_key)[i] = c; } -if (store_key) - (*store_key)[i] =0 ;return 0 ;
dir.c +1 -1
@@ -711,7 +711,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,close (fd);return 0 ; } - buf =xmalloc (size+1 ); + buf =xmallocz (size);if (read_in_full (fd, buf, size) != size) {free (buf);close (fd);
entry.c +1 -1
@@ -6,7 +6,7 @@static void create_directories (const char *path,int path_len,const struct checkout *state) { -char *buf =xmalloc (path_len +1 ); +char *buf =xmallocz (path_len);int len =0 ;while (len < path_len) {
grep.c +1 -2
@@ -1741,7 +1741,7 @@ static int grep_source_load_file(struct grep_source *gs) i =open (filename,O_RDONLY );if (i <0 ) goto err_ret; - data =xmalloc (size +1 ); + data =xmallocz (size);if (st.st_size !=read_in_full (i, data, size)) {error (_ ("'%s': short read %s" ), filename,strerror (errno));close (i);
@@ -1749,7 +1749,6 @@ static int grep_source_load_file(struct grep_source *gs)return -1 ; }close (i); - data[size] =0 ; gs->buf = data; gs->size = size;
imap-send.c +2 -3
@@ -892,12 +892,11 @@ static char *cram(const char *challenge_64, const char *user, const char *pass) response =xstrfmt ("%s %s" , user, hex); resp_len =strlen (response) +1 ; - response_64 =xmalloc (ENCODED_SIZE (resp_len) +1 ); + response_64 =xmallocz (ENCODED_SIZE (resp_len)); encoded_len =EVP_EncodeBlock ((unsigned char *)response_64, (unsigned char *)response, resp_len);if (encoded_len <0 )die ("EVP_EncodeBlock error" ); - response_64[encoded_len] ='\0' ;return (char *)response_64; }
@@ -1188,7 +1187,7 @@ static void lf_to_crlf(struct strbuf *msg) j++; } - new =xmalloc (j +1 ); + new =xmallocz (j);/* * Second pass: write the new string. Note that this loop is
ll-merge.c +1 -1
@@ -205,7 +205,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,if (fstat (fd, &st)) goto close_bad; result->size = st.st_size; - result->ptr =xmalloc (result->size +1 ); + result->ptr =xmallocz (result->size);if (read_in_full (fd, result->ptr, result->size) != result->size) {free (result->ptr); result->ptr =NULL ;
progress.c +1 -1
@@ -247,7 +247,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)size_t len =strlen (msg) +5 ;struct throughput *tp = progress->throughput; - bufp = (len <sizeof (buf)) ? buf :xmalloc (len +1 ); + bufp = (len <sizeof (buf)) ? buf :xmallocz (len);if (tp) {unsigned int rate = !tp->avg_misecs ?0 : tp->avg_bytes / tp->avg_misecs;
refs.c +1 -1
@@ -124,7 +124,7 @@ int refname_is_safe(const char *refname)char *buf;int result; - buf =xmalloc (strlen (refname) +1 ); + buf =xmallocz (strlen (refname));/* * Does the refname try to escape refs/? * For example: refs/foo/../bar is safe but refs/foo/../../bar
setup.c +2 -3
@@ -88,7 +88,7 @@ char *prefix_path_gently(const char *prefix, int len,const char *orig = path;char *sanitized;if (is_absolute_path (orig)) { - sanitized =xmalloc (strlen (path) +1 ); + sanitized =xmallocz (strlen (path));if (remaining_prefix) *remaining_prefix =0 ;if (normalize_path_copy_len (sanitized, path, remaining_prefix)) {
@@ -488,14 +488,13 @@ const char *read_gitfile_gently(const char *path, int *return_error_code) error_code =READ_GITFILE_ERR_OPEN_FAILED ; goto cleanup_return; } - buf =xmalloc (st.st_size +1 ); + buf =xmallocz (st.st_size); len =read_in_full (fd, buf, st.st_size);close (fd);if (len != st.st_size) { error_code =READ_GITFILE_ERR_READ_FAILED ; goto cleanup_return; } - buf[len] ='\0' ;if (!starts_with (buf,"gitdir: " )) { error_code =READ_GITFILE_ERR_INVALID_FORMAT ; goto cleanup_return;
strbuf.c +1 -1
@@ -718,7 +718,7 @@ char *xstrdup_tolower(const char *string)size_t len, i; len =strlen (string); - result =xmalloc (len +1 ); + result =xmallocz (len);for (i =0 ; i < len; i++) result[i] =tolower (string[i]); result[i] ='\0' ;
Signature · 1 line
2.7.1.572.gf718037
PATCH 7/18 convert trivial cases to FLEX_ARRAY macros
In reply to Jeff KingUsing FLEX_ARRAY macros reduces the amount of manual computation size we have to do. It also ensures we don't overflow size_t, and it makes sure we write the same number of bytes that we allocated.
--- attr.c | 4 +--- builtin/blame.c | 4 +--- builtin/help.c | 9 +++------ builtin/mktree.c | 9 +++++---- builtin/reflog.c | 7 ++----- cache-tree.c | 4 +--- combine-diff.c | 4 +--- diff.c | 7 ++----- dir.c | 16 +++------------- hashmap.c | 3 +-- help.c | 6 ++---- log-tree.c | 5 ++--- name-hash.c | 3 +-- ref-filter.c | 6 ++---- refs.c | 6 ++---- refs/files-backend.c | 19 +++++-------------- remote.c | 5 +---- 17 files changed, 35 insertions(+), 82 deletions(-)
attr.c +1 -3
@@ -93,9 +93,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)if (invalid_attr_name (name, len))return NULL ; - a =xmalloc (sizeof (*a) + len +1 ); -memcpy (a->name, name, len); - a->name[len] =0 ; +FLEX_ALLOC_MEM (a, name, name, len); a->h = hval; a->next = git_attr_hash[pos]; a->attr_nr = attr_nr++;
builtin/blame.c +1 -3
@@ -466,13 +466,11 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,static struct origin *make_origin (struct commit *commit,const char *path) {struct origin *o; -size_t pathlen =strlen (path) +1 ; - o =xcalloc (1 ,sizeof (*o) + pathlen); +FLEX_ALLOC_STR (o, path, path); o->commit = commit; o->refcnt =1 ; o->next = commit->util; commit->util = o; -memcpy (o->path, path, pathlen);/* includes NUL */ return o; }
builtin/help.c +3 -6
@@ -171,12 +171,10 @@ static void exec_man_cmd(const char *cmd, const char *page)static void add_man_viewer (const char *name) {struct man_viewer_list **p = &man_viewer_list; -size_t len =strlen (name);while (*p) p = &((*p)->next); - *p =xcalloc (1 , (sizeof (**p) + len +1 )); -memcpy ((*p)->name, name, len);/* NUL-terminated by xcalloc */ +FLEX_ALLOC_STR (*p, name, name); }static int supported_man_viewer (const char *name,size_t len)
@@ -190,9 +188,8 @@ static void do_add_man_viewer_info(const char *name,size_t len, const char *value) { - struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1); - -memcpy (new->name, name, len);/* NUL-terminated by xcalloc */ + struct man_viewer_info_list *new; +FLEX_ALLOC_MEM (new, name, name, len); new->info =xstrdup (value); new->next = man_viewer_info_list; man_viewer_info_list = new;
builtin/mktree.c +5 -4
@@ -19,16 +19,17 @@ static int alloc, used;static void append_to_tree (unsigned mode,unsigned char *sha1,char *path) {struct treeent *ent; -int len =strlen (path); +size_t len =strlen (path);if (strchr (path,'/' ))die ("path %s contains slash" , path); -ALLOC_GROW (entries, used +1 , alloc); - ent = entries[used++] =xmalloc (sizeof (**entries) + len +1 ); +FLEX_ALLOC_MEM (ent, name, path, len); ent->mode = mode; ent->len = len;hashcpy (ent->sha1, sha1); -memcpy (ent->name, path, len+1 ); + +ALLOC_GROW (entries, used +1 , alloc); + entries[used++] = ent; }static int ent_compare (const void *a_,const void *b_)
builtin/reflog.c +2 -5
@@ -382,11 +382,9 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus {struct collected_reflog *e;struct collect_reflog_cb *cb = cb_data; -size_t namelen =strlen (ref); - e =xmalloc (sizeof (*e) + namelen +1 ); +FLEX_ALLOC_STR (e, reflog, ref);hashcpy (e->sha1, oid->hash); -memcpy (e->reflog, ref, namelen +1 );ALLOC_GROW (cb->e, cb->nr +1 , cb->alloc); cb->e[cb->nr++] = e;return 0 ;
@@ -412,8 +410,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) !memcmp(ent->pattern, pattern, len))return ent; - ent =xcalloc (1 , (sizeof (*ent) + len)); -memcpy (ent->pattern, pattern, len); +FLEX_ALLOC_MEM (ent, pattern, pattern, len); ent->len = len; *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);
cache-tree.c +1 -3
@@ -79,11 +79,9 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,ALLOC_GROW (it->down, it->subtree_nr +1 , it->subtree_alloc); it->subtree_nr++; - down =xmalloc (sizeof (*down) + pathlen +1 ); +FLEX_ALLOC_MEM (down, name, path, pathlen); down->cache_tree =NULL ; down->namelen = pathlen; -memcpy (down->name, path, pathlen); - down->name[pathlen] =0 ;if (pos < it->subtree_nr) memmove(it->down + pos + 1,
combine-diff.c +1 -3
@@ -319,7 +319,7 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)if (line[len-1 ] =='\n' ) len--; - lline =xmalloc (sizeof (*lline) + len +1 ); +FLEX_ALLOC_MEM (lline, line, line, len); lline->len = len; lline->next =NULL ; lline->prev = sline->plost.lost_tail;
@@ -330,8 +330,6 @@ static void append_lost(struct sline *sline, int n, const char *line, int len) sline->plost.lost_tail = lline; sline->plost.len++; lline->parent_map = this_mask; -memcpy (lline->line, line, len); - lline->line[len] =0 ; }struct combine_diff_state {
diff.c +2 -5
@@ -2607,12 +2607,9 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,struct diff_filespec *alloc_filespec (const char *path) { -int namelen =strlen (path); -struct diff_filespec *spec =xmalloc (sizeof (*spec) + namelen +1 ); +struct diff_filespec *spec; -memset (spec,0 ,sizeof (*spec)); - spec->path = (char *)(spec +1 ); -memcpy (spec->path, path, namelen+1 ); +FLEXPTR_ALLOC_STR (spec, path, path); spec->count =1 ; spec->is_binary =-1 ;return spec;
dir.c +3 -13
@@ -503,12 +503,7 @@ void add_exclude(const char *string, const char *base,parse_exclude_pattern (&string, &patternlen, &flags, &nowildcardlen);if (flags &EXC_FLAG_MUSTBEDIR ) { -char *s; - x =xmalloc (sizeof (*x) + patternlen +1 ); - s = (char *)(x+1 ); -memcpy (s, string, patternlen); - s[patternlen] ='\0' ; - x->pattern = s; +FLEXPTR_ALLOC_MEM (x, pattern, string, patternlen); }else { x =xmalloc (sizeof (*x)); x->pattern = string;
@@ -625,10 +620,7 @@ static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, } uc->dir_created++; - d =xmalloc (sizeof (*d) + len +1 ); -memset (d,0 ,sizeof (*d)); -memcpy (d->name, name, len); - d->name[len] ='\0' ; +FLEX_ALLOC_MEM (d, name, name, len);ALLOC_GROW (dir->dirs, dir->dirs_nr +1 , dir->dirs_alloc); memmove(dir->dirs + first + 1, dir->dirs + first,
@@ -1167,10 +1159,8 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len) {struct dir_entry *ent; - ent =xmalloc (sizeof (*ent) + len +1 ); +FLEX_ALLOC_MEM (ent, name, pathname, len); ent->len = len; -memcpy (ent->name, pathname, len); - ent->name[len] =0 ;return ent; }
hashmap.c +1 -2
@@ -256,10 +256,9 @@ const void *memintern(const void *data, size_t len) e =hashmap_get (&map, &key, data);if (!e) {/* not found: create it */ - e =xmallocz (sizeof (struct pool_entry ) + len); +FLEX_ALLOC_MEM (e, data, data, len);hashmap_entry_init (e, key.ent.hash); e->len = len; -memcpy (e->data, data, len);hashmap_add (&map, e); }return e->data;
help.c +2 -4
@@ -11,11 +11,9 @@void add_cmdname (struct cmdnames *cmds,const char *name,int len) { -struct cmdname *ent =xmalloc (sizeof (*ent) + len +1 ); - +struct cmdname *ent; +FLEX_ALLOC_MEM (ent, name, name, len); ent->len = len; -memcpy (ent->name, name, len); - ent->name[len] =0 ;ALLOC_GROW (cmds->names, cmds->cnt +1 , cmds->alloc); cmds->names[cmds->cnt++] = ent;
log-tree.c +2 -3
@@ -77,9 +77,8 @@ int parse_decorate_color_config(const char *var, const char *slot_name, const chvoid add_name_decoration (enum decoration_type type,const char *name,struct object *obj) { -int nlen =strlen (name); -struct name_decoration *res =xmalloc (sizeof (*res) + nlen +1 ); -memcpy (res->name, name, nlen +1 ); +struct name_decoration *res; +FLEX_ALLOC_STR (res, name, name); res->type = type; res->next =add_decoration (&name_decoration, obj, res); }
name-hash.c +1 -2
@@ -55,10 +55,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate, dir =find_dir_entry (istate, ce->name, namelen);if (!dir) {/* not found, create it and add to hash table */ - dir =xcalloc (1 ,sizeof (struct dir_entry ) + namelen +1 ); +FLEX_ALLOC_MEM (dir, name, ce->name, namelen);hashmap_entry_init (dir,memihash (ce->name, namelen)); dir->namelen = namelen; -strncpy (dir->name, ce->name, namelen);hashmap_add (&istate->dir_hash, dir);/* recursively add missing parent directories */
ref-filter.c +2 -4
@@ -1255,10 +1255,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname, const unsigned char *objectname, int flag) { - size_t len = strlen(refname); -struct ref_array_item *ref =xcalloc (1 ,sizeof (struct ref_array_item ) + len +1 ); -memcpy (ref->refname, refname, len); - ref->refname[len] ='\0' ; +struct ref_array_item *ref; +FLEX_ALLOC_STR (ref, refname, refname);hashcpy (ref->objectname, objectname); ref->flag = flag;
refs.c +2 -4
@@ -761,10 +761,8 @@ void ref_transaction_free(struct ref_transaction *transaction)static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) { -size_t len =strlen (refname) +1 ; -struct ref_update *update =xcalloc (1 ,sizeof (*update) + len); - -memcpy ((char *)update->refname, refname, len);/* includes NUL */ +struct ref_update *update; +FLEX_ALLOC_STR (update, refname, refname);ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); transaction->updates[transaction->nr++] = update;return update;
refs/files-backend.c +5 -14
@@ -199,17 +199,14 @@ static struct ref_entry *create_ref_entry(const char *refname, const unsigned char *sha1, int flag, int check_name) { - int len; struct ref_entry *ref;if (check_name &&check_refname_format (refname,REFNAME_ALLOW_ONELEVEL ))die ("Reference has invalid format: '%s'" , refname); - len =strlen (refname) +1 ; - ref =xmalloc (sizeof (struct ref_entry ) + len); +FLEX_ALLOC_STR (ref, name, refname);hashcpy (ref->u.value.oid.hash, sha1);oidclr (&ref->u.value.peeled); -memcpy (ref->name, refname, len); ref->flag = flag;return ref; }
@@ -268,9 +265,7 @@ static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache, int incomplete) {struct ref_entry *direntry; - direntry =xcalloc (1 ,sizeof (struct ref_entry ) + len +1 ); -memcpy (direntry->name, dirname, len); - direntry->name[len] ='\0' ; +FLEX_ALLOC_MEM (direntry, name, dirname, len); direntry->u.subdir.ref_cache = ref_cache; direntry->flag =REF_DIR | (incomplete ?REF_INCOMPLETE :0 );return direntry;
@@ -939,13 +934,10 @@ static void clear_loose_ref_cache(struct ref_cache *refs)*/ static struct ref_cache *create_ref_cache (const char *submodule) { -int len;struct ref_cache *refs;if (!submodule) submodule ="" ; - len =strlen (submodule) +1 ; - refs =xcalloc (1 ,sizeof (struct ref_cache ) + len); -memcpy (refs->name, submodule, len); +FLEX_ALLOC_STR (refs, name, submodule); refs->next = submodule_ref_caches; submodule_ref_caches = refs;return refs;
@@ -2197,10 +2189,9 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)/* Schedule the loose reference for pruning if requested. */ if ((cb->flags &PACK_REFS_PRUNE )) { -int namelen =strlen (entry->name) +1 ; -struct ref_to_prune *n =xcalloc (1 ,sizeof (*n) + namelen); +struct ref_to_prune *n; +FLEX_ALLOC_STR (n, name, entry->name);hashcpy (n->sha1, entry->u.value.oid.hash); -memcpy (n->name, entry->name, namelen);/* includes NUL */ n->next = cb->ref_to_prune; cb->ref_to_prune = n; }
remote.c +1 -4
@@ -2136,16 +2136,13 @@ static int one_local_ref(const char *refname, const struct object_id *oid, {struct ref ***local_tail = cb_data;struct ref *ref; -int len;/* we already know it starts with refs/ to get here */ if (check_refname_format (refname +5 ,0 ))return 0 ; - len =strlen (refname) +1 ; - ref =xcalloc (1 ,sizeof (*ref) + len); + ref =alloc_ref (refname);oidcpy (&ref->new_oid, oid); -memcpy (ref->name, refname, len); **local_tail = ref; *local_tail = &ref->next;return 0 ;
Signature · 1 line
2.7.1.572.gf718037
PATCH 8/18 use st_add and st_mult for allocation size computation
In reply to Jeff KingIf our size computation overflows size_t, we may allocate a much smaller buffer than we expected and overflow it. It's probably impossible to trigger an overflow in most of these sites in practice, but it is easy enough convert their additions and multiplications into overflow-checking variants. This may be fixing real bugs, and it makes auditing the code easier.
--- archive.c | 4 ++-- builtin/apply.c | 2 +- builtin/clean.c | 2 +- builtin/fetch.c | 2 +- builtin/index-pack.c | 4 ++-- builtin/merge.c | 2 +- builtin/mv.c | 4 ++-- builtin/receive-pack.c | 2 +- combine-diff.c | 14 +++++++------- commit.c | 2 +- compat/mingw.c | 4 ++-- compat/qsort.c | 2 +- compat/setenv.c | 2 +- compat/win32/syslog.c | 4 ++-- diffcore-delta.c | 6 ++++-- diffcore-rename.c | 2 +- dir.c | 4 ++-- fast-import.c | 2 +- refs.c | 2 +- remote.c | 8 ++++---- revision.c | 2 +- sha1_file.c | 20 +++++++++++--------- sha1_name.c | 5 ++--- shallow.c | 2 +- submodule.c | 6 +++--- 25 files changed, 56 insertions(+), 53 deletions(-)
archive.c +2 -2
@@ -171,8 +171,8 @@ static void queue_directory(const unsigned char *sha1, unsigned mode, int stage, struct archiver_context *c) { struct directory *d; -size_t len = base->len +1 +strlen (filename) +1 ; - d =xmalloc (sizeof (*d) + len); +size_t len =st_add4 (base->len,1 ,strlen (filename),1 ); + d =xmalloc (st_add (sizeof (*d), len)); d->up = c->bottom; d->baselen = base->len; d->mode = mode;
builtin/apply.c +1 -1
@@ -2632,7 +2632,7 @@ static void update_image(struct image *img, insert_count = postimage->len;/* Adjust the contents */ - result =xmalloc (img->len + insert_count - remove_count +1 ); + result =xmalloc (st_add3 (st_sub (img->len, remove_count), insert_count,1 ));memcpy (result, img->buf, applied_at);memcpy (result + applied_at, postimage->buf, postimage->len); memcpy(result + applied_at + postimage->len,
builtin/clean.c +1 -1
@@ -615,7 +615,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff) nr += chosen[i]; } - result =xcalloc (nr +1 ,sizeof (int )); + result =xcalloc (st_add (nr,1 ),sizeof (int ));for (i =0 ; i < stuff->nr && j < nr; i++) {if (chosen[i]) result[j++] = i;
builtin/fetch.c +1 -1
@@ -1110,7 +1110,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)if (argc >0 ) {int j =0 ;int i; - refs =xcalloc (argc +1 ,sizeof (const char *)); + refs =xcalloc (st_add (argc,1 ),sizeof (const char *));for (i =0 ; i < argc; i++) {if (!strcmp (argv[i],"tag" )) { i++;
builtin/index-pack.c +2 -2
@@ -1744,9 +1744,9 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix) curr_pack =open_pack_file (pack_name);parse_pack_header (); - objects =xcalloc (nr_objects +1 ,sizeof (struct object_entry )); + objects =xcalloc (st_add (nr_objects,1 ),sizeof (struct object_entry ));if (show_stat) - obj_stat =xcalloc (nr_objects +1 ,sizeof (struct object_stat )); + obj_stat =xcalloc (st_add (nr_objects,1 ),sizeof (struct object_stat )); ofs_deltas =xcalloc (nr_objects,sizeof (struct ofs_delta_entry ));parse_pack_objects (pack_sha1);resolve_deltas ();
builtin/merge.c +1 -1
@@ -939,7 +939,7 @@ static int setup_with_upstream(const char ***argv)if (!branch->merge_nr)die (_ ("No default upstream defined for the current branch." )); - args =xcalloc (branch->merge_nr +1 ,sizeof (char *)); + args =xcalloc (st_add (branch->merge_nr,1 ),sizeof (char *));for (i =0 ; i < branch->merge_nr; i++) {if (!branch->merge[i]->dst) die(_("No remote-tracking branch for %s from %s"),
builtin/mv.c +2 -2
@@ -48,9 +48,9 @@ static const char **internal_copy_pathspec(const char *prefix,static const char *add_slash (const char *path) { -int len =strlen (path); +size_t len =strlen (path);if (path[len -1 ] !='/' ) { -char *with_slash =xmalloc (len +2 ); +char *with_slash =xmalloc (st_add (len,2 ));memcpy (with_slash, path, len); with_slash[len++] ='/' ; with_slash[len] =0 ;
builtin/receive-pack.c +1 -1
@@ -1378,7 +1378,7 @@ static struct command **queue_command(struct command **tail, refname = line +82 ; reflen = linelen -82 ; - cmd =xcalloc (1 ,sizeof (struct command ) + reflen +1 ); + cmd =xcalloc (1 ,st_add3 (sizeof (struct command ), reflen,1 ));hashcpy (cmd->old_sha1, old_sha1);hashcpy (cmd->new_sha1, new_sha1);memcpy (cmd->ref_name, refname, reflen);
combine-diff.c +7 -7
@@ -189,11 +189,11 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,* - Else if we have NEW, insert newend lline into base and * consume newend */ - lcs =xcalloc (origbaselen +1 ,sizeof (int *)); - directions =xcalloc (origbaselen +1 ,sizeof (enum coalesce_direction *)); + lcs =xcalloc (st_add (origbaselen,1 ),sizeof (int *)); + directions =xcalloc (st_add (origbaselen,1 ),sizeof (enum coalesce_direction *));for (i =0 ; i < origbaselen +1 ; i++) { - lcs[i] =xcalloc (lennew +1 ,sizeof (int )); - directions[i] =xcalloc (lennew +1 ,sizeof (enum coalesce_direction )); + lcs[i] =xcalloc (st_add (lennew,1 ),sizeof (int )); + directions[i] =xcalloc (st_add (lennew,1 ),sizeof (enum coalesce_direction )); directions[i][0 ] =BASE ; }for (j =1 ; j < lennew +1 ; j++)
@@ -1111,7 +1111,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,if (result_size && result[result_size-1 ] !='\n' ) cnt++;/* incomplete line */ - sline =xcalloc (cnt+2 ,sizeof (*sline)); + sline =xcalloc (st_add (cnt,2 ),sizeof (*sline)); sline[0 ].bol = result;for (lno =0 , cp = result; cp < result + result_size; cp++) {if (*cp =='\n' ) {
@@ -1130,7 +1130,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,/* Even p_lno[cnt+1] is valid -- that is for the end line number * for deletion hunk at the end. */ - sline[0 ].p_lno =xcalloc ((cnt+2 ) * num_parent,sizeof (unsigned long )); + sline[0 ].p_lno =xcalloc (st_mult (st_add (cnt,2 ), num_parent),sizeof (unsigned long ));for (lno =0 ; lno <= cnt; lno++) sline[lno+1 ].p_lno = sline[lno].p_lno + num_parent;
@@ -1262,7 +1262,7 @@ static struct diff_filepair *combined_pair(struct combine_diff_path *p,struct diff_filespec *pool; pair =xmalloc (sizeof (*pair)); - pool =xcalloc (num_parent +1 ,sizeof (struct diff_filespec )); + pool =xcalloc (st_add (num_parent,1 ),sizeof (struct diff_filespec )); pair->one = pool +1 ; pair->two = pool;
commit.c +1 -1
@@ -147,7 +147,7 @@ struct commit_graft *read_graft_line(char *buf, int len)if ((len +1 ) % entry_size) goto bad_graft_data; i = (len +1 ) / entry_size -1 ; - graft =xmalloc (sizeof (*graft) +GIT_SHA1_RAWSZ * i); + graft =xmalloc (st_add (sizeof (*graft),st_mult (GIT_SHA1_RAWSZ , i))); graft->nr_parent = i;if (get_oid_hex (buf, &graft->oid)) goto bad_graft_data;
compat/mingw.c +2 -2
@@ -771,7 +771,7 @@ static const char *quote_arg(const char *arg)return arg;/* insert \ where necessary */ - d = q =xmalloc (len+n+3 ); + d = q =xmalloc (st_add3 (len, n,3 )); *d++ ='"' ;while (*arg) {if (*arg =='"' )
@@ -1030,7 +1030,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenfree (quoted); } - wargs =xmalloc ((2 * args.len +1 ) *sizeof (wchar_t)); + wargs =xmalloc_array (st_add (st_mult (2 , args.len),1 ),sizeof (wchar_t));xutftowcs (wargs, args.buf,2 * args.len +1 );strbuf_release (&args);
compat/qsort.c +1 -1
@@ -47,7 +47,7 @@ static void msort_with_tmp(void *b, size_t n, size_t s,void git_qsort (void *b,size_t n,size_t s,int (*cmp)(const void *,const void *)) { -const size_t size = n * s; +const size_t size =st_mult (n, s);char buf[1024 ];if (size <sizeof (buf)) {
compat/setenv.c +1 -1
@@ -18,7 +18,7 @@ int gitsetenv(const char *name, const char *value, int replace) namelen =strlen (name); valuelen =strlen (value); - envstr =malloc ((namelen + valuelen +2 )); + envstr =malloc (st_add3 (namelen, valuelen,2 ));if (!envstr) { errno =ENOMEM ;return -1 ;
compat/win32/syslog.c +2 -2
@@ -32,7 +32,7 @@ void syslog(int priority, const char *fmt, ...)return ; } - str =malloc (str_len +1 ); + str =malloc (st_add (str_len,1 ));if (!str) {warning ("malloc failed: '%s'" ,strerror (errno));return ;
@@ -43,7 +43,7 @@ void syslog(int priority, const char *fmt, ...)va_end (ap);while ((pos =strstr (str,"%1" )) !=NULL ) { - str =realloc (str, ++str_len +1 ); + str =realloc (str,st_add (++str_len,1 ));if (!str) {warning ("realloc failed: '%s'" ,strerror (errno));return ;
diffcore-delta.c +4 -2
@@ -53,7 +53,8 @@ static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)int osz =1 << orig->alloc_log2;int sz = osz <<1 ; - new =xmalloc (sizeof (*orig) +sizeof (struct spanhash ) * sz); + new =xmalloc (st_add (sizeof (*orig), +st_mult (sizeof (struct spanhash ), sz))); new->alloc_log2 = orig->alloc_log2 +1 ; new->free =INITIAL_FREE (new->alloc_log2);memset (new->data,0 ,sizeof (struct spanhash ) * sz);
@@ -130,7 +131,8 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)int is_text = !diff_filespec_is_binary (one); i =INITIAL_HASH_SIZE ; - hash =xmalloc (sizeof (*hash) +sizeof (struct spanhash ) * (1 <<i)); + hash =xmalloc (st_add (sizeof (*hash), +st_mult (sizeof (struct spanhash ),1 <<i))); hash->alloc_log2 = i; hash->free =INITIAL_FREE (i);memset (hash->data,0 ,sizeof (struct spanhash ) * (1 <<i));
diffcore-rename.c +1 -1
@@ -537,7 +537,7 @@ void diffcore_rename(struct diff_options *options) rename_dst_nr * rename_src_nr, 50, 1); } - mx =xcalloc (num_create *NUM_CANDIDATE_PER_DST ,sizeof (*mx)); + mx =xcalloc (st_mult (num_create,NUM_CANDIDATE_PER_DST ),sizeof (*mx));for (dst_cnt = i =0 ; i < rename_dst_nr; i++) {struct diff_filespec *two = rename_dst[i].two;struct diff_score *m;
dir.c +2 -2
@@ -689,7 +689,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,return 0 ; }if (buf[size-1 ] !='\n' ) { - buf =xrealloc (buf, size+1 ); + buf =xrealloc (buf,st_add (size,1 )); buf[size++] ='\n' ; } }else {
@@ -2488,7 +2488,7 @@ static int read_one_dir(struct untracked_cache_dir **untracked_, next = data + len +1 ;if (next > rd->end)return -1 ; - *untracked_ = untracked =xmalloc (sizeof (*untracked) + len); + *untracked_ = untracked =xmalloc (st_add (sizeof (*untracked), len));memcpy (untracked, &ud,sizeof (ud));memcpy (untracked->name, data, len +1 ); data = next;
fast-import.c +1 -1
@@ -622,7 +622,7 @@ static void *pool_alloc(size_t len)return xmalloc (len); } total_allocd +=sizeof (struct mem_pool ) + mem_pool_alloc; - p =xmalloc (sizeof (struct mem_pool ) + mem_pool_alloc); + p =xmalloc (st_add (sizeof (struct mem_pool ), mem_pool_alloc)); p->next_pool = mem_pool; p->next_free = (char *) p->space; p->end = p->next_free + mem_pool_alloc;
refs.c +1 -1
@@ -906,7 +906,7 @@ char *shorten_unambiguous_ref(const char *refname, int strict)/* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */ total_len +=strlen (ref_rev_parse_rules[nr_rules]) -2 +1 ; - scanf_fmts =xmalloc (nr_rules *sizeof (char *) + total_len); + scanf_fmts =xmalloc (st_add (st_mult (nr_rules,sizeof (char *)), total_len)); offset =0 ;for (i =0 ; i < nr_rules; i++) {
remote.c +4 -4
@@ -931,7 +931,7 @@ static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, const char *name) {size_t len =strlen (name); -struct ref *ref =xcalloc (1 ,sizeof (struct ref ) + prefixlen + len +1 ); +struct ref *ref =xcalloc (1 ,st_add4 (sizeof (*ref), prefixlen, len,1 ));memcpy (ref->name, prefix, prefixlen);memcpy (ref->name + prefixlen, name, len);return ref;
@@ -948,9 +948,9 @@ struct ref *copy_ref(const struct ref *ref)size_t len;if (!ref)return NULL ; - len =strlen (ref->name); - cpy =xmalloc (sizeof (struct ref ) + len +1 ); -memcpy (cpy, ref,sizeof (struct ref ) + len +1 ); + len =st_add3 (sizeof (struct ref ),strlen (ref->name),1 ); + cpy =xmalloc (len); +memcpy (cpy, ref, len); cpy->next =NULL ; cpy->symref =xstrdup_or_null (ref->symref); cpy->remote_status =xstrdup_or_null (ref->remote_status);
revision.c +1 -1
@@ -540,7 +540,7 @@ struct treesame_state {static struct treesame_state *initialise_treesame (struct rev_info *revs,struct commit *commit) {unsigned n =commit_list_count (commit->parents); -struct treesame_state *st =xcalloc (1 ,sizeof (*st) + n); +struct treesame_state *st =xcalloc (1 ,st_add (sizeof (*st), n)); st->nparents = n;add_decoration (&revs->treesame, &commit->object, st);return st;
sha1_file.c +11 -9
@@ -253,7 +253,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base, {struct alternate_object_database *ent;struct alternate_object_database *alt; -int pfxlen, entlen; +size_t pfxlen, entlen;struct strbuf pathbuf =STRBUF_INIT ;if (!is_absolute_path (entry) && relative_base) {
@@ -273,8 +273,8 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,while (pfxlen && pathbuf.buf[pfxlen-1 ] =='/' ) pfxlen -=1 ; - entlen = pfxlen +43 ;/* '/' + 2 hex + '/' + 38 hex + NUL */ - ent =xmalloc (sizeof (*ent) + entlen); + entlen =st_add (pfxlen,43 );/* '/' + 2 hex + '/' + 38 hex + NUL */ + ent =xmalloc (st_add (sizeof (*ent), entlen));memcpy (ent->base, pathbuf.buf, pfxlen);strbuf_release (&pathbuf);
@@ -1134,7 +1134,7 @@ unsigned char *use_pack(struct packed_git *p,static struct packed_git *alloc_packed_git (int extra) { -struct packed_git *p =xmalloc (sizeof (*p) + extra); +struct packed_git *p =xmalloc (st_add (sizeof (*p), extra));memset (p,0 ,sizeof (*p)); p->pack_fd =-1 ;return p;
@@ -1168,7 +1168,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)* ".pack" is long enough to hold any suffix we're adding (and * the use xsnprintf double-checks that) */ - alloc = path_len +strlen (".pack" ) +1 ; + alloc =st_add3 (path_len,strlen (".pack" ),1 ); p =alloc_packed_git (alloc);memcpy (p->pack_name, path, path_len);
@@ -1196,7 +1196,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)struct packed_git *parse_pack_index (unsigned char *sha1,const char *idx_path) {const char *path =sha1_pack_name (sha1); -int alloc =strlen (path) +1 ; +size_t alloc =st_add (strlen (path),1 );struct packed_git *p =alloc_packed_git (alloc);memcpy (p->pack_name, path, alloc);/* includes NUL */
@@ -1413,10 +1413,12 @@ static void mark_bad_packed_object(struct packed_git *p, {unsigned i;for (i =0 ; i < p->num_bad_objects; i++) -if (!hashcmp (sha1, p->bad_object_sha1 +20 * i)) +if (!hashcmp (sha1, p->bad_object_sha1 +GIT_SHA1_RAWSZ * i))return ; - p->bad_object_sha1 =xrealloc (p->bad_object_sha1,20 * (p->num_bad_objects +1 )); -hashcpy (p->bad_object_sha1 +20 * p->num_bad_objects, sha1); + p->bad_object_sha1 =xrealloc (p->bad_object_sha1, +st_mult (GIT_SHA1_RAWSZ , +st_add (p->num_bad_objects,1 ))); +hashcpy (p->bad_object_sha1 +GIT_SHA1_RAWSZ * p->num_bad_objects, sha1); p->num_bad_objects++; }
sha1_name.c +2 -3
@@ -87,9 +87,8 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa* object databases including our own. */ const char *objdir =get_object_directory (); -int objdir_len =strlen (objdir); -int entlen = objdir_len +43 ; - fakeent =xmalloc (sizeof (*fakeent) + entlen); +size_t objdir_len =strlen (objdir); + fakeent =xmalloc (st_add3 (sizeof (*fakeent), objdir_len,43 ));memcpy (fakeent->base, objdir, objdir_len); fakeent->name = fakeent->base + objdir_len +1 ; fakeent->name[-1 ] ='/' ;
shallow.c +1 -1
@@ -389,7 +389,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,unsigned int i, nr;struct commit_list *head =NULL ;int bitmap_nr = (info->nr_bits +31 ) /32 ; -int bitmap_size = bitmap_nr *sizeof (uint32_t ); +size_t bitmap_size =st_mult (bitmap_nr,sizeof (uint32_t ));uint32_t *tmp =xmalloc (bitmap_size);/* to be freed before return */ uint32_t *bitmap =paint_alloc (info);struct commit *c =lookup_commit_reference_gently (sha1,1 );
submodule.c +3 -3
@@ -123,7 +123,7 @@ static int add_submodule_odb(const char *path)struct strbuf objects_directory =STRBUF_INIT ;struct alternate_object_database *alt_odb;int ret =0 ; -int alloc; +size_t alloc;strbuf_git_path_submodule (&objects_directory, path,"objects/" );if (!is_directory (objects_directory.buf)) {
@@ -138,8 +138,8 @@ static int add_submodule_odb(const char *path) objects_directory.len)) goto done; - alloc = objects_directory.len +42 ;/* for "12/345..." sha1 */ - alt_odb =xmalloc (sizeof (*alt_odb) + alloc); + alloc =st_add (objects_directory.len,42 );/* for "12/345..." sha1 */ + alt_odb =xmalloc (st_add (sizeof (*alt_odb), alloc)); alt_odb->next = alt_odb_list;xsnprintf (alt_odb->base, alloc,"%s" , objects_directory.buf); alt_odb->name = alt_odb->base + objects_directory.len;
Signature · 1 line
2.7.1.572.gf718037
PATCH 9/18 write_untracked_extension: use FLEX_ALLOC helper
In reply to Jeff KingWe perform unchecked additions when computing the size of a "struct ondisk_untracked_cache". This is unlikely to have an integer overflow in practice, but we'd like to avoid this dangerous pattern to make further audits easier.
Note that there's one subtlety here, though. We protect ourselves against a NULL exclude_per_dir entry in our source, and avoid calling strlen() on it, keeping "len" at
0. But later, we unconditionally memcpy "len + 1" bytes to get the trailing NUL byte. If we did have a NULL exclude_per_dir, we would read from bogus memory.
As it turns out, though, we always create this field pointing to a string literal, so there's no bug. We can just get rid of the pointless extra conditional.
--- dir.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
dir.c +4 -5
@@ -2360,16 +2360,15 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untrastruct ondisk_untracked_cache *ouc;struct write_data wd;unsigned char varbuf[16 ]; -int len =0 , varint_len; -if (untracked->exclude_per_dir) - len =strlen (untracked->exclude_per_dir); - ouc =xmalloc (sizeof (*ouc) + len +1 ); +int varint_len; +size_t len =strlen (untracked->exclude_per_dir); + +FLEX_ALLOC_MEM (ouc, exclude_per_dir, untracked->exclude_per_dir, len);stat_data_to_disk (&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);stat_data_to_disk (&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);hashcpy (ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);hashcpy (ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1); ouc->dir_flags =htonl (untracked->dir_flags); -memcpy (ouc->exclude_per_dir, untracked->exclude_per_dir, len +1 ); varint_len =encode_varint (untracked->ident.len, varbuf);strbuf_add (out, varbuf, varint_len);
Signature · 1 line
2.7.1.572.gf718037
PATCH 10/18 fast-import: simplify allocation in start_packfile
In reply to Jeff KingThis function allocates a packed_git flex-array, and adds a mysterious 2 bytes to the length of the pack_name field. One is for the trailing NUL, but the other has no purpose. This is probably cargo-culted from add_packed_git, which gets the ".idx" path and needs to allocate enough space to hold the matching ".pack" (though since 48bcc1c, we calculate the size there differently).
This site, however, is using the raw path of a tempfile, and does not need the extra byte. We can just replace the allocation with FLEX_ALLOC_STR, which handles the allocation and the NUL for us.
--- fast-import.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
fast-import.c +1 -4
@@ -865,15 +865,12 @@ static void start_packfile(void) {static char tmp_file[PATH_MAX ];struct packed_git *p; -int namelen;struct pack_header hdr;int pack_fd; pack_fd =odb_mkstemp (tmp_file,sizeof (tmp_file),"pack/tmp_pack_XXXXXX" ); - namelen =strlen (tmp_file) +2 ; - p =xcalloc (1 ,sizeof (*p) + namelen); -xsnprintf (p->pack_name, namelen,"%s" , tmp_file); +FLEX_ALLOC_STR (p, pack_name, tmp_file); p->pack_fd = pack_fd; p->do_not_close =1 ; pack_file =sha1fd (pack_fd, p->pack_name);
Signature · 1 line
2.7.1.572.gf718037
PATCH 11/18 fetch-pack: simplify add_sought_entry
In reply to Jeff KingWe have two variants of this function, one that takes a string and one that takes a ptr/len combo. But we only call the latter with the length of a NUL-terminated string, so our first simplification is to drop it in favor of the string variant.
Since we know we have a string, we can also replace the manual memory computation with a call to alloc_ref().
Furthermore, we can rely on get_oid_hex() to complain if it hits the end of the string. That means we can simplify the check for "<sha1> <ref>" versus just "<ref>". Rather than manage the ptr/len pair, we can just bump the start of our string forward. The original code over-allocated based on the original "namelen" (which wasn't _wrong_, but was simply wasteful and confusing).
--- builtin/fetch-pack.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
builtin/fetch-pack.c +9 -18
@@ -10,33 +10,24 @@ static const char fetch_pack_usage[] ="[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] " "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]" ; -static void add_sought_entry_mem (struct ref ***sought,int *nr,int *alloc, -const char *name,int namelen) +static void add_sought_entry (struct ref ***sought,int *nr,int *alloc, +const char *name) { -struct ref *ref =xcalloc (1 ,sizeof (*ref) + namelen +1 ); +struct ref *ref;struct object_id oid; -const int chunksz =GIT_SHA1_HEXSZ +1 ; -if (namelen > chunksz && name[chunksz -1 ] ==' ' && - !get_oid_hex (name, &oid)) { -oidcpy (&ref->old_oid, &oid); - name += chunksz; - namelen -= chunksz; - } +if (!get_oid_hex (name, &oid) && name[GIT_SHA1_HEXSZ ] ==' ' ) + name +=GIT_SHA1_HEXSZ +1 ; +else +oidclr (&oid); -memcpy (ref->name, name, namelen); - ref->name[namelen] ='\0' ; + ref =alloc_ref (name); +oidcpy (&ref->old_oid, &oid); (*nr)++;ALLOC_GROW (*sought, *nr, *alloc); (*sought)[*nr -1 ] = ref; } -static void add_sought_entry (struct ref ***sought,int *nr,int *alloc, -const char *string) -{ -add_sought_entry_mem (sought, nr, alloc, string,strlen (string)); -} -int cmd_fetch_pack (int argc,const char **argv,const char *prefix) {int i, ret;
Signature · 1 line
2.7.1.572.gf718037
PATCH 12/18 test-path-utils: fix normalize_path_copy output buffer size
In reply to Jeff KingThe normalize_path_copy function needs an output buffer that is at least as long as its input (it may shrink the path, but never expand it). However, this test program feeds it static PATH_MAX-sized buffers, which have no relation to the input size.
In the normalize_ceiling_entry case, we do at least check the size against PATH_MAX and die(), but that case is even more convoluted. We normalize into a fixed-size buffer, free the original, and then replace it with a strdup'd copy of the result. But normalize_path_copy explicitly allows normalizing in-place, so we can simply do that.
--- test-path-utils.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
test-path-utils.c +4 -11
@@ -8,21 +8,14 @@*/ static int normalize_ceiling_entry (struct string_list_item *item,void *unused) { -const char *ceil = item->string; -int len =strlen (ceil); -char buf[PATH_MAX +1 ]; +char *ceil = item->string; -if (len ==0 ) +if (!*ceil)die ("Empty path is not supported" ); -if (len >PATH_MAX ) -die ("Path \"%s\" is too long" , ceil);if (!is_absolute_path (ceil))die ("Path \"%s\" is not absolute" , ceil); -if (normalize_path_copy (buf, ceil) <0 ) +if (normalize_path_copy (ceil, ceil) <0 )die ("Path \"%s\" could not be normalized" , ceil); - len =strlen (buf); -free (item->string); - item->string =xstrdup (buf);return 1 ; }
@@ -166,7 +159,7 @@ static struct test_data dirname_data[] = {int main (int argc,char **argv) {if (argc ==3 && !strcmp (argv[1 ],"normalize_path_copy" )) { -char *buf =xmalloc (PATH_MAX +1 ); +char *buf =xmallocz (strlen (argv[2 ]));int rv =normalize_path_copy (buf, argv[2 ]);if (rv) buf ="++failed++" ;
Signature · 1 line
2.7.1.572.gf718037
PATCH 13/18 sequencer: simplify memory allocation of get_message
In reply to Jeff KingFor a commit with has "1234abcd" and subject "foo", this function produces a struct with three strings:
1. "foo"
2. "1234abcd... foo"
3. "parent of 1234abcd... foo"
It takes advantage of the fact that these strings are subsets of each other, and allocates only _one_ string, with pointers into the various parts. Unfortunately, this makes the string allocation complicated and hard to follow.
Since we keep only one of these in memory at a time, we can afford to simply allocate three strings. This lets us build on tools like xstrfmt and avoid manual computation.
While we're here, we can also drop the ad-hoc reimplementation of get_git_commit_encoding(), and simply call that function.
--- sequencer.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-)
sequencer.c +10 -19
@@ -124,42 +124,33 @@ static const char *action_name(const struct replay_opts *opts)struct commit_message {char *parent_label; -const char *label; -const char *subject; +char *label; +char *subject;const char *message; };static int get_message (struct commit *commit,struct commit_message *out) {const char *abbrev, *subject; -int abbrev_len, subject_len; -char *q; - -if (!git_commit_encoding) - git_commit_encoding ="UTF-8" ; +int subject_len; - out->message =logmsg_reencode (commit,NULL , git_commit_encoding); + out->message =logmsg_reencode (commit,NULL ,get_commit_output_encoding ()); abbrev =find_unique_abbrev (commit->object.oid.hash,DEFAULT_ABBREV ); - abbrev_len =strlen (abbrev); subject_len =find_commit_subject (out->message, &subject); - out->parent_label =xmalloc (strlen ("parent of " ) + abbrev_len + -strlen ("... " ) + subject_len +1 ); - q = out->parent_label; - q =mempcpy (q,"parent of " ,strlen ("parent of " )); - out->label = q; - q =mempcpy (q, abbrev, abbrev_len); - q =mempcpy (q,"... " ,strlen ("... " )); - out->subject = q; - q =mempcpy (q, subject, subject_len); - *q ='\0' ; + out->subject =xmemdupz (subject, subject_len); + out->label =xstrfmt ("%s... %s" , abbrev, out->subject); + out->parent_label =xstrfmt ("parent of %s" , out->label); +return 0 ; }static void free_message (struct commit *commit,struct commit_message *msg) {free (msg->parent_label); +free (msg->label); +free (msg->subject);unuse_commit_buffer (commit, msg->message); }
Signature · 1 line
2.7.1.572.gf718037
PATCH 14/18 git-compat-util: drop mempcpy compat code
In reply to Jeff KingThere are no callers of this left, as the last one was dropped in the previous patch. And there are no likely to be new ones, as the function has been around since 2010 without gaining any new callers.
--- git-compat-util.h | 9 --------- 1 file changed, 9 deletions(-)
git-compat-util.h +0 -9
@@ -681,7 +681,6 @@ extern int git_vsnprintf(char *str, size_t maxsize,#ifdef __GLIBC_PREREQ#if __GLIBC_PREREQ (2 ,1 )#define HAVE_STRCHRNUL -#define HAVE_MEMPCPY #endif #endif
@@ -695,14 +694,6 @@ static inline char *gitstrchrnul(const char *s, int c) }#endif -#ifndef HAVE_MEMPCPY -#define mempcpy gitmempcpy -static inline void *gitmempcpy (void *dest,const void *src,size_t n) -{ -return (char *)memcpy (dest, src, n) + n; -} -#endif -#ifdef NO_INET_PTON int inet_pton (int af,const char *src,void *dst);#endif
Signature · 1 line
2.7.1.572.gf718037
PATCH 15/18 transport_anonymize_url: use xstrfmt
In reply to Jeff KingThis function uses xcalloc and two memcpy calls to concatenate two strings. We can do this as an xstrfmt one-liner, and then it is more clear that we are allocating the correct amount of memory.
--- transport.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
transport.c +3 -5
@@ -1351,7 +1351,7 @@ int transport_disconnect(struct transport *transport)*/ char *transport_anonymize_url (const char *url) { -char *anon_url, *scheme_prefix, *anon_part; +char *scheme_prefix, *anon_part;size_t anon_len, prefix_len =0 ; anon_part =strchr (url,'@' );
@@ -1385,10 +1385,8 @@ char *transport_anonymize_url(const char *url) goto literal_copy; prefix_len = scheme_prefix - url +3 ; } - anon_url =xcalloc (1 ,1 + prefix_len + anon_len); -memcpy (anon_url, url, prefix_len); -memcpy (anon_url + prefix_len, anon_part, anon_len); -return anon_url; +return xstrfmt ("%.*s%.*s" , (int )prefix_len, url, + (int )anon_len, anon_part); literal_copy:return xstrdup (url); }
Signature · 1 line
2.7.1.572.gf718037
PATCH 16/18 diff_populate_gitlink: use a strbuf
In reply to Jeff KingWe allocate 100 bytes to hold the "Submodule commit ..." text. This is enough, but it's not immediately obvious that this is the case, and we have to repeat the magic 100 twice.
We could get away with xstrfmt here, but we want to know the size, as well, so let's use a real strbuf. And while we're here, we can clean up the logic around size_only. It currently sets and clears the "data" field pointlessly, and leaves the "should_free" flag on even after we have cleared the data.
--- diff.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
diff.c +8 -8
@@ -2704,21 +2704,21 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, intstatic int diff_populate_gitlink (struct diff_filespec *s,int size_only) { -int len; -char *data =xmalloc (100 ), *dirty ="" ; +struct strbuf buf =STRBUF_INIT ; +char *dirty ="" ;/* Are we looking at the work tree? */ if (s->dirty_submodule) dirty ="-dirty" ; - len =snprintf (data,100 , -"Subproject commit %s%s\n" ,sha1_to_hex (s->sha1), dirty); - s->data = data; - s->size = len; - s->should_free =1 ; +strbuf_addf (&buf,"Subproject commit %s%s\n" ,sha1_to_hex (s->sha1), dirty); + s->size = buf.len;if (size_only) { s->data =NULL ; -free (data); +strbuf_release (&buf); + }else { + s->data =strbuf_detach (&buf,NULL ); + s->should_free =1 ; }return 0 ; }
Signature · 1 line
2.7.1.572.gf718037
PATCH 17/18 convert ewah/bitmap code to use xmalloc
In reply to Jeff KingThis code was originally written with the idea that it could be spun off into its own ewah library, and uses the overrideable ewah_malloc to do allocations.
We plug in xmalloc as our ewah_malloc, of course. But over the years the ewah code itself has become more entangled with git, and the return value of many ewah_malloc sites is not checked.
Let's just drop the level of indirection and use xmalloc and friends directly. This saves a few lines, and will let us adapt these sites to our more advanced malloc helpers.
--- ewah/bitmap.c | 12 ++++++------ ewah/ewah_bitmap.c | 9 +++------ ewah/ewah_io.c | 10 ++-------- ewah/ewok.h | 10 ---------- 4 files changed, 11 insertions(+), 30 deletions(-)
ewah/bitmap.c +6 -6
@@ -25,8 +25,8 @@struct bitmap *bitmap_new (void ) { -struct bitmap *bitmap =ewah_malloc (sizeof (struct bitmap )); - bitmap->words =ewah_calloc (32 ,sizeof (eword_t)); +struct bitmap *bitmap =xmalloc (sizeof (struct bitmap )); + bitmap->words =xcalloc (32 ,sizeof (eword_t)); bitmap->word_alloc =32 ;return bitmap; }
@@ -38,8 +38,8 @@ void bitmap_set(struct bitmap *self, size_t pos)if (block >= self->word_alloc) {size_t old_size = self->word_alloc; self->word_alloc = block *2 ; - self->words =ewah_realloc (self->words, - self->word_alloc *sizeof (eword_t)); + self->words =xrealloc (self->words, + self->word_alloc *sizeof (eword_t));memset (self->words + old_size,0x0 , (self->word_alloc - old_size) *sizeof (eword_t));
@@ -102,7 +102,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)while (ewah_iterator_next (&blowup, &it)) {if (i >= bitmap->word_alloc) { bitmap->word_alloc *=1.5 ; - bitmap->words =ewah_realloc ( + bitmap->words =xrealloc ( bitmap->words, bitmap->word_alloc *sizeof (eword_t)); }
@@ -134,7 +134,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)if (self->word_alloc < other_final) { self->word_alloc = other_final; - self->words =ewah_realloc (self->words, + self->words =xrealloc (self->words, self->word_alloc *sizeof (eword_t));memset (self->words + original_size,0x0 , (self->word_alloc - original_size) *sizeof (eword_t));
ewah/ewah_bitmap.c +3 -6
@@ -39,7 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)return ; self->alloc_size = new_size; - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); self->rlw = self->buffer + (rlw_offset /sizeof (eword_t)); }
@@ -282,11 +282,8 @@ struct ewah_bitmap *ewah_new(void) {struct ewah_bitmap *self; - self =ewah_malloc (sizeof (struct ewah_bitmap )); -if (self ==NULL ) -return NULL ; - - self->buffer =ewah_malloc (32 *sizeof (eword_t)); + self =xmalloc (sizeof (struct ewah_bitmap )); + self->buffer =xmalloc (32 *sizeof (eword_t)); self->alloc_size =32 ;ewah_clear (self);
ewah/ewah_io.c +2 -8
@@ -134,12 +134,9 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len) self->buffer_size = self->alloc_size =get_be32 (ptr); ptr +=sizeof (uint32_t ); - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); -if (!self->buffer) -return -1 ; -/* * Copy the raw data for the bitmap as a whole chunk; * if we're in a little-endian platform, we'll perform
@@ -180,12 +177,9 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)return -1 ; self->buffer_size = self->alloc_size = (size_t )ntohl (word_count); - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); -if (!self->buffer) -return -1 ; -/** 64 bit x N -- compressed words */ buffer = self->buffer; words_left = self->buffer_size;
ewah/ewok.h +0 -10
@@ -20,16 +20,6 @@#ifndef __EWOK_BITMAP_H__#define __EWOK_BITMAP_H__ -#ifndef ewah_malloc -# define ewah_malloc xmalloc -#endif -#ifndef ewah_realloc -# define ewah_realloc xrealloc -#endif -#ifndef ewah_calloc -# define ewah_calloc xcalloc -#endif -struct strbuf ;typedef uint64_t eword_t ;#define BITS_IN_EWORD (sizeof(eword_t) * 8)
Signature · 1 line
2.7.1.572.gf718037
PATCH 18/18 ewah: convert to REALLOC_ARRAY, etc
In reply to Jeff KingNow that we're built around xmalloc and friends, we can use helpers like REALLOC_ARRAY, ALLOC_GROW, and so on to make the code shorter and protect against integer overflow.
--- ewah/bitmap.c | 16 ++++------------ ewah/ewah_bitmap.c | 5 ++--- ewah/ewah_io.c | 6 ++---- 3 files changed, 8 insertions(+), 19 deletions(-)
ewah/bitmap.c +4 -12
@@ -17,7 +17,7 @@* along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "git-compat-util.h" +#include "cache.h" #include "ewok.h" #define EWAH_MASK (x) ((eword_t)1 << (x % BITS_IN_EWORD))
@@ -38,9 +38,7 @@ void bitmap_set(struct bitmap *self, size_t pos)if (block >= self->word_alloc) {size_t old_size = self->word_alloc; self->word_alloc = block *2 ; - self->words =xrealloc (self->words, - self->word_alloc *sizeof (eword_t)); - +REALLOC_ARRAY (self->words, self->word_alloc);memset (self->words + old_size,0x0 , (self->word_alloc - old_size) *sizeof (eword_t)); }
@@ -100,12 +98,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)ewah_iterator_init (&it, ewah);while (ewah_iterator_next (&blowup, &it)) { -if (i >= bitmap->word_alloc) { - bitmap->word_alloc *=1.5 ; - bitmap->words =xrealloc ( - bitmap->words, bitmap->word_alloc *sizeof (eword_t)); - } - +ALLOC_GROW (bitmap->words, i +1 , bitmap->word_alloc); bitmap->words[i++] = blowup; }
@@ -134,8 +127,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)if (self->word_alloc < other_final) { self->word_alloc = other_final; - self->words =xrealloc (self->words, - self->word_alloc *sizeof (eword_t)); +REALLOC_ARRAY (self->words, self->word_alloc);memset (self->words + original_size,0x0 , (self->word_alloc - original_size) *sizeof (eword_t)); }
ewah/ewah_bitmap.c +2 -3
@@ -39,8 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)return ; self->alloc_size = new_size; - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size); self->rlw = self->buffer + (rlw_offset /sizeof (eword_t)); }
@@ -283,8 +282,8 @@ struct ewah_bitmap *ewah_new(void)struct ewah_bitmap *self; self =xmalloc (sizeof (struct ewah_bitmap )); - self->buffer =xmalloc (32 *sizeof (eword_t)); self->alloc_size =32 ; +ALLOC_ARRAY (self->buffer, self->alloc_size);ewah_clear (self);return self;
ewah/ewah_io.c +2 -4
@@ -134,8 +134,7 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len) self->buffer_size = self->alloc_size =get_be32 (ptr); ptr +=sizeof (uint32_t ); - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size);/* * Copy the raw data for the bitmap as a whole chunk;
@@ -177,8 +176,7 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)return -1 ; self->buffer_size = self->alloc_size = (size_t )ntohl (word_count); - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size);/** 64 bit x N -- compressed words */ buffer = self->buffer;
Signature · 1 line
2.7.1.572.gf718037
Quoted 10 lines · Jeff King
The only bug I have actually confirmed in practice here is fixed by patch 2 (which is why it's at the front). There's another one in
path_name(), but that function is already dropped by the nearby jk/lose-name-path topic.The rest are cleanups of spots which _might_ be buggy, but I didn't dig too far to find out. As with the earlier audit, I tried to refactor using helpers that make the code clearer and less error-prone. So maybe they're fixing bugs or not, but they certainly make it easier to audit the result for problems.
After this, looking for /[cm]alloc.*\+/ is pretty clean. I _didn't_ fix any sites in xdiff, but those are already protected by dcd1742 (xdiff:
reject files larger than ~1GB, 2015-09-24).
That's not necessarily complete coverage, though, as you can always screw up the computation outside of the xmalloc call, and pass in the truncated version. E.g.:
int alloc = a + b; char *foo = xmalloc(alloc);
However, this is only a big problem if you then copy "a" and "b" separately. If you use "alloc" later as the limit, like:
xsnprintf(foo, alloc, "%s%s", a, b);
that's only a minor bug (we notice the overflow and complain, rather than smashing the heap).
-Peff
Re: [PATCH 04/18] add helpers for allocating flex-array structs
In reply to Jeff KingQuoted 6 lines · Jeff King
Allocating a struct with a flex array is pretty simple in
practice: you over-allocate the struct, then copy some data into the over-allocation. But it can be a slight pain to make sure you're allocating and copying the right amounts.This patch adds a few helpers to turn simple cases of into a
Grammo: "cases of into"
git-compat-util.h extern FILE *fopen_for_writing(const char *path); · line 786
Quoted 12 lines
one-liner that properly checks for overflow. See the embedded documentation for details.
[...]Signed-off-by Jeff King <[email protected]>---
git-compat-util.h +5
-0@@ -782,6 +782,68 @@ extern FILE *fopen_for_writing(const char *path); +* struct foo *f; +* FLEX_ALLOC_STR(f, name, src); +* +* and "name" will point to a block of memory after the struct, which will be +* freed along with the struct (but the pointer can be repoined anywhere).
"repoined"?
Quoted 2 lines
git-compat-util.h +2
-0+* The *_STR variants accept a string parameter rather than a ptr/len +* combination.
Re: [PATCH 04/18] add helpers for allocating flex-array structs
Quoted 3 lines · Eric Sunshine
Quoted 1 line
This patch adds a few helpers to turn simple cases of into a
Grammo: "cases of into"
Oops. Cases of "flex-array struct allocation into...".
Quoted 4 lines
Quoted 2 lines
+ * and "name" will point to a block of memory after the struct, which will be
+ * freed along with the struct (but the pointer can be repoined anywhere)."repoined"?
Repointed.
Fixed patch below.
-- >8 --
Subject: [PATCH] add helpers for allocating flex-array structs
Allocating a struct with a flex array is pretty simple in
practice: you over-allocate the struct, then copy some data into the over-allocation. But it can be a slight pain to make sure you're allocating and copying the right amounts.
This patch adds a few helpers to turn simple cases of flex-array struct allocation into a one-liner that properly checks for overflow. See the embedded documentation for details.
Ideally we could provide a more flexible version that could handle multiple strings, like:
FLEX_ALLOC_FMT(ref, name, "%s%s", prefix, name);
But we have to implement this as a macro (because of the offset calculation of the flex member), which means we would need all compilers to support variadic macros.
--- git-compat-util.h | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
git-compat-util.h +62 -0
@@ -782,6 +782,68 @@ extern FILE *fopen_for_writing(const char *path);#define ALLOC_ARRAY (x, alloc) (x) = xmalloc(st_mult((alloc), sizeof(*(x))))#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), st_mult((alloc), sizeof(*(x)))) +/* +* These functions help you allocate structs with flex arrays, and copy +* the data directly into the array. For example, if you had: +* +* struct foo { +* int bar; +* char name[FLEX_ARRAY]; +* }; +* +* you can do: +* +* struct foo *f; +* FLEX_ALLOC_MEM(f, name, src, len); +* +* to allocate a "foo" with the contents of "src" in the "name" field. +* The resulting struct is automatically zero'd, and the flex-array field +* is NUL-terminated (whether the incoming src buffer was or not). +* +* The FLEXPTR_* variants operate on structs that don't use flex-arrays, +* but do want to store a pointer to some extra data in the same allocated +* block. For example, if you have: +* +* struct foo { +* char *name; +* int bar; +* }; +* +* you can do: +* +* struct foo *f; +* FLEX_ALLOC_STR(f, name, src); +* +* and "name" will point to a block of memory after the struct, which will be +* freed along with the struct (but the pointer can be repointed anywhere). +* +* The *_STR variants accept a string parameter rather than a ptr/len +* combination. +* +* Note that these macros will evaluate the first parameter multiple +* times, and it must be assignable as an lvalue. +*/ +#define FLEX_ALLOC_MEM (x, flexname, buf, len) do { \ + (x) = NULL;/* silence -Wuninitialized for offset calculation */ \ + (x) =xalloc_flex (sizeof (*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \ +} while (0) +#define FLEXPTR_ALLOC_MEM (x, ptrname, buf, len) do { \ + (x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \ + (x)->ptrname = (void *)((x)+1); \ +} while(0) +#define FLEX_ALLOC_STR (x, flexname, str) \ + FLEX_ALLOC_MEM((x), flexname, (str), strlen(str)) +#define FLEXPTR_ALLOC_STR (x, ptrname, str) \ + FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str)) + +static inline void *xalloc_flex (size_t base_len,size_t offset, +const void *src,size_t src_len) +{ +unsigned char *ret =xcalloc (1 ,st_add3 (base_len, src_len,1 )); +memcpy (ret + offset, src, src_len); +return ret; +} +static inline char *xstrdup_or_null (const char *str) {return str ?xstrdup (str) :NULL ;
Signature · 1 line
2.7.1.574.gccd43a9
Re: [PATCH 05/18] convert trivial cases to ALLOC_ARRAY
In reply to Jeff KingQuoted 17 lines · Jeff King
Each of these cases can be converted to use
ALLOC_ARRAYorREALLOC_ARRAY, which has two advantages:1. It automatically checks the array-size multiplication for overflow.
2. It always uses
sizeof(*array)for the element-size, so that it can never go out of sync with the declared type of the array.Signed-off-by Jeff King <[email protected]>---
compat/mingw.c +0
-0
compat/mingw.c static char **get_path_split(void) · line 857
Quoted 5 lines
compat/mingw.c +1
-1@@ -854,7 +854,7 @@ static char **get_path_split(void)if (!n)return NULL ; - path =xmalloc ((n+1 )*sizeof (char *)); +ALLOC_ARRAY (path, n+1 );
Elsewhere in this patch, you've reformatted "x+c" as "x + c"; perhaps do so here, as well.
Quoted 3 lines
compat/mingw.c +0
-0p = envpath; i =0 ; do {
Re: [PATCH 05/18] convert trivial cases to ALLOC_ARRAY
Quoted 21 lines · Eric Sunshine
Quoted 20 lines · Jeff King
Each of these cases can be converted to use
ALLOC_ARRAYorREALLOC_ARRAY, which has two advantages:1. It automatically checks the array-size multiplication for overflow.
2. It always uses
sizeof(*array)for the element-size, so that it can never go out of sync with the declared type of the array.Signed-off-by Jeff King <[email protected]>---
compat/mingw.c +0
-0@@ -854,7 +854,7 @@ static char **get_path_split(void)if (!n)return NULL ;
Quoted 5 lines
compat/mingw.c static char **get_path_split(void) · line 857
Quoted 2 lines
compat/mingw.c +1
-1- path =xmalloc ((n+1 )*sizeof (char *)); +ALLOC_ARRAY (path, n+1 );Elsewhere in this patch, you've reformatted "x+c" as "x + c"; perhaps do so here, as well.
Will do. I noticed while going over this before sending it out that it may also be technically possible for "n+1" to overflow here (and I think in a few other places in this patch). I don't know how paranoid we want to be.
-Peff
Re: [PATCH 05/18] convert trivial cases to ALLOC_ARRAY
Quoted 5 lines · Jeff King
Quoted 4 lines · Eric Sunshine
Quoted 2 lines · Jeff King
compat/mingw.c +1
-1- path =xmalloc ((n+1 )*sizeof (char *)); +ALLOC_ARRAY (path, n+1 );
Quoted 7 lines
Quoted 2 lines
Elsewhere in this patch, you've reformatted "x+c" as "x + c"; perhaps do so here, as well.
Will do. I noticed while going over this before sending it out that it may also be technically possible for "n+1" to overflow here (and I think in a few other places in this patch). I don't know how paranoid we want to be.
Yes, I also noticed those and considered mentioning it. There was also some multiplication which might be of concern.
ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);
It would be easy enough to manually call st_add() and st_mult() for those cases, but I haven't examined them closely enough to determine how likely they would be to overflow, nor do I know if the resulting noisiness of code is desirable.
Re: [PATCH 05/18] convert trivial cases to ALLOC_ARRAY
Quoted 17 lines · Eric Sunshine
Quoted 5 lines · Jeff King
Quoted 4 lines · Eric Sunshine
Quoted 2 lines · Jeff King
compat/mingw.c +1
-1- path =xmalloc ((n+1 )*sizeof (char *)); +ALLOC_ARRAY (path, n+1 );Quoted 7 lines
Quoted 2 lines
Elsewhere in this patch, you've reformatted "x+c" as "x + c"; perhaps do so here, as well.
Will do. I noticed while going over this before sending it out that it may also be technically possible for "n+1" to overflow here (and I think in a few other places in this patch). I don't know how paranoid we want to be.
Yes, I also noticed those and considered mentioning it. There was also some multiplication which might be of concern.
Quoted 6 lines
ALLOC_ARRAY(graph->mapping, 2 * graph->column_capacity);It would be easy enough to manually call
st_add()andst_mult()for those cases, but I haven't examined them closely enough to determine how likely they would be to overflow, nor do I know if the resulting noisiness of code is desirable.
Yeah, I'm quite sure that one is safe (we set column_capacity to a fixed integer immediately beforehand). And many of the "+" ones are likely safe, too. If "n" is close to wrapping, then allocating "n" structs will probably fail beforehand (though not always, if you have a ton of RAM and "n" is a signed int).
But part of the point of this series is that we shouldn't have to wonder if things are safe. They should just be obviously so, and we should err on the side of caution. So I think it probably _is_ worth sprinkling st_add() calls in those places. I'll take a look for the re-roll.
-Peff
Re: [PATCH 07/18] convert trivial cases to FLEX_ARRAY macros
In reply to Jeff KingQuoted 9 lines · Jeff King
Using
FLEX_ARRAYmacros reduces the amount of manual computation size we have to do. It also ensures we don't overflowsize_t, and it makes sure we write the same number of bytes that we allocated.Signed-off-by Jeff King <[email protected]>---
builtin/reflog.c static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) · line 413
Quoted 6 lines
builtin/reflog.c +1
-2@@ -412,8 +410,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) !memcmp(ent->pattern, pattern, len))return ent; - ent =xcalloc (1 , (sizeof (*ent) + len)); -memcpy (ent->pattern, pattern, len); +FLEX_ALLOC_MEM (ent, pattern, pattern, len);
Does the incoming 'len' already account for the NUL terminator, or was the original code underallocating?
Answering my own question: Looking at reflog_expire_config() and parse_config_key(), I gather that 'len' already accounts for the NUL, thus the new code is overallocating (which should not be a problem).
hashmap.c const void *memintern(const void *data, size_t len) · line 259
Quoted 10 lines
builtin/reflog.c +0
-0ent->len = len; *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);hashmap.c +1
-1@@ -256,10 +256,9 @@ const void *memintern(const void *data, size_t len) e =hashmap_get (&map, &key, data);if (!e) {/* not found: create it */ - e =xmallocz (sizeof (struct pool_entry ) + len); +FLEX_ALLOC_MEM (e, data, data, len);
Ditto. I guess the new code is overallocating (which should be okay).
Quoted 6 lines
hashmap.c +0
-1hashmap_entry_init (e, key.ent.hash); e->len = len; -memcpy (e->data, data, len);hashmap_add (&map, e); }return e->data;
Re: [PATCH 07/18] convert trivial cases to FLEX_ARRAY macros
Quoted 13 lines · Eric Sunshine
builtin/reflog.c static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) · line 413
Quoted 9 lines
---
builtin/reflog.c +1
-2@@ -412,8 +410,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) !memcmp(ent->pattern, pattern, len))return ent; - ent =xcalloc (1 , (sizeof (*ent) + len)); -memcpy (ent->pattern, pattern, len); +FLEX_ALLOC_MEM (ent, pattern, pattern, len);Does the incoming 'len' already account for the NUL terminator, or was the original code underallocating?
Quoted 3 lines
Answering my own question: Looking at
reflog_expire_config()andparse_config_key(), I gather that 'len' already accounts for the NUL, thus the new code is overallocating (which should not be a problem).
Actually, I think the original underallocates. If we have gc.foobar.reflogExpire, then "pattern" will poitn to "foobar" and "len" will be 6. Meaning we allocate without a trailing NUL.
That _should_ be OK, because the struct has a "len" field, and readers can be careful not to go past it. And indeed, in the loop above, we check the length and use memcmp().
But later, in set_reflog_expiry_param(), we walk through the list and hand ent->pattern directly to wildmatch, which assumes a NUL-terminated string. In practice, it probably works out 7 out of 8 times, because malloc will align the struct, and we're on a zeroed page, so unless the string is exactly 8 characters, we'll get some extra NULs afterwards. But I could demonstrate it by doing:
gdb --args git -c gc.foobar12.reflogexpire=never reflog expire --all
and breaking on wildmatch, which yields:
Breakpoint 1, wildmatch (pattern=0x85eb70 "foobar12Q", text=0x85e4d4
"refs/heads/master", flags=0, wo=0x0)
So this is in fact fixing a bug. I can't say I'm terribly surprised nobody noticed it, as per-ref reflog expiration is pretty obscure.
I hope this increases confidence in my patch series. Even though I didn't _know_ there was a bug here, I did know that malloc computations are a potential source of errors. And the FLEX_ALLOC helpers are designed to remove that work and have a simple interface. We don't know whether the caller will want a NUL afterwards or not, but we err on the side of over-allocating by a byte (just as we over-allocate read_sha1_file() output by a byte), because safety is better than squeezing out a single byte.
Quoted 9 lines
hashmap.c const void *memintern(const void *data, size_t len) · line 259
Quoted 7 lines
hashmap.c +1
-1@@ -256,10 +256,9 @@ const void *memintern(const void *data, size_t len) e =hashmap_get (&map, &key, data);if (!e) {/* not found: create it */ - e =xmallocz (sizeof (struct pool_entry ) + len); +FLEX_ALLOC_MEM (e, data, data, len);Ditto. I guess the new code is overallocating (which should be okay).
It is, but so was the original (it used xmallocz to get an extra NUL).
-Peff
Re: [PATCH 07/18] convert trivial cases to FLEX_ARRAY macros
Quoted 21 lines · Jeff King
Quoted 3 lines
Answering my own question: Looking at
reflog_expire_config()andparse_config_key(), I gather that 'len' already accounts for the NUL, thus the new code is overallocating (which should not be a problem).Actually, I think the original underallocates. If we have gc.foobar.reflogExpire, then "pattern" will poitn to "foobar" and "len" will be 6. Meaning we allocate without a trailing NUL.
That _should_ be OK, because the struct has a "len" field, and readers can be careful not to go past it. And indeed, in the loop above, we check the length and use
memcmp().But later, in
set_reflog_expiry_param(), we walk through the list and handent->patterndirectly to wildmatch, which assumes a NUL-terminated string. In practice, it probably works out 7 out of 8 times, because malloc will align the struct, and we're on a zeroed page, so unless the string is exactly 8 characters, we'll get some extra NULs afterwards. But I could demonstrate it by doing:gdb --args git -c gc.foobar12.reflogexpire=never reflog expire --all
Quoted 7 lines
and breaking on wildmatch, which yields:
Breakpoint 1, wildmatch (pattern=0x85eb70 "foobar12Q", text=0x85e4d4
"refs/heads/master", flags=0, wo=0x0)So this is in fact fixing a bug. I can't say I'm terribly surprised nobody noticed it, as per-ref reflog expiration is pretty obscure.
We could do this on top of my series (I can also factor out the fix separately to go at the beginning if we don't want to hold the bugfix hostage).
-- >8 --
Subject: [PATCH] reflog_expire_cfg: drop misleading "len" parameter
You can tweak the reflog expiration for a particular subset of refs by configuring gc.foo.reflogexpire. We keep a linked list of reflog_expire_cfg structs, each of which holds the pattern and a "len" field for the length of the pattern.
However, we feed the pattern directly to wildmatch(), which means that it must be a NUL-terminated string. Before the recent conversion to FLEX_ALLOC_MEM, we got this wrong, and could feed extra garbage to wildmatch(). That's now fixed, but the "len" parameter is simply misleading. The pattern is a string, and we don't need to record its length.
To get rid of it, we do need to tweak the "do we have it already?" search in find_cfg_ent(), but we can do so without having a recorded length by just using strncmp.
--- builtin/reflog.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
builtin/reflog.c +2 -4
@@ -394,7 +394,6 @@ static struct reflog_expire_cfg {struct reflog_expire_cfg *next;unsigned long expire_total;unsigned long expire_unreachable; -size_t len;char pattern[FLEX_ARRAY ]; } *reflog_expire_cfg, **reflog_expire_cfg_tail;
@@ -406,12 +405,11 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) reflog_expire_cfg_tail = &reflog_expire_cfg;for (ent = reflog_expire_cfg; ent; ent = ent->next) -if (ent->len == len && - !memcmp (ent->pattern, pattern, len)) +if (!strncmp (ent->pattern, pattern, len) && + ent->pattern[len] =='\0' )return ent;FLEX_ALLOC_MEM (ent, pattern, pattern, len); - ent->len = len; *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);return ent;
Signature · 1 line
2.7.1.574.gccd43a9
Re: [PATCH 07/18] convert trivial cases to FLEX_ARRAY macros
Quoted 6 lines · Jeff King
We could do this on top of my series (I can also factor out the fix separately to go at the beginning if we don't want to hold the bugfix hostage).
-- >8 --
Subject: [PATCH]reflog_expire_cfg: drop misleading "len" parameter
Here it is as a separate fix. Applying my series on top would need a minor and obvious tweak. I'll hold a re-roll for more comments, but will otherwise plan to stick this at the front of the series.
-- >8 --
Subject: [PATCH] reflog_expire_cfg: NUL-terminate pattern field
You can tweak the reflog expiration for a particular subset of refs by configuring gc.foo.reflogexpire. We keep a linked list of reflog_expire_cfg structs, each of which holds the pattern and a "len" field for the length of the pattern. The pattern itself is _not_ NUL-terminated.
However, we feed the pattern directly to wildmatch(), which expects a NUL-terminated string, meaning it may keep reading random junk after our struct.
We can fix this by allocating an extra byte for the NUL (which is already zero because we use xcalloc). Let's also drop the misleading "len" field, which is no longer necessary. The existing use of "len" can be converted to use strncmp().
--- builtin/reflog.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
builtin/reflog.c +3 -5
@@ -396,7 +396,6 @@ static struct reflog_expire_cfg {struct reflog_expire_cfg *next;unsigned long expire_total;unsigned long expire_unreachable; -size_t len;char pattern[FLEX_ARRAY ]; } *reflog_expire_cfg, **reflog_expire_cfg_tail;
@@ -408,13 +407,12 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) reflog_expire_cfg_tail = &reflog_expire_cfg;for (ent = reflog_expire_cfg; ent; ent = ent->next) -if (ent->len == len && - !memcmp (ent->pattern, pattern, len)) +if (!strncmp (ent->pattern, pattern, len) && + ent->pattern[len] =='\0' )return ent; - ent =xcalloc (1 , (sizeof (*ent) + len)); + ent =xcalloc (1 ,sizeof (*ent) + len +1 );memcpy (ent->pattern, pattern, len); - ent->len = len; *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);return ent;
Signature · 1 line
2.7.1.574.gccd43a9
Re: [PATCH 07/18] convert trivial cases to FLEX_ARRAY macros
Quoted 11 lines · Jeff King
Quoted 6 lines · Jeff King
We could do this on top of my series (I can also factor out the fix separately to go at the beginning if we don't want to hold the bugfix hostage).
-- >8 --
Subject: [PATCH]reflog_expire_cfg: drop misleading "len" parameterHere it is as a separate fix. Applying my series on top would need a minor and obvious tweak. I'll hold a re-roll for more comments, but will otherwise plan to stick this at the front of the series.
Yep, I prefer this version of the patch too, as it makes it explicit that a bug is being fixed rather than it happening "by accident" via the FLEX_ALLOC_MEM conversion, which is easily overlooked.
Quoted 23 lines
-- >8 --
Subject: [PATCH]reflog_expire_cfg: NUL-terminate pattern fieldYou can tweak the reflog expiration for a particular subset of refs by configuring gc.foo.reflogexpire. We keep a linked list of
reflog_expire_cfgstructs, each of which holds the pattern and a "len" field for the length of the pattern. The pattern itself is _not_ NUL-terminated.However, we feed the pattern directly to
wildmatch(), which expects a NUL-terminated string, meaning it may keep reading random junk after our struct.We can fix this by allocating an extra byte for the NUL (which is already zero because we use xcalloc). Let's also drop the misleading "len" field, which is no longer necessary. The existing use of "len" can be converted to use
strncmp().Signed-off-by Jeff King <[email protected]>---
builtin/reflog.c static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) · line 411
Quoted 7 lines
builtin/reflog.c +2
-2@@ -408,13 +407,12 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) reflog_expire_cfg_tail = &reflog_expire_cfg;for (ent = reflog_expire_cfg; ent; ent = ent->next) -if (ent->len == len && - !memcmp (ent->pattern, pattern, len)) +if (!strncmp (ent->pattern, pattern, len) && + ent->pattern[len] =='\0' )
If 'ent->pattern' is shorter than 'pattern' then the strncmp() will fail, thus it will short-circuit before ent->pattern[len] has a chance to access beyond the end of memory allocated for 'ent->pattern'. Okay, makes sense.
Quoted 11 lines
builtin/reflog.c +1
-3return ent; - ent =xcalloc (1 , (sizeof (*ent) + len)); + ent =xcalloc (1 ,sizeof (*ent) + len +1 );memcpy (ent->pattern, pattern, len); - ent->len = len; *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);return ent; --2.7.1.574.gccd43a9
Re: [PATCH 07/18] convert trivial cases to FLEX_ARRAY macros
Quoted 4 lines · Eric Sunshine
Quoted 4 lines
builtin/reflog.c +0
-0@@ -408,13 +407,12 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) reflog_expire_cfg_tail = &reflog_expire_cfg;
Quoted 10 lines
builtin/reflog.c static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) · line 411
Quoted 5 lines
builtin/reflog.c +2
-2for (ent = reflog_expire_cfg; ent; ent = ent->next) -if (ent->len == len && - !memcmp (ent->pattern, pattern, len)) +if (!strncmp (ent->pattern, pattern, len) && + ent->pattern[len] =='\0' )If '
ent->pattern' is shorter than 'pattern' then thestrncmp()will fail, thus it will short-circuit before ent->pattern[len] has a chance to access beyond the end of memory allocated for 'ent->pattern'. Okay, makes sense.
Yeah. It took me a minute to convince myself that this was correct. If you have a shorter or more clear way of writing it, I'm open to it. The best I could come up with is running an extra "strlen" and otherwise keeping the loop as it is; the performance on that is not as good, but if performance is a concern, maybe something besides a linear search would be in order. :)
-Peff
Re: [PATCH 07/18] convert trivial cases to FLEX_ARRAY macros
In reply to Jeff KingQuoted 8 lines · Jeff King
Quoted 7 lines · Eric Sunshine
builtin/reflog.c static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) · line 413
Quoted 3 lines
builtin/reflog.c +1
-2- ent =xcalloc (1 , (sizeof (*ent) + len)); -memcpy (ent->pattern, pattern, len); +FLEX_ALLOC_MEM (ent, pattern, pattern, len);Does the incoming 'len' already account for the NUL terminator, or was the original code underallocating?
Quoted 7 lines
Quoted 3 lines
Answering my own question: Looking at
reflog_expire_config()andparse_config_key(), I gather that 'len' already accounts for the NUL, thus the new code is overallocating (which should not be a problem).Actually, I think the original underallocates. If we have gc.foobar.reflogExpire, then "pattern" will poitn to "foobar" and "len" will be 6. Meaning we allocate without a trailing NUL.
Ugh, yeah, I misread the code.
Quoted 11 lines
That _should_ be OK, because the struct has a "len" field, and readers can be careful not to go past it. And indeed, in the loop above, we check the length and use
memcmp().But later, in
set_reflog_expiry_param(), we walk through the list and handent->patterndirectly to wildmatch, which assumes a NUL-terminated string. In practice, it probably works out 7 out of 8 times, because malloc will align the struct, and we're on a zeroed page, so unless the string is exactly 8 characters, we'll get some extra NULs afterwards. But I could demonstrate it by doing:
Quoted 6 lines
gdb --args git -c gc.foobar12.reflogexpire=never reflog expire --alland breaking on wildmatch, which yields:
Breakpoint 1, wildmatch (pattern=0x85eb70 "foobar12Q", text=0x85e4d4
"refs/heads/master", flags=0, wo=0x0)
Nice confirmation.
Re: [PATCH 08/18] use st_add and st_mult for allocation size computation
In reply to Jeff KingQuoted 12 lines · Jeff King
If our size computation overflows
size_t, we may allocate a much smaller buffer than we expected and overflow it. It's probably impossible to trigger an overflow in most of these sites in practice, but it is easy enough convert their additions and multiplications into overflow-checking variants. This may be fixing real bugs, and it makes auditing the code easier.Signed-off-by Jeff King <[email protected]>---
builtin/apply.c static void update_image(struct image *img, · line 2632
Quoted 2 lines
builtin/apply.c +1
-1@@ -2632,7 +2632,7 @@ static void update_image(struct image *img, - result =xmalloc (img->len + insert_count - remove_count +1 ); + result =xmalloc (st_add3 (st_sub (img->len, remove_count), insert_count,1 ));
Phew, what a mouthful, and not easy to read compared to the original. Fortunately, the remainder of the changes in this patch are straightforward and often simple.
sha1_name.c static void find_short_object_filename(int len, const char *hex_pfx, struct disa · line 91
Quoted 10 lines
sha1_name.c +2
-3@@ -87,9 +87,8 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disaconst char *objdir =get_object_directory (); -int objdir_len =strlen (objdir); -int entlen = objdir_len +43 ; - fakeent =xmalloc (sizeof (*fakeent) + entlen); +size_t objdir_len =strlen (objdir); + fakeent =xmalloc (st_add3 (sizeof (*fakeent), objdir_len,43 ));memcpy (fakeent->base, objdir, objdir_len); fakeent->name = fakeent->base + objdir_len +1 ;
If we've gotten this far without die()ing due to overflow in st_add3() when invoking xmalloc(), then we know that this fakeent->name computation won't overflow. Okay.
Quoted 1 line
sha1_name.c +0
-0fakeent->name[-1 ] ='/' ;
Re: [PATCH 13/18] sequencer: simplify memory allocation of get_message
In reply to Jeff KingQuoted 1 line · Jeff King
For a commit with has "
1234abcd" and subject "foo", this
Did you mean s/with has/which has ID/ or something?
Quoted 22 lines
function produces a struct with three strings:
1. "foo"
2. "
1234abcd... foo"3. "parent of
1234abcd... foo"It takes advantage of the fact that these strings are subsets of each other, and allocates only _one_ string, with pointers into the various parts. Unfortunately, this makes the string allocation complicated and hard to follow.
Since we keep only one of these in memory at a time, we can afford to simply allocate three strings. This lets us build on tools like xstrfmt and avoid manual computation.
While we're here, we can also drop the ad-hoc reimplementation of
get_git_commit_encoding(), and simply call that function.Signed-off-by Jeff King <[email protected]>
Re: [PATCH 14/18] git-compat-util: drop mempcpy compat code
In reply to Jeff KingQuoted 2 lines · Jeff King
There are no callers of this left, as the last one was dropped in the previous patch. And there are no likely to be
s/no/not
Quoted 4 lines
new ones, as the function has been around since 2010 without gaining any new callers.
Signed-off-by Jeff King <[email protected]>
v2 cover hardening allocations against integer overflow
In reply to Jeff KingHere's a re-roll of jk/tighten-alloc series from:
http://thread.gmane.org/gmane.comp.version-control.git/286253
This one fixes all of the minor typo/gramm-o problems from the first round. As Eric noted, the change to reflog_expire_cfg is an actual bug-fix. Rather than silently fixing it, I've bumped the fix out to its own commit at the front of the series.
I also took a look at the raw computation in the ALLOC_ARRAY and REALLOC_ARRAY lines, as well as the ones in ALLOC_GROW. In theory something like this is dangerous:
ALLOC_GROW(foo, nr_foo + 1, alloc_foo); foo[nr_foo++] = whatever;
if we overflow nr_foo, which is quite often an "int". In practice, I think we're OK, for two reasons:
1. Overflowing a signed "int" here is going to make it go negative (technically, it invokes undefined behavior, but let's be blindly pragmatic for a minute and assume twos-complement wrapping). On a system with a 64-bit size_t, that will try to allocate an enormous amount of memory and fail. On a 32 bit system, it will be only about 2GB. But...
2. We're talking about overflowing 2^31 counters here. And the counter is multiplied by the size of each object we're storing in the array. So even if we assume that foo is "char *", we know we've allocated close to 2GB already. On a 32-bit system, the subsequent 2GB allocation is pretty much guaranteed to fail.
On a 64-bit system, I suspect it's possible to convince some of these counters to wrap (e.g., storing an array of ints, we're talking about only 8GB; that's a lot, but plenty of machines, especially servers, can allocate that).
So I have a feeling we're mostly OK there, but the reasoning is certainly hand-wavy and I'd like to do better. Just switching to:
ALLOC_GROW(foo, st_add(nr_foo, 1), alloc_foo); foo[nr_foo++] = whatever;
doesn't quite cut it. We might succeed in the allocation, and it stays big, which is good. But if nr_foo is an int, and we wrap to negative values, we'll start writing to memory before "foo", corrupting the heap.
So I really think we need to look at each site (and there are a lot of them) and start using size_t more consistently for these. Or alternatively, have an int-sized version of st_add and use that, though it's probably just as much work to convert it to a size_t, which IMHO is more correct. I really wanted to make a type-agnostic version of st_add(), but I don't think it's possible to do so portably. My best attempts needed either typeof() or compiler intrinsics.
So I've punted on that for this series, because I'm not convinced there are active problems, and it's quite a lot of work (and the patches will be quite disruptive).
While pondering this, I also looked at what happens if an incoming packfile claims to have 2^32 objects in its header. In index-pack we actually read this into a signed "int". Which is kind of bad, but in practice means we run into the "whoops, I can't allocate (size_t)-1 memory" problem and die. We could change this to a uint32_t (which is what the actual incoming format supports), but I have a feeling that makes things worse (if we actually manage to process that many objects, we then start doing some other computations based on the number of objects, all using ints; so at least as it is now, we bail early).
While peeking at some of these sites, though, I did realize that many of the ones that became "ALLOC_ARRAY(foo + 1)" were doing so to make a NULL-terminated argv list. So there are two new patches in this iteration to switch them to argv_array (one to catch the mundane cases, and one for a unique snowflake).
[01/21]: reflog_expire_cfg: NUL-terminate pattern field [02/21]: add helpers for detecting size_t overflow
[03/21]: tree-diff: catch integer overflow in combine_diff_path allocation [04/21]: harden REALLOC_ARRAY and xcalloc against size_t overflow [05/21]: add helpers for allocating flex-array structs [06/21]: convert manual allocations to argv_array
[07/21]: convert trivial cases to ALLOC_ARRAY
[08/21]: use xmallocz to avoid size arithmetic
[09/21]: convert trivial cases to FLEX_ARRAY macros
[10/21]: use st_add and st_mult for allocation size computation [11/21]: prepare_{git,shell}_cmd: use argv_array
[12/21]: write_untracked_extension: use FLEX_ALLOC helper [13/21]: fast-import: simplify allocation in start_packfile [14/21]: fetch-pack: simplify add_sought_entry
[15/21]: test-path-utils: fix normalize_path_copy output buffer size [16/21]: sequencer: simplify memory allocation of get_message [17/21]: git-compat-util: drop mempcpy compat code
[18/21]: transport_anonymize_url: use xstrfmt
[19/21]: diff_populate_gitlink: use a strbuf
[20/21]: convert ewah/bitmap code to use xmalloc
[21/21]: ewah: convert to REALLOC_ARRAY, etc
-Peff
PATCH v2 1/21 reflog_expire_cfg: NUL-terminate pattern field
You can tweak the reflog expiration for a particular subset of refs by configuring gc.foo.reflogexpire. We keep a linked list of reflog_expire_cfg structs, each of which holds the pattern and a "len" field for the length of the pattern. The pattern itself is _not_ NUL-terminated.
However, we feed the pattern directly to wildmatch(), which expects a NUL-terminated string, meaning it may keep reading random junk after our struct.
We can fix this by allocating an extra byte for the NUL (which is already zero because we use xcalloc). Let's also drop the misleading "len" field, which is no longer necessary. The existing use of "len" can be converted to use strncmp().
--- builtin/reflog.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
builtin/reflog.c +3 -5
@@ -396,7 +396,6 @@ static struct reflog_expire_cfg {struct reflog_expire_cfg *next;unsigned long expire_total;unsigned long expire_unreachable; -size_t len;char pattern[FLEX_ARRAY ]; } *reflog_expire_cfg, **reflog_expire_cfg_tail;
@@ -408,13 +407,12 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) reflog_expire_cfg_tail = &reflog_expire_cfg;for (ent = reflog_expire_cfg; ent; ent = ent->next) -if (ent->len == len && - !memcmp (ent->pattern, pattern, len)) +if (!strncmp (ent->pattern, pattern, len) && + ent->pattern[len] =='\0' )return ent; - ent =xcalloc (1 , (sizeof (*ent) + len)); + ent =xcalloc (1 ,sizeof (*ent) + len +1 );memcpy (ent->pattern, pattern, len); - ent->len = len; *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);return ent;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 2/21 add helpers for detecting size_t overflow
In reply to Jeff KingPerforming computations on size_t variables that we feed to xmalloc and friends can be dangerous, as an integer overflow can cause us to allocate a much smaller chunk than we realized.
We already have unsigned_add_overflows(), but let's add unsigned_mult_overflows() to that. Furthermore, rather than have each site manually check and die on overflow, we can provide some helpers that will:
- promote the arguments to size_t, so that we know we are doing our computation in the same size of integer that will ultimately be fed to xmalloc
- check and die on overflow
- return the result so that computations can be done in the parameter list of xmalloc.
These functions are a lot uglier to use than normal arithmetic operators (you have to do "st_add(foo, bar)" instead of "foo + bar"). To at least limit the damage, we also provide multi-valued versions. So rather than:
st_add(st_add(a, b), st_add(c, d));
you can write:
st_add4(a, b, c, d);
This isn't nearly as elegant as a varargs function, but it's a lot harder to get it wrong. You don't have to remember to add a sentinel value at the end, and the compiler will complain if you get the number of arguments wrong. This patch adds only the numbered variants required to convert the current code base; we can easily add more later if needed.
--- git-compat-util.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
git-compat-util.h +34 -0
@@ -96,6 +96,14 @@#define unsigned_add_overflows (a, b) \ ((b) > maximum_unsigned_value_of_type(a) - (a)) +/* +* Returns true if the multiplication of "a" and "b" will +* overflow. The types of "a" and "b" must match and must be unsigned. +* Note that this macro evaluates "a" twice! +*/ +#define unsigned_mult_overflows (a, b) \ + ((a) && (b) > maximum_unsigned_value_of_type(a) / (a)) +#ifdef __GNUC__#define TYPEOF (x) (__typeof__(x))#else
@@ -713,6 +721,32 @@ extern void release_pack_memory(size_t);typedef void (*try_to_free_t )(size_t );extern try_to_free_t set_try_to_free_routine (try_to_free_t ); +static inline size_t st_add (size_t a,size_t b) +{ +if (unsigned_add_overflows (a, b)) +die ("size_t overflow: %" PRIuMAX" + %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a + b; +} +#define st_add3 (a,b,c) st_add((a),st_add((b),(c))) +#define st_add4 (a,b,c,d) st_add((a),st_add3((b),(c),(d))) + +static inline size_t st_mult (size_t a,size_t b) +{ +if (unsigned_mult_overflows (a, b)) +die ("size_t overflow: %" PRIuMAX" * %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a * b; +} + +static inline size_t st_sub (size_t a,size_t b) +{ +if (a < b) +die ("size_t underflow: %" PRIuMAX" - %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a - b; +} +#ifdef HAVE_ALLOCA_H # include <alloca.h> # define xalloca (size) (alloca(size))
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 3/21 tree-diff: catch integer overflow in combine_diff_path allocation
In reply to Jeff KingA combine_diff_path struct has two "flex" members allocated alongside the struct: a string to hold the pathname, and an array of parent pointers. We use an "int" to compute this, meaning we may easily overflow it if the pathname is extremely long.
We can fix this by using size_t, and checking for overflow with the st_add helper.
--- diff.h | 4 ++-- tree-diff.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
diff.h +2 -2
@@ -222,8 +222,8 @@ struct combine_diff_path { } parent[FLEX_ARRAY ]; };#define combine_diff_path_size (n, l) \ - (sizeof(struct combine_diff_path) + \ - sizeof(struct combine_diff_parent) * (n) + (l) + 1) + st_add4(sizeof(struct combine_diff_path), (l), 1, \ + st_mult(sizeof(struct combine_diff_parent), (n)))extern void show_combined_diff (struct combine_diff_path *elem,int num_parent,int dense,struct rev_info *);
tree-diff.c +2 -2
@@ -124,8 +124,8 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last, unsigned mode, const unsigned char *sha1) { struct combine_diff_path *p; -int len = base->len + pathlen; -int alloclen =combine_diff_path_size (nparent, len); +size_t len =st_add (base->len, pathlen); +size_t alloclen =combine_diff_path_size (nparent, len);/* if last->next is !NULL - it is a pre-allocated memory, we can reuse */ p = last->next;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 4/21 harden REALLOC_ARRAY and xcalloc against size_t overflow
In reply to Jeff KingREALLOC_ARRAY inherently involves a multiplication which can overflow size_t, resulting in a much smaller buffer than we think we've allocated. We can easily harden it by using st_mult() to check for overflow. Likewise, we can add ALLOC_ARRAY to do the same thing for xmalloc calls.
xcalloc() should already be fine, because it takes the two factors separately, assuming the system calloc actually checks for overflow. However, before we even hit the system calloc(), we do our memory_limit_check, which involves a multiplication. Let's check for overflow ourselves so that this limit cannot be bypassed.
--- git-compat-util.h | 3 ++- wrapper.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-)
git-compat-util.h +2 -1
@@ -779,7 +779,8 @@ extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);extern char *xgetcwd (void );extern FILE *fopen_for_writing (const char *path); -#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) +#define ALLOC_ARRAY (x, alloc) (x) = xmalloc(st_mult((alloc), sizeof(*(x)))) +#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), st_mult((alloc), sizeof(*(x))))static inline char *xstrdup_or_null (const char *str) {
wrapper.c +3 -0
@@ -152,6 +152,9 @@ void *xcalloc(size_t nmemb, size_t size) {void *ret; +if (unsigned_mult_overflows (nmemb, size)) +die ("data too large to fit into virtual memory space" ); +memory_limit_check (size * nmemb,0 ); ret =calloc (nmemb, size);if (!ret && (!nmemb || !size))
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 5/21 add helpers for allocating flex-array structs
In reply to Jeff KingAllocating a struct with a flex array is pretty simple in
practice: you over-allocate the struct, then copy some data into the over-allocation. But it can be a slight pain to make sure you're allocating and copying the right amounts.
This patch adds a few helpers to turn simple cases of flex-array struct allocation into a one-liner that properly checks for overflow. See the embedded documentation for details.
Ideally we could provide a more flexible version that could handle multiple strings, like:
FLEX_ALLOC_FMT(ref, name, "%s%s", prefix, name);
But we have to implement this as a macro (because of the offset calculation of the flex member), which means we would need all compilers to support variadic macros.
--- git-compat-util.h | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
git-compat-util.h +62 -0
@@ -782,6 +782,68 @@ extern FILE *fopen_for_writing(const char *path);#define ALLOC_ARRAY (x, alloc) (x) = xmalloc(st_mult((alloc), sizeof(*(x))))#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), st_mult((alloc), sizeof(*(x)))) +/* +* These functions help you allocate structs with flex arrays, and copy +* the data directly into the array. For example, if you had: +* +* struct foo { +* int bar; +* char name[FLEX_ARRAY]; +* }; +* +* you can do: +* +* struct foo *f; +* FLEX_ALLOC_MEM(f, name, src, len); +* +* to allocate a "foo" with the contents of "src" in the "name" field. +* The resulting struct is automatically zero'd, and the flex-array field +* is NUL-terminated (whether the incoming src buffer was or not). +* +* The FLEXPTR_* variants operate on structs that don't use flex-arrays, +* but do want to store a pointer to some extra data in the same allocated +* block. For example, if you have: +* +* struct foo { +* char *name; +* int bar; +* }; +* +* you can do: +* +* struct foo *f; +* FLEX_ALLOC_STR(f, name, src); +* +* and "name" will point to a block of memory after the struct, which will be +* freed along with the struct (but the pointer can be repointed anywhere). +* +* The *_STR variants accept a string parameter rather than a ptr/len +* combination. +* +* Note that these macros will evaluate the first parameter multiple +* times, and it must be assignable as an lvalue. +*/ +#define FLEX_ALLOC_MEM (x, flexname, buf, len) do { \ + (x) = NULL;/* silence -Wuninitialized for offset calculation */ \ + (x) =xalloc_flex (sizeof (*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \ +} while (0) +#define FLEXPTR_ALLOC_MEM (x, ptrname, buf, len) do { \ + (x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \ + (x)->ptrname = (void *)((x)+1); \ +} while(0) +#define FLEX_ALLOC_STR (x, flexname, str) \ + FLEX_ALLOC_MEM((x), flexname, (str), strlen(str)) +#define FLEXPTR_ALLOC_STR (x, ptrname, str) \ + FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str)) + +static inline void *xalloc_flex (size_t base_len,size_t offset, +const void *src,size_t src_len) +{ +unsigned char *ret =xcalloc (1 ,st_add3 (base_len, src_len,1 )); +memcpy (ret + offset, src, src_len); +return ret; +} +static inline char *xstrdup_or_null (const char *str) {return str ?xstrdup (str) :NULL ;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 6/21 convert manual allocations to argv_array
In reply to Jeff KingThere are many manual argv allocations that predate the argv_array API. Switching to that API brings a few advantages:
1. We no longer have to manually compute the correct final array size (so it's one less thing we can screw up).
2. In many cases we had to make a separate pass to count, then allocate, then fill in the array. Now we can do it in one pass, making the code shorter and easier to follow.
3. argv_array handles memory ownership for us, making it more obvious when things should be free()d and and when not.
Most of these cases are pretty straightforward. In some, we switch from "run_command_v" to "run_command" which lets us directly use the argv_array embedded in "struct child_process".
--- builtin/grep.c | 10 +++++----- builtin/receive-pack.c | 12 +++--------- builtin/remote-ext.c | 26 +++++--------------------- daemon.c | 12 +++++------- git.c | 14 +++++--------- line-log.c | 26 ++++++++++---------------- remote-curl.c | 23 +++++++++++------------ 7 files changed, 44 insertions(+), 79 deletions(-)
builtin/grep.c +5 -5
@@ -365,17 +365,17 @@ static void append_path(struct grep_opt *opt, const void *data, size_t len)static void run_pager (struct grep_opt *opt,const char *prefix) {struct string_list *path_list = opt->output_priv; -const char **argv =xmalloc (sizeof (const char *) * (path_list->nr +1 )); +struct child_process child =CHILD_PROCESS_INIT ;int i, status;for (i =0 ; i < path_list->nr; i++) - argv[i] = path_list->items[i].string; - argv[path_list->nr] =NULL ; +argv_array_push (&child.args, path_list->items[i].string); + child.dir = prefix; + child.use_shell =1 ; - status =run_command_v_opt_cd_env (argv,RUN_USING_SHELL , prefix,NULL ); + status =run_command (&child);if (status)exit (status); -free (argv); }static int grep_cache (struct grep_opt *opt,const struct pathspec *pathspec,int cached)
builtin/receive-pack.c +3 -9
@@ -1031,7 +1031,6 @@ static void run_update_post_hook(struct command *commands) {struct command *cmd;int argc; -const char **argv;struct child_process proc =CHILD_PROCESS_INIT ;const char *hook;
@@ -1044,21 +1043,16 @@ static void run_update_post_hook(struct command *commands)if (!argc || !hook)return ; - argv =xmalloc (sizeof (*argv) * (2 + argc)); - argv[0 ] = hook; - -for (argc =1 , cmd = commands; cmd; cmd = cmd->next) { +argv_array_push (&proc.args, hook); +for (cmd = commands; cmd; cmd = cmd->next) {if (cmd->error_string || cmd->did_not_exist)continue ; - argv[argc] =xstrdup (cmd->ref_name); - argc++; +argv_array_push (&proc.args, cmd->ref_name); } - argv[argc] =NULL ; proc.no_stdin =1 ; proc.stdout_to_stderr =1 ; proc.err = use_sideband ?-1 :0 ; - proc.argv = argv;if (!start_command (&proc)) {if (use_sideband)
builtin/remote-ext.c +5 -21
@@ -114,30 +114,14 @@ static char *strip_escapes(const char *str, const char *service, } } -/* Should be enough... */ -#define MAXARGUMENTS 256 - -static const char **parse_argv (const char *arg,const char *service) +static void parse_argv (struct argv_array *out,const char *arg,const char *service) { -int arguments =0 ; -int i; -const char **ret; -char *temparray[MAXARGUMENTS +1 ]; -while (*arg) { -char *expanded; -if (arguments ==MAXARGUMENTS ) -die ("remote-ext command has too many arguments" ); - expanded =strip_escapes (arg, service, &arg); +char *expanded =strip_escapes (arg, service, &arg);if (expanded) - temparray[arguments++] = expanded; +argv_array_push (out, expanded); +free (expanded); } - - ret =xmalloc ((arguments +1 ) *sizeof (char *)); -for (i =0 ; i < arguments; i++) - ret[i] = temparray[i]; - ret[arguments] =NULL ; -return ret; } static void send_git_request(int stdin_fd, const char *serv, const char *repo,
@@ -158,7 +142,7 @@ static int run_child(const char *arg, const char *service) child.in =-1 ; child.out =-1 ; child.err =0 ; - child.argv =parse_argv (arg, service); +parse_argv (&child.args, arg, service);if (start_command (&child) <0 )die ("Can't run specified command" );
daemon.c +5 -7
@@ -808,7 +808,7 @@ static void check_dead_children(void) cradle = &blanket->next; } -static char **cld_argv; +struct argv_array cld_argv =ARGV_ARRAY_INIT ;static void handle (int incoming,struct sockaddr *addr,socklen_t addrlen) {struct child_process cld =CHILD_PROCESS_INIT ;
@@ -842,7 +842,7 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)#endif } - cld.argv = (const char **)cld_argv; + cld.argv = cld_argv.argv; cld.in = incoming; cld.out =dup (incoming);
@@ -1374,12 +1374,10 @@ int main(int argc, char **argv)write_file (pid_file,"%" PRIuMAX, (uintmax_t )getpid ());/* prepare argv for serving-processes */ - cld_argv =xmalloc (sizeof (char *) * (argc +2 )); - cld_argv[0 ] = argv[0 ];/* git-daemon */ - cld_argv[1 ] ="--serve" ; +argv_array_push (&cld_argv, argv[0 ]);/* git-daemon */ +argv_array_push (&cld_argv,"--serve" );for (i =1 ; i < argc; ++i) - cld_argv[i+1 ] = argv[i]; - cld_argv[argc+1 ] =NULL ; +argv_array_push (&cld_argv, argv[i]);return serve (&listen_addr, listen_port, cred); }
git.c +5 -9
@@ -247,20 +247,16 @@ static int handle_alias(int *argcp, const char ***argv) alias_string =alias_lookup (alias_command);if (alias_string) {if (alias_string[0 ] =='!' ) { -const char **alias_argv; -int argc = *argcp, i; +struct child_process child =CHILD_PROCESS_INIT ;commit_pager_choice ();restore_env (1 ); -/* build alias_argv */ - alias_argv =xmalloc (sizeof (*alias_argv) * (argc +1 )); - alias_argv[0 ] = alias_string +1 ; -for (i =1 ; i < argc; ++i) - alias_argv[i] = (*argv)[i]; - alias_argv[argc] =NULL ; + child.use_shell =1 ; +argv_array_push (&child.args, alias_string +1 ); +argv_array_pushv (&child.args, (*argv) +1 ); - ret =run_command_v_opt (alias_argv,RUN_USING_SHELL ); + ret =run_command (&child);if (ret >=0 )/* normal exit */ exit (ret);
line-log.c +10 -16
@@ -14,6 +14,7 @@#include "graph.h" #include "userdiff.h" #include "line-log.h" +#include "argv-array.h" static void range_set_grow (struct range_set *rs,size_t extra) {
@@ -746,23 +747,16 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_listadd_line_range (rev, commit, range);if (!rev->diffopt.detect_rename) { -int i, count =0 ; -struct line_log_data *r = range; -const char **paths; -while (r) { - count++; - r = r->next; - } - paths =xmalloc ((count+1 )*sizeof (char *)); - r = range; -for (i =0 ; i < count; i++) { - paths[i] =xstrdup (r->path); - r = r->next; - } - paths[count] =NULL ; +struct line_log_data *r; +struct argv_array paths =ARGV_ARRAY_INIT ; + +for (r = range; r; r = r->next) +argv_array_push (&paths, r->path);parse_pathspec (&rev->diffopt.pathspec,0 , -PATHSPEC_PREFER_FULL ,"" , paths); -free (paths); +PATHSPEC_PREFER_FULL ,"" , paths.argv); +/* argv strings are now owned by pathspec */ + paths.argc =0 ; +argv_array_clear (&paths); } }
remote-curl.c +11 -12
@@ -845,23 +845,22 @@ static void parse_fetch(struct strbuf *buf)static int push_dav (int nr_spec,char **specs) { -const char **argv =xmalloc ((10 + nr_spec) *sizeof (char *)); -int argc =0 , i; +struct child_process child =CHILD_PROCESS_INIT ; +size_t i; - argv[argc++] ="http-push" ; - argv[argc++] ="--helper-status" ; + child.git_cmd =1 ; +argv_array_push (&child.args,"http-push" ); +argv_array_push (&child.args,"--helper-status" );if (options.dry_run) - argv[argc++] ="--dry-run" ; +argv_array_push (&child.args,"--dry-run" );if (options.verbosity >1 ) - argv[argc++] ="--verbose" ; - argv[argc++] = url.buf; +argv_array_push (&child.args,"--verbose" ); +argv_array_push (&child.args, url.buf);for (i =0 ; i < nr_spec; i++) - argv[argc++] = specs[i]; - argv[argc++] =NULL ; +argv_array_push (&child.args, specs[i]); -if (run_command_v_opt (argv,RUN_GIT_CMD )) -die ("git-%s failed" , argv[0 ]); -free (argv); +if (run_command (&child)) +die ("git-http-push failed" );return 0 ; }
Signature · 1 line
2.7.1.577.gfed91b8
Re: [PATCH 06/21] convert manual allocations to argv_array
Quoted 42 lines · Jeff King
There are many manual argv allocations that predate the
argv_arrayAPI. Switching to that API brings a few advantages:1. We no longer have to manually compute the correct final array size (so it's one less thing we can screw up).
2. In many cases we had to make a separate pass to count, then allocate, then fill in the array. Now we can do it in one pass, making the code shorter and easier to follow.
3.
argv_arrayhandles memory ownership for us, making it more obvious when things should be free()d and and when not.Most of these cases are pretty straightforward. In some, we switch from "
run_command_v" to "run_command" which lets us directly use theargv_arrayembedded in "structchild_process".Signed-off-by Jeff King <[email protected]>---
line-log.c +2
-14@@ -746,23 +747,16 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_listif (!rev->diffopt.detect_rename) { -int i, count =0 ; -struct line_log_data *r = range; -const char **paths; -while (r) { - count++; - r = r->next; - } - paths =xmalloc ((count+1 )*sizeof (char *)); - r = range; -for (i =0 ; i < count; i++) { - paths[i] =xstrdup (r->path); - r = r->next; - } - paths[count] =NULL ; +struct line_log_data *r; +struct argv_array paths =ARGV_ARRAY_INIT ;
line-log.c void line_log_init(struct rev_info *rev, const char *prefix, struct string_list · line 757
Quoted 10 lines
line-log.c +7
-2+ +for (r = range; r; r = r->next) +argv_array_push (&paths, r->path);parse_pathspec (&rev->diffopt.pathspec,0 , -PATHSPEC_PREFER_FULL ,"" , paths); -free (paths); +PATHSPEC_PREFER_FULL ,"" , paths.argv); +/* argv strings are now owned by pathspec */ + paths.argc =0 ; +argv_array_clear (&paths);
This overly intimate knowledge of the internal implementation of argv_array_clear() is rather ugly.
Quoted 2 lines
line-log.c +0
-0} }
Re: [PATCH 06/21] convert manual allocations to argv_array
Quoted 24 lines · Eric Sunshine
Quoted 24 lines
line-log.c +5
-15@@ -746,23 +747,16 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_listif (!rev->diffopt.detect_rename) { -int i, count =0 ; -struct line_log_data *r = range; -const char **paths; -while (r) { - count++; - r = r->next; - } - paths =xmalloc ((count+1 )*sizeof (char *)); - r = range; -for (i =0 ; i < count; i++) { - paths[i] =xstrdup (r->path); - r = r->next; - } - paths[count] =NULL ; +struct line_log_data *r; +struct argv_array paths =ARGV_ARRAY_INIT ; + +for (r = range; r; r = r->next) +argv_array_push (&paths, r->path); parse_pathspec(&rev->diffopt.pathspec, 0, -PATHSPEC_PREFER_FULL ,"" , paths);
Quoted 8 lines
line-log.c void line_log_init(struct rev_info *rev, const char *prefix, struct string_list · line 759
Quoted 5 lines
line-log.c +4
-1-free (paths); + PATHSPEC_PREFER_FULL, "", paths.argv); +/* argv strings are now owned by pathspec */ + paths.argc =0 ; +argv_array_clear (&paths);This overly intimate knowledge of the internal implementation of
argv_array_clear()is rather ugly.
Yep, I agree. Suggestions?
We can just leak the array of "char *". This function is called only once per program invocation, and that's unlikely to change.
I guess we can make an argv_array_detach_strings() function. Or maybe even just argv_array_detach() would be less gross, and then this function could manually free the array but not the strings themselves.
-Peff
Re: [PATCH 06/21] convert manual allocations to argv_array
Quoted 8 lines · Jeff King
Quoted 6 lines · Eric Sunshine
line-log.c void line_log_init(struct rev_info *rev, const char *prefix, struct string_list · line 759
Quoted 3 lines
line-log.c +3
-0+/* argv strings are now owned by pathspec */ + paths.argc =0 ; +argv_array_clear (&paths);This overly intimate knowledge of the internal implementation of
argv_array_clear()is rather ugly.
Quoted 8 lines
Yep, I agree. Suggestions?
We can just leak the array of "char *". This function is called only once per program invocation, and that's unlikely to change.
I guess we can make an
argv_array_detach_strings()function. Or maybe even justargv_array_detach()would be less gross, and then this function could manually free the array but not the strings themselves.
The latter is what I was thinking, and I agree that argv_array_detach() would be less gross than argv_array_detach_strings(), however, it also feels a bit wrong since this sort of ownership transfer is kind of out of scope for argv_array.
I wonder if a simple "dup'ing" string_list would be more suitable for this case. You'd have to append the NULL item manually with string_list_append_nodup(), and string_list_clear() would then be the correct way to dispose of the list without intimate knowledge of its implementation and no need for an API extension.
Re: [PATCH 06/21] convert manual allocations to argv_array
Quoted 24 lines · Eric Sunshine
Quoted 8 lines · Jeff King
Quoted 6 lines · Eric Sunshine
line-log.c void line_log_init(struct rev_info *rev, const char *prefix, struct string_list · line 759
Quoted 3 lines
line-log.c +3
-0+/* argv strings are now owned by pathspec */ + paths.argc =0 ; +argv_array_clear (&paths);This overly intimate knowledge of the internal implementation of
argv_array_clear()is rather ugly.Quoted 8 lines
Yep, I agree. Suggestions?
We can just leak the array of "char *". This function is called only once per program invocation, and that's unlikely to change.
I guess we can make an
argv_array_detach_strings()function. Or maybe even justargv_array_detach()would be less gross, and then this function could manually free the array but not the strings themselves.The latter is what I was thinking, and I agree that
argv_array_detach()would be less gross thanargv_array_detach_strings(), however, it also feels a bit wrong since this sort of ownership transfer is kind of out of scope forargv_array.
Quoted 5 lines
I wonder if a simple "dup'ing"
string_listwould be more suitable for this case. You'd have to append the NULL item manually withstring_list_append_nodup(), andstring_list_clear()would then be the correct way to dispose of the list without intimate knowledge of its implementation and no need for an API extension.
A string_list doesn't just store pointers; it's a struct with a util field. So you can't pass it to things expecting a "const char **".
I think argv_array_detach() is the least-bad thing here. It matches strbuf_detach() to say "you now own the storage" (as opposed to just peeking at argv.argv, which we should do only in a read-only way).
-Peff
Re: [PATCH 06/21] convert manual allocations to argv_array
Quoted 24 lines · Jeff King
Quoted 16 lines · Eric Sunshine
Quoted 8 lines · Jeff King
Quoted 6 lines · Eric Sunshine
line-log.c void line_log_init(struct rev_info *rev, const char *prefix, struct string_list · line 759
Quoted 3 lines
line-log.c +3
-0+/* argv strings are now owned by pathspec */ + paths.argc =0 ; +argv_array_clear (&paths);This overly intimate knowledge of the internal implementation of
argv_array_clear()is rather ugly.Quoted 6 lines
Yep, I agree. Suggestions?
[...]I guess we can make an
argv_array_detach_strings()function. Or maybe even justargv_array_detach()would be less gross, and then this function could manually free the array but not the strings themselves.Quoted 6 lines
[...]
I wonder if a simple "dup'ing"string_listwould be more suitable for this case. You'd have to append the NULL item manually withstring_list_append_nodup(), andstring_list_clear()would then be the correct way to dispose of the list without intimate knowledge of its implementation and no need for an API extension.
Quoted 2 lines
A
string_listdoesn't just store pointers; it's a struct with a util field. So you can't pass it to things expecting a "const char **".
Yep, I knew that but wasn't thinking straight.
Quoted 3 lines
I think
argv_array_detach()is the least-bad thing here. It matchesstrbuf_detach()to say "you now own the storage" (as opposed to just peeking at argv.argv, which we should do only in a read-only way).
I also had made the strbuf_detach() analogy in my response but deleted it before sending; I do think it's a reasonable API template to mirror via new argv_array_detach().
PATCH v2 7/21 convert trivial cases to ALLOC_ARRAY
In reply to Jeff KingEach of these cases can be converted to use ALLOC_ARRAY or REALLOC_ARRAY, which has two advantages:
1. It automatically checks the array-size multiplication for overflow.
2. It always uses sizeof(*array) for the element-size, so that it can never go out of sync with the declared type of the array.
--- alias.c | 2 +- attr.c | 2 +- bisect.c | 4 ++-- builtin/blame.c | 3 ++- builtin/clean.c | 2 +- builtin/fast-export.c | 2 +- builtin/index-pack.c | 4 ++-- builtin/merge-base.c | 2 +- builtin/mv.c | 3 ++- builtin/pack-objects.c | 7 ++++--- builtin/pack-redundant.c | 2 +- builtin/receive-pack.c | 5 ++--- column.c | 2 +- combine-diff.c | 4 ++-- commit.c | 2 +- compat/mingw.c | 6 +++--- diffcore-order.c | 4 ++-- dir.c | 6 +++--- fast-import.c | 5 +++-- fsck.c | 3 ++- graph.c | 10 ++++------ khash.h | 2 +- levenshtein.c | 8 +++++--- line-log.c | 8 ++++---- notes.c | 2 +- pack-check.c | 2 +- pack-revindex.c | 12 ++++++++---- pathspec.c | 5 +++-- remote-curl.c | 3 ++- sha1_file.c | 4 ++-- shallow.c | 6 +++--- show-index.c | 3 ++- transport.c | 2 +- xdiff-interface.c | 2 +- 34 files changed, 75 insertions(+), 64 deletions(-)
alias.c +1 -1
@@ -23,7 +23,7 @@ int split_cmdline(char *cmdline, const char ***argv)int src, dst, count =0 , size =16 ;char quoted =0 ; - *argv =xmalloc (sizeof (**argv) * size); +ALLOC_ARRAY (*argv, size);/* split alias_string */ (*argv)[count++] = cmdline;
attr.c +1 -1
@@ -799,7 +799,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check) ++count; } *num = count; - *check =xmalloc (sizeof (**check) * count); +ALLOC_ARRAY (*check, count); j =0 ;for (i =0 ; i < attr_nr; i++) {const char *value = check_all_attr[i].value;
bisect.c +2 -2
@@ -708,10 +708,10 @@ static struct commit *get_commit_reference(const unsigned char *sha1)static struct commit **get_bad_and_good_commits (int *rev_nr) { -int len =1 + good_revs.nr; -struct commit **rev =xmalloc (len *sizeof (*rev)); +struct commit **rev;int i, n =0 ; +ALLOC_ARRAY (rev,1 + good_revs.nr); rev[n++] =get_commit_reference (current_bad_oid->hash);for (i =0 ; i < good_revs.nr; i++) rev[n++] =get_commit_reference (good_revs.sha1[i]);
builtin/blame.c +2 -1
@@ -2059,7 +2059,8 @@ static int prepare_lines(struct scoreboard *sb)for (p = buf; p < end; p =get_next_line (p, end)) num++; - sb->lineno = lineno =xmalloc (sizeof (*sb->lineno) * (num +1 )); +ALLOC_ARRAY (sb->lineno, num +1 ); + lineno = sb->lineno;for (p = buf; p < end; p =get_next_line (p, end)) *lineno++ = p - buf;
builtin/clean.c +1 -1
@@ -543,7 +543,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)int eof =0 ;int i; - chosen =xmalloc (sizeof (int ) * stuff->nr); +ALLOC_ARRAY (chosen, stuff->nr);/* set chosen as uninitialized */ for (i =0 ; i < stuff->nr; i++) chosen[i] =-1 ;
builtin/fast-export.c +1 -1
@@ -1021,7 +1021,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)const char **refspecs_str;int i; - refspecs_str =xmalloc (sizeof (*refspecs_str) * refspecs_list.nr); +ALLOC_ARRAY (refspecs_str, refspecs_list.nr);for (i =0 ; i < refspecs_list.nr; i++) refspecs_str[i] = refspecs_list.items[i].string;
builtin/index-pack.c +2 -2
@@ -1346,7 +1346,7 @@ static void fix_unresolved_deltas(struct sha1file *f)* before deltas depending on them, a good heuristic is to start * resolving deltas in the same order as their position in the pack. */ - sorted_by_pos =xmalloc (nr_ref_deltas *sizeof (*sorted_by_pos)); +ALLOC_ARRAY (sorted_by_pos, nr_ref_deltas);for (i =0 ; i < nr_ref_deltas; i++) sorted_by_pos[i] = &ref_deltas[i];qsort (sorted_by_pos, nr_ref_deltas,sizeof (*sorted_by_pos), delta_pos_compare);
@@ -1759,7 +1759,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)if (show_stat)show_pack_info (stat_only); - idx_objects =xmalloc ((nr_objects) *sizeof (struct pack_idx_entry *)); +ALLOC_ARRAY (idx_objects, nr_objects);for (i =0 ; i < nr_objects; i++) idx_objects[i] = &objects[i].idx; curr_index =write_idx_file (index_name, idx_objects, nr_objects, &opts, pack_sha1);
builtin/merge-base.c +1 -1
@@ -252,7 +252,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)if (argc <2 )usage_with_options (merge_base_usage, options); - rev =xmalloc (argc *sizeof (*rev)); +ALLOC_ARRAY (rev, argc);while (argc-- >0 ) rev[rev_nr++] =get_commit_reference (*argv++);return show_merge_base (rev, rev_nr, show_all);
builtin/mv.c +2 -1
@@ -24,7 +24,8 @@ static const char **internal_copy_pathspec(const char *prefix, int count, unsigned flags) { int i; -const char **result =xmalloc ((count +1 ) *sizeof (const char *)); +const char **result; +ALLOC_ARRAY (result, count +1 );memcpy (result, pathspec, count *sizeof (const char *)); result[count] =NULL ;for (i =0 ; i < count; i++) {
builtin/pack-objects.c +4 -3
@@ -624,7 +624,7 @@ static struct object_entry **compute_write_order(void) {unsigned int i, wo_end, last_untagged; -struct object_entry **wo =xmalloc (to_pack.nr_objects *sizeof (*wo)); +struct object_entry **wo;struct object_entry *objects = to_pack.objects;for (i =0 ; i < to_pack.nr_objects; i++) {
@@ -657,6 +657,7 @@ static struct object_entry **compute_write_order(void)* Give the objects in the original recency order until * we see a tagged tip. */ +ALLOC_ARRAY (wo, to_pack.nr_objects);for (i = wo_end =0 ; i < to_pack.nr_objects; i++) {if (objects[i].tagged)break ;
@@ -769,7 +770,7 @@ static void write_pack_file(void)if (progress > pack_to_stdout) progress_state =start_progress (_ ("Writing objects" ), nr_result); - written_list =xmalloc (to_pack.nr_objects *sizeof (*written_list)); +ALLOC_ARRAY (written_list, to_pack.nr_objects); write_order =compute_write_order (); do {
@@ -2129,7 +2130,7 @@ static void prepare_pack(int window, int depth)if (!to_pack.nr_objects || !window || !depth)return ; - delta_list =xmalloc (to_pack.nr_objects *sizeof (*delta_list)); +ALLOC_ARRAY (delta_list, to_pack.nr_objects); nr_deltas = n =0 ;for (i =0 ; i < to_pack.nr_objects; i++) {
builtin/pack-redundant.c +1 -1
@@ -53,7 +53,7 @@ static inline struct llist_item *llist_item_get(void) free_nodes = free_nodes->next; }else {int i =1 ; - new =xmalloc (sizeof (struct llist_item ) *BLKSIZE ); +ALLOC_ARRAY (new,BLKSIZE );for (; i <BLKSIZE ; i++)llist_item_put (&new[i]); }
builtin/receive-pack.c +2 -3
@@ -1591,8 +1591,7 @@ static void prepare_shallow_update(struct command *commands, {int i, j, k, bitmap_size = (si->ref->nr +31 ) /32 ; - si->used_shallow =xmalloc (sizeof (*si->used_shallow) * - si->shallow->nr); +ALLOC_ARRAY (si->used_shallow, si->shallow->nr);assign_shallow_commits_to_refs (si, si->used_shallow,NULL ); si->need_reachability_test =
@@ -1658,7 +1657,7 @@ static void update_shallow_info(struct command *commands,return ; } - ref_status =xmalloc (sizeof (*ref_status) * ref->nr); +ALLOC_ARRAY (ref_status, ref->nr);assign_shallow_commits_to_refs (si,NULL , ref_status);for (cmd = commands; cmd; cmd = cmd->next) {if (is_null_sha1 (cmd->new_sha1))
column.c +1 -1
@@ -164,7 +164,7 @@ static void display_table(const struct string_list *list, data.colopts = colopts; data.opts = *opts; - data.len =xmalloc (sizeof (*data.len) * list->nr); +ALLOC_ARRAY (data.len, list->nr);for (i =0 ; i < list->nr; i++) data.len[i] =item_length (colopts, list->items[i].string);
combine-diff.c +2 -2
@@ -1372,7 +1372,7 @@ static struct combine_diff_path *find_paths_multitree(struct combine_diff_path paths_head;struct strbuf base; - parents_sha1 =xmalloc (nparent *sizeof (parents_sha1[0 ])); +ALLOC_ARRAY (parents_sha1, nparent);for (i =0 ; i < nparent; i++) parents_sha1[i] = parents->sha1[i];
@@ -1483,7 +1483,7 @@ void diff_tree_combined(const unsigned char *sha1,if (opt->orderfile && num_paths) {struct obj_order *o; - o =xmalloc (sizeof (*o) * num_paths); +ALLOC_ARRAY (o, num_paths);for (i =0 , p = paths; p; p = p->next, i++) o[i].obj = p;order_objects (opt->orderfile, path_path, o, num_paths);
commit.c +1 -1
@@ -903,7 +903,7 @@ static int remove_redundant(struct commit **array, int cnt) work =xcalloc (cnt,sizeof (*work)); redundant =xcalloc (cnt,1 ); - filled_index =xmalloc (sizeof (*filled_index) * (cnt -1 )); +ALLOC_ARRAY (filled_index, cnt -1 );for (i =0 ; i < cnt; i++)parse_commit (array[i]);
compat/mingw.c +3 -3
@@ -893,7 +893,7 @@ static char **get_path_split(void)if (!n)return NULL ; - path =xmalloc ((n+1 )*sizeof (char *)); +ALLOC_ARRAY (path, n +1 ); p = envpath; i =0 ; do {
@@ -978,7 +978,7 @@ static wchar_t *make_environment_block(char **deltaenv) i++;/* copy the environment, leaving space for changes */ - tmpenv =xmalloc ((size + i) *sizeof (char *)); +ALLOC_ARRAY (tmpenv, size + i);memcpy (tmpenv, environ, size *sizeof (char *));/* merge supplied environment changes into the temporary environment */
@@ -1168,7 +1168,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)int argc =0 ;const char **argv2;while (argv[argc]) argc++; - argv2 =xmalloc (sizeof (*argv) * (argc+1 )); +ALLOC_ARRAY (argv2, argc +1 ); argv2[0 ] = (char *)cmd;/* full path to the script file */ memcpy (&argv2[1 ], &argv[1 ],sizeof (*argv) * argc); pid =mingw_spawnv (prog, argv2,1 );
diffcore-order.c +2 -2
@@ -52,7 +52,7 @@ static void prepare_order(const char *orderfile) }if (pass ==0 ) { order_cnt = cnt; - order =xmalloc (sizeof (*order) * cnt); +ALLOC_ARRAY (order, cnt); } } }
@@ -120,7 +120,7 @@ void diffcore_order(const char *orderfile)if (!q->nr)return ; - o =xmalloc (sizeof (*o) * q->nr); +ALLOC_ARRAY (o, q->nr);for (i =0 ; i < q->nr; i++) o[i].obj = q->queue[i];order_objects (orderfile, pair_pathtwo, o, q->nr);
dir.c +3 -3
@@ -2484,14 +2484,14 @@ static int read_one_dir(struct untracked_cache_dir **untracked_, ud.untracked_alloc = value; ud.untracked_nr = value;if (ud.untracked_nr) - ud.untracked =xmalloc (sizeof (*ud.untracked) * ud.untracked_nr); +ALLOC_ARRAY (ud.untracked, ud.untracked_nr); data = next; next = data; ud.dirs_alloc = ud.dirs_nr =decode_varint (&next);if (next > end)return -1 ; - ud.dirs =xmalloc (sizeof (*ud.dirs) * ud.dirs_nr); +ALLOC_ARRAY (ud.dirs, ud.dirs_nr); data = next; len =strlen ((const char *)data);
@@ -2611,7 +2611,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long rd.data = next; rd.end = end; rd.index =0 ; - rd.ucd =xmalloc (sizeof (*rd.ucd) * len); +ALLOC_ARRAY (rd.ucd, len);if (read_one_dir (&uc->root, &rd) || rd.index != len) goto done;
fast-import.c +3 -2
@@ -814,7 +814,8 @@ static struct tree_entry *new_tree_entry(void)if (!avail_tree_entry) {unsigned int n = tree_entry_alloc; total_allocd += n *sizeof (struct tree_entry ); - avail_tree_entry = e =xmalloc (n *sizeof (struct tree_entry )); +ALLOC_ARRAY (e, n); + avail_tree_entry = e;while (n-- >1 ) { *((void **)e) = e +1 ; e++;
@@ -898,7 +899,7 @@ static const char *create_index(void)struct object_entry_pool *o;/* Build the table of object IDs. */ - idx =xmalloc (object_count *sizeof (*idx)); +ALLOC_ARRAY (idx, object_count); c = idx;for (o = blocks; o; o = o->next_pool)for (e = o->next_free; e-- != o->entries;)
fsck.c +2 -1
@@ -199,7 +199,8 @@ void fsck_set_msg_type(struct fsck_options *options,if (!options->msg_type) {int i; -int *msg_type =xmalloc (sizeof (int ) *FSCK_MSG_MAX ); +int *msg_type; +ALLOC_ARRAY (msg_type,FSCK_MSG_MAX );for (i =0 ; i <FSCK_MSG_MAX ; i++) msg_type[i] =fsck_msg_type (i, options); options->msg_type = msg_type;
graph.c +4 -6
@@ -234,12 +234,10 @@ struct git_graph *graph_init(struct rev_info *opt)* We'll automatically grow columns later if we need more room. */ graph->column_capacity =30 ; - graph->columns =xmalloc (sizeof (struct column ) * - graph->column_capacity); - graph->new_columns =xmalloc (sizeof (struct column ) * - graph->column_capacity); - graph->mapping =xmalloc (sizeof (int ) *2 * graph->column_capacity); - graph->new_mapping =xmalloc (sizeof (int ) *2 * graph->column_capacity); +ALLOC_ARRAY (graph->columns, graph->column_capacity); +ALLOC_ARRAY (graph->new_columns, graph->column_capacity); +ALLOC_ARRAY (graph->mapping,2 * graph->column_capacity); +ALLOC_ARRAY (graph->new_mapping,2 * graph->column_capacity);/* * The diff output prefix callback, with this we can make
khash.h +1 -1
@@ -117,7 +117,7 @@ static const double __ac_HASH_UPPER = 0.77;if (new_n_buckets <4 ) new_n_buckets =4 ; \if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER +0.5 )) j =0 ;/* requested size is too small */ \else {/* hash table size to be changed (shrink or expand); rehash */ \ - new_flags = (khint32_t *)xmalloc (__ac_fsize (new_n_buckets) *sizeof (khint32_t)); \ +ALLOC_ARRAY (new_flags,__ac_fsize (new_n_buckets)); \if (!new_flags)return -1 ; \memset (new_flags,0xaa ,__ac_fsize (new_n_buckets) *sizeof (khint32_t)); \if (h->n_buckets < new_n_buckets) {/* expand */ \
levenshtein.c +5 -3
@@ -42,11 +42,13 @@ int levenshtein(const char *string1, const char *string2, int w, int s, int a, int d) { int len1 = strlen(string1), len2 = strlen(string2); -int *row0 =xmalloc (sizeof (int ) * (len2 +1 )); -int *row1 =xmalloc (sizeof (int ) * (len2 +1 )); -int *row2 =xmalloc (sizeof (int ) * (len2 +1 )); +int *row0, *row1, *row2;int i, j; +ALLOC_ARRAY (row0, len2 +1 ); +ALLOC_ARRAY (row1, len2 +1 ); +ALLOC_ARRAY (row2, len2 +1 ); +for (j =0 ; j <= len2; j++) row1[j] = j * a;for (i =0 ; i < len1; i++) {
line-log.c +4 -4
@@ -522,7 +522,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,if (diff_populate_filespec (spec,0 ))die ("Cannot read blob %s" ,sha1_to_hex (spec->sha1)); - ends =xmalloc (size *sizeof (*ends)); +ALLOC_ARRAY (ends, size); ends[cur++] =0 ; data = spec->data;while (num < spec->size) {
@@ -1140,9 +1140,9 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *commif (nparents >1 && rev->first_parent_only) nparents =1 ; - diffqueues =xmalloc (nparents *sizeof (*diffqueues)); - cand =xmalloc (nparents *sizeof (*cand)); - parents =xmalloc (nparents *sizeof (*parents)); +ALLOC_ARRAY (diffqueues, nparents); +ALLOC_ARRAY (cand, nparents); +ALLOC_ARRAY (parents, nparents); p = commit->parents;for (i =0 ; i < nparents; i++) {
notes.c +1 -1
@@ -1035,7 +1035,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs, int flags)struct string_list_item *item;int counter =0 ;struct notes_tree **trees; - trees =xmalloc ((refs->nr+1 ) *sizeof (struct notes_tree *)); +ALLOC_ARRAY (trees, refs->nr +1 );for_each_string_list_item (item, refs) {struct notes_tree *t =xcalloc (1 ,sizeof (struct notes_tree ));init_notes (t, item->string, combine_notes_ignore, flags);
pack-check.c +1 -1
@@ -89,7 +89,7 @@ static int verify_packfile(struct packed_git *p,* we do not do scan-streaming check on the pack file. */ nr_objects = p->num_objects; - entries =xmalloc ((nr_objects +1 ) *sizeof (*entries)); +ALLOC_ARRAY (entries, nr_objects +1 ); entries[nr_objects].offset = pack_sig_ofs;/* first sort entries by pack offset, since unpacking them is more efficient that way */ for (i =0 ; i < nr_objects; i++) {
pack-revindex.c +8 -4
@@ -44,10 +44,14 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)* keep track of them with alias pointers, always sorting from "from" * to "to". */ -struct revindex_entry *tmp =xmalloc (n *sizeof (*tmp)); -struct revindex_entry *from = entries, *to = tmp; +struct revindex_entry *tmp, *from, *to;int bits; -unsigned *pos =xmalloc (BUCKETS *sizeof (*pos)); +unsigned *pos; + +ALLOC_ARRAY (pos,BUCKETS ); +ALLOC_ARRAY (tmp, n); + from = entries; + to = tmp;/* * If (max >> bits) is zero, then we know that the radix digit we are
@@ -121,7 +125,7 @@ static void create_pack_revindex(struct packed_git *p)unsigned i;const char *index = p->index_data; - p->revindex =xmalloc (sizeof (*p->revindex) * (num_ent +1 )); +ALLOC_ARRAY (p->revindex, num_ent +1 ); index +=4 *256 ;if (p->index_version >1 ) {
pathspec.c +3 -2
@@ -406,7 +406,8 @@ void parse_pathspec(struct pathspec *pathspec, n++; pathspec->nr = n; - pathspec->items = item =xmalloc (sizeof (*item) * n); +ALLOC_ARRAY (pathspec->items, n); + item = pathspec->items; pathspec->_raw = argv; prefixlen = prefix ?strlen (prefix) :0 ;
@@ -483,7 +484,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)void copy_pathspec (struct pathspec *dst,const struct pathspec *src) { *dst = *src; - dst->items =xmalloc (sizeof (struct pathspec_item ) * dst->nr); +ALLOC_ARRAY (dst->items, dst->nr);memcpy (dst->items, src->items,sizeof (struct pathspec_item ) * dst->nr); }
remote-curl.c +2 -1
@@ -696,9 +696,10 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)static int fetch_dumb (int nr_heads,struct ref **to_fetch) {struct walker *walker; -char **targets =xmalloc (nr_heads *sizeof (char *)); +char **targets;int ret, i; +ALLOC_ARRAY (targets, nr_heads);if (options.depth)die ("dumb http transport does not support --depth" );for (i =0 ; i < nr_heads; i++)
sha1_file.c +2 -2
@@ -1942,7 +1942,7 @@ static enum object_type packed_to_object_type(struct packed_git *p,/* Push the object we're going to leave behind */ if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) { poi_stack_alloc =alloc_nr (poi_stack_nr); - poi_stack =xmalloc (sizeof (off_t)*poi_stack_alloc); +ALLOC_ARRAY (poi_stack, poi_stack_alloc);memcpy (poi_stack, small_poi_stack,sizeof (off_t)*poi_stack_nr); }else {ALLOC_GROW (poi_stack, poi_stack_nr+1 , poi_stack_alloc);
@@ -2308,7 +2308,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,if (delta_stack_nr >= delta_stack_alloc && delta_stack == small_delta_stack) { delta_stack_alloc =alloc_nr (delta_stack_nr); - delta_stack =xmalloc (sizeof (*delta_stack)*delta_stack_alloc); +ALLOC_ARRAY (delta_stack, delta_stack_alloc);memcpy (delta_stack, small_delta_stack,sizeof (*delta_stack)*delta_stack_nr); }else {
shallow.c +3 -3
@@ -315,8 +315,8 @@ void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa) info->shallow = sa;if (!sa)return ; - info->ours =xmalloc (sizeof (*info->ours) * sa->nr); - info->theirs =xmalloc (sizeof (*info->theirs) * sa->nr); +ALLOC_ARRAY (info->ours, sa->nr); +ALLOC_ARRAY (info->theirs, sa->nr);for (i =0 ; i < sa->nr; i++) {if (has_sha1_file (sa->sha1[i])) {struct commit_graft *graft;
@@ -487,7 +487,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,struct paint_info pi;trace_printf_key (&trace_shallow,"shallow: assign_shallow_commits_to_refs\n" ); - shallow =xmalloc (sizeof (*shallow) * (info->nr_ours + info->nr_theirs)); +ALLOC_ARRAY (shallow, info->nr_ours + info->nr_theirs);for (i =0 ; i < info->nr_ours; i++) shallow[nr_shallow++] = info->ours[i];for (i =0 ; i < info->nr_theirs; i++)
show-index.c +2 -1
@@ -50,7 +50,8 @@ int main(int argc, char **argv)unsigned char sha1[20 ];uint32_t crc;uint32_t off; - } *entries =xmalloc (nr *sizeof (entries[0 ])); + } *entries; +ALLOC_ARRAY (entries, nr);for (i =0 ; i < nr; i++)if (fread (entries[i].sha1,20 ,1 , stdin) !=1 )die ("unable to read sha1 %u/%u" , i, nr);
transport.c +1 -1
@@ -977,7 +977,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)* This condition shouldn't be met in a non-deepening fetch * (see builtin/fetch.c:quickfetch()). */ - heads =xmalloc (nr_refs *sizeof (*heads)); +ALLOC_ARRAY (heads, nr_refs);for (rm = refs; rm; rm = rm->next) heads[nr_heads++] = rm; }
xdiff-interface.c +1 -1
@@ -265,7 +265,7 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)for (i =0 , regs->nr =1 ; value[i]; i++)if (value[i] =='\n' ) regs->nr++; - regs->array =xmalloc (regs->nr *sizeof (struct ff_reg )); +ALLOC_ARRAY (regs->array, regs->nr);for (i =0 ; i < regs->nr; i++) {struct ff_reg *reg = regs->array + i;const char *ep =strchr (value,'\n' ), *expression;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 8/21 use xmallocz to avoid size arithmetic
In reply to Jeff KingWe frequently allocate strings as xmalloc(len + 1), where the extra 1 is for the NUL terminator. This can be done more simply with xmallocz, which also checks for integer overflow.
There's no case where switching xmalloc(n+1) to xmallocz(n) is wrong; the result is the same length, and malloc made no guarantees about what was in the buffer anyway. But in some cases, we can stop manually placing NUL at the end of the allocated buffer. But that's only safe if it's clear that the contents will always fill the buffer.
In each case where this patch does so, I manually examined the control flow, and I tried to err on the side of caution.
--- builtin/check-ref-format.c | 2 +- builtin/merge-tree.c | 2 +- builtin/worktree.c | 2 +- column.c | 3 +-- combine-diff.c | 4 +--- config.c | 4 +--- dir.c | 2 +- entry.c | 2 +- grep.c | 3 +-- imap-send.c | 5 ++--- ll-merge.c | 2 +- progress.c | 2 +- refs.c | 2 +- setup.c | 5 ++--- strbuf.c | 2 +- 15 files changed, 17 insertions(+), 25 deletions(-)
builtin/check-ref-format.c +1 -1
@@ -20,7 +20,7 @@ static const char builtin_check_ref_format_usage[] =*/ static char *collapse_slashes (const char *refname) { -char *ret =xmalloc (strlen (refname) +1 ); +char *ret =xmallocz (strlen (refname));char ch;char prev ='/' ;char *cp = ret;
builtin/merge-tree.c +1 -1
@@ -174,7 +174,7 @@ static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsistatic char *traverse_path (const struct traverse_info *info,const struct name_entry *n) { -char *path =xmalloc (traverse_path_len (info, n) +1 ); +char *path =xmallocz (traverse_path_len (info, n));return make_traverse_path (path, info, n); }
builtin/worktree.c +1 -1
@@ -52,7 +52,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)return 1 ; } len = st.st_size; - path =xmalloc (len +1 ); + path =xmallocz (len);read_in_full (fd, path, len);close (fd);while (len && (path[len -1 ] =='\n' || path[len -1 ] =='\r' ))
column.c +1 -2
@@ -173,9 +173,8 @@ static void display_table(const struct string_list *list,if (colopts &COL_DENSE )shrink_columns (&data); - empty_cell =xmalloc (initial_width +1 ); + empty_cell =xmallocz (initial_width);memset (empty_cell,' ' , initial_width); - empty_cell[initial_width] ='\0' ;for (y =0 ; y < data.rows; y++) {for (x =0 ; x < data.cols; x++)if (display_cell (&data, initial_width, empty_cell, x, y))
combine-diff.c +1 -3
@@ -1043,7 +1043,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, elem->mode =canon_mode (S_IFLNK ); result_size = len; - result =xmalloc (len +1 ); + result =xmallocz (len); done =read_in_full (fd, result, len);if (done <0 )
@@ -1051,8 +1051,6 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,else if (done < len)die ("early EOF '%s'" , elem->path); - result[len] =0 ; -/* If not a fake symlink, apply filters, e.g. autocrlf */ if (is_file) {struct strbuf buf =STRBUF_INIT ;
config.c +1 -3
@@ -1902,7 +1902,7 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *basele* Validate the key and while at it, lower case it for matching. */ if (store_key) - *store_key =xmalloc (strlen (key) +1 ); + *store_key =xmallocz (strlen (key)); dot =0 ;for (i =0 ; key[i]; i++) {
@@ -1926,8 +1926,6 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *baseleif (store_key) (*store_key)[i] = c; } -if (store_key) - (*store_key)[i] =0 ;return 0 ;
dir.c +1 -1
@@ -711,7 +711,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,close (fd);return 0 ; } - buf =xmalloc (size+1 ); + buf =xmallocz (size);if (read_in_full (fd, buf, size) != size) {free (buf);close (fd);
entry.c +1 -1
@@ -6,7 +6,7 @@static void create_directories (const char *path,int path_len,const struct checkout *state) { -char *buf =xmalloc (path_len +1 ); +char *buf =xmallocz (path_len);int len =0 ;while (len < path_len) {
grep.c +1 -2
@@ -1741,7 +1741,7 @@ static int grep_source_load_file(struct grep_source *gs) i =open (filename,O_RDONLY );if (i <0 ) goto err_ret; - data =xmalloc (size +1 ); + data =xmallocz (size);if (st.st_size !=read_in_full (i, data, size)) {error (_ ("'%s': short read %s" ), filename,strerror (errno));close (i);
@@ -1749,7 +1749,6 @@ static int grep_source_load_file(struct grep_source *gs)return -1 ; }close (i); - data[size] =0 ; gs->buf = data; gs->size = size;
imap-send.c +2 -3
@@ -892,12 +892,11 @@ static char *cram(const char *challenge_64, const char *user, const char *pass) response =xstrfmt ("%s %s" , user, hex); resp_len =strlen (response) +1 ; - response_64 =xmalloc (ENCODED_SIZE (resp_len) +1 ); + response_64 =xmallocz (ENCODED_SIZE (resp_len)); encoded_len =EVP_EncodeBlock ((unsigned char *)response_64, (unsigned char *)response, resp_len);if (encoded_len <0 )die ("EVP_EncodeBlock error" ); - response_64[encoded_len] ='\0' ;return (char *)response_64; }
@@ -1188,7 +1187,7 @@ static void lf_to_crlf(struct strbuf *msg) j++; } - new =xmalloc (j +1 ); + new =xmallocz (j);/* * Second pass: write the new string. Note that this loop is
ll-merge.c +1 -1
@@ -205,7 +205,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,if (fstat (fd, &st)) goto close_bad; result->size = st.st_size; - result->ptr =xmalloc (result->size +1 ); + result->ptr =xmallocz (result->size);if (read_in_full (fd, result->ptr, result->size) != result->size) {free (result->ptr); result->ptr =NULL ;
progress.c +1 -1
@@ -247,7 +247,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)size_t len =strlen (msg) +5 ;struct throughput *tp = progress->throughput; - bufp = (len <sizeof (buf)) ? buf :xmalloc (len +1 ); + bufp = (len <sizeof (buf)) ? buf :xmallocz (len);if (tp) {unsigned int rate = !tp->avg_misecs ?0 : tp->avg_bytes / tp->avg_misecs;
refs.c +1 -1
@@ -124,7 +124,7 @@ int refname_is_safe(const char *refname)char *buf;int result; - buf =xmalloc (strlen (refname) +1 ); + buf =xmallocz (strlen (refname));/* * Does the refname try to escape refs/? * For example: refs/foo/../bar is safe but refs/foo/../../bar
setup.c +2 -3
@@ -88,7 +88,7 @@ char *prefix_path_gently(const char *prefix, int len,const char *orig = path;char *sanitized;if (is_absolute_path (orig)) { - sanitized =xmalloc (strlen (path) +1 ); + sanitized =xmallocz (strlen (path));if (remaining_prefix) *remaining_prefix =0 ;if (normalize_path_copy_len (sanitized, path, remaining_prefix)) {
@@ -488,14 +488,13 @@ const char *read_gitfile_gently(const char *path, int *return_error_code) error_code =READ_GITFILE_ERR_OPEN_FAILED ; goto cleanup_return; } - buf =xmalloc (st.st_size +1 ); + buf =xmallocz (st.st_size); len =read_in_full (fd, buf, st.st_size);close (fd);if (len != st.st_size) { error_code =READ_GITFILE_ERR_READ_FAILED ; goto cleanup_return; } - buf[len] ='\0' ;if (!starts_with (buf,"gitdir: " )) { error_code =READ_GITFILE_ERR_INVALID_FORMAT ; goto cleanup_return;
strbuf.c +1 -1
@@ -718,7 +718,7 @@ char *xstrdup_tolower(const char *string)size_t len, i; len =strlen (string); - result =xmalloc (len +1 ); + result =xmallocz (len);for (i =0 ; i < len; i++) result[i] =tolower (string[i]); result[i] ='\0' ;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 9/21 convert trivial cases to FLEX_ARRAY macros
In reply to Jeff KingUsing FLEX_ARRAY macros reduces the amount of manual computation size we have to do. It also ensures we don't overflow size_t, and it makes sure we write the same number of bytes that we allocated.
--- attr.c | 4 +--- builtin/blame.c | 4 +--- builtin/help.c | 9 +++------ builtin/mktree.c | 9 +++++---- builtin/reflog.c | 7 ++----- cache-tree.c | 4 +--- combine-diff.c | 4 +--- diff.c | 7 ++----- dir.c | 16 +++------------- hashmap.c | 3 +-- help.c | 6 ++---- log-tree.c | 5 ++--- name-hash.c | 3 +-- ref-filter.c | 6 ++---- refs.c | 6 ++---- refs/files-backend.c | 19 +++++-------------- remote.c | 5 +---- 17 files changed, 35 insertions(+), 82 deletions(-)
attr.c +1 -3
@@ -93,9 +93,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)if (invalid_attr_name (name, len))return NULL ; - a =xmalloc (sizeof (*a) + len +1 ); -memcpy (a->name, name, len); - a->name[len] =0 ; +FLEX_ALLOC_MEM (a, name, name, len); a->h = hval; a->next = git_attr_hash[pos]; a->attr_nr = attr_nr++;
builtin/blame.c +1 -3
@@ -466,13 +466,11 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,static struct origin *make_origin (struct commit *commit,const char *path) {struct origin *o; -size_t pathlen =strlen (path) +1 ; - o =xcalloc (1 ,sizeof (*o) + pathlen); +FLEX_ALLOC_STR (o, path, path); o->commit = commit; o->refcnt =1 ; o->next = commit->util; commit->util = o; -memcpy (o->path, path, pathlen);/* includes NUL */ return o; }
builtin/help.c +3 -6
@@ -171,12 +171,10 @@ static void exec_man_cmd(const char *cmd, const char *page)static void add_man_viewer (const char *name) {struct man_viewer_list **p = &man_viewer_list; -size_t len =strlen (name);while (*p) p = &((*p)->next); - *p =xcalloc (1 , (sizeof (**p) + len +1 )); -memcpy ((*p)->name, name, len);/* NUL-terminated by xcalloc */ +FLEX_ALLOC_STR (*p, name, name); }static int supported_man_viewer (const char *name,size_t len)
@@ -190,9 +188,8 @@ static void do_add_man_viewer_info(const char *name,size_t len, const char *value) { - struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1); - -memcpy (new->name, name, len);/* NUL-terminated by xcalloc */ + struct man_viewer_info_list *new; +FLEX_ALLOC_MEM (new, name, name, len); new->info =xstrdup (value); new->next = man_viewer_info_list; man_viewer_info_list = new;
builtin/mktree.c +5 -4
@@ -19,16 +19,17 @@ static int alloc, used;static void append_to_tree (unsigned mode,unsigned char *sha1,char *path) {struct treeent *ent; -int len =strlen (path); +size_t len =strlen (path);if (strchr (path,'/' ))die ("path %s contains slash" , path); -ALLOC_GROW (entries, used +1 , alloc); - ent = entries[used++] =xmalloc (sizeof (**entries) + len +1 ); +FLEX_ALLOC_MEM (ent, name, path, len); ent->mode = mode; ent->len = len;hashcpy (ent->sha1, sha1); -memcpy (ent->name, path, len+1 ); + +ALLOC_GROW (entries, used +1 , alloc); + entries[used++] = ent; }static int ent_compare (const void *a_,const void *b_)
builtin/reflog.c +2 -5
@@ -382,11 +382,9 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus {struct collected_reflog *e;struct collect_reflog_cb *cb = cb_data; -size_t namelen =strlen (ref); - e =xmalloc (sizeof (*e) + namelen +1 ); +FLEX_ALLOC_STR (e, reflog, ref);hashcpy (e->sha1, oid->hash); -memcpy (e->reflog, ref, namelen +1 );ALLOC_GROW (cb->e, cb->nr +1 , cb->alloc); cb->e[cb->nr++] = e;return 0 ;
@@ -411,8 +409,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) ent->pattern[len] == '\0')return ent; - ent =xcalloc (1 ,sizeof (*ent) + len +1 ); -memcpy (ent->pattern, pattern, len); +FLEX_ALLOC_MEM (ent, pattern, pattern, len); *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);return ent;
cache-tree.c +1 -3
@@ -79,11 +79,9 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,ALLOC_GROW (it->down, it->subtree_nr +1 , it->subtree_alloc); it->subtree_nr++; - down =xmalloc (sizeof (*down) + pathlen +1 ); +FLEX_ALLOC_MEM (down, name, path, pathlen); down->cache_tree =NULL ; down->namelen = pathlen; -memcpy (down->name, path, pathlen); - down->name[pathlen] =0 ;if (pos < it->subtree_nr) memmove(it->down + pos + 1,
combine-diff.c +1 -3
@@ -319,7 +319,7 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)if (line[len-1 ] =='\n' ) len--; - lline =xmalloc (sizeof (*lline) + len +1 ); +FLEX_ALLOC_MEM (lline, line, line, len); lline->len = len; lline->next =NULL ; lline->prev = sline->plost.lost_tail;
@@ -330,8 +330,6 @@ static void append_lost(struct sline *sline, int n, const char *line, int len) sline->plost.lost_tail = lline; sline->plost.len++; lline->parent_map = this_mask; -memcpy (lline->line, line, len); - lline->line[len] =0 ; }struct combine_diff_state {
diff.c +2 -5
@@ -2607,12 +2607,9 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,struct diff_filespec *alloc_filespec (const char *path) { -int namelen =strlen (path); -struct diff_filespec *spec =xmalloc (sizeof (*spec) + namelen +1 ); +struct diff_filespec *spec; -memset (spec,0 ,sizeof (*spec)); - spec->path = (char *)(spec +1 ); -memcpy (spec->path, path, namelen+1 ); +FLEXPTR_ALLOC_STR (spec, path, path); spec->count =1 ; spec->is_binary =-1 ;return spec;
dir.c +3 -13
@@ -503,12 +503,7 @@ void add_exclude(const char *string, const char *base,parse_exclude_pattern (&string, &patternlen, &flags, &nowildcardlen);if (flags &EXC_FLAG_MUSTBEDIR ) { -char *s; - x =xmalloc (sizeof (*x) + patternlen +1 ); - s = (char *)(x+1 ); -memcpy (s, string, patternlen); - s[patternlen] ='\0' ; - x->pattern = s; +FLEXPTR_ALLOC_MEM (x, pattern, string, patternlen); }else { x =xmalloc (sizeof (*x)); x->pattern = string;
@@ -625,10 +620,7 @@ static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, } uc->dir_created++; - d =xmalloc (sizeof (*d) + len +1 ); -memset (d,0 ,sizeof (*d)); -memcpy (d->name, name, len); - d->name[len] ='\0' ; +FLEX_ALLOC_MEM (d, name, name, len);ALLOC_GROW (dir->dirs, dir->dirs_nr +1 , dir->dirs_alloc); memmove(dir->dirs + first + 1, dir->dirs + first,
@@ -1167,10 +1159,8 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len) {struct dir_entry *ent; - ent =xmalloc (sizeof (*ent) + len +1 ); +FLEX_ALLOC_MEM (ent, name, pathname, len); ent->len = len; -memcpy (ent->name, pathname, len); - ent->name[len] =0 ;return ent; }
hashmap.c +1 -2
@@ -256,10 +256,9 @@ const void *memintern(const void *data, size_t len) e =hashmap_get (&map, &key, data);if (!e) {/* not found: create it */ - e =xmallocz (sizeof (struct pool_entry ) + len); +FLEX_ALLOC_MEM (e, data, data, len);hashmap_entry_init (e, key.ent.hash); e->len = len; -memcpy (e->data, data, len);hashmap_add (&map, e); }return e->data;
help.c +2 -4
@@ -11,11 +11,9 @@void add_cmdname (struct cmdnames *cmds,const char *name,int len) { -struct cmdname *ent =xmalloc (sizeof (*ent) + len +1 ); - +struct cmdname *ent; +FLEX_ALLOC_MEM (ent, name, name, len); ent->len = len; -memcpy (ent->name, name, len); - ent->name[len] =0 ;ALLOC_GROW (cmds->names, cmds->cnt +1 , cmds->alloc); cmds->names[cmds->cnt++] = ent;
log-tree.c +2 -3
@@ -77,9 +77,8 @@ int parse_decorate_color_config(const char *var, const char *slot_name, const chvoid add_name_decoration (enum decoration_type type,const char *name,struct object *obj) { -int nlen =strlen (name); -struct name_decoration *res =xmalloc (sizeof (*res) + nlen +1 ); -memcpy (res->name, name, nlen +1 ); +struct name_decoration *res; +FLEX_ALLOC_STR (res, name, name); res->type = type; res->next =add_decoration (&name_decoration, obj, res); }
name-hash.c +1 -2
@@ -55,10 +55,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate, dir =find_dir_entry (istate, ce->name, namelen);if (!dir) {/* not found, create it and add to hash table */ - dir =xcalloc (1 ,sizeof (struct dir_entry ) + namelen +1 ); +FLEX_ALLOC_MEM (dir, name, ce->name, namelen);hashmap_entry_init (dir,memihash (ce->name, namelen)); dir->namelen = namelen; -strncpy (dir->name, ce->name, namelen);hashmap_add (&istate->dir_hash, dir);/* recursively add missing parent directories */
ref-filter.c +2 -4
@@ -1255,10 +1255,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname, const unsigned char *objectname, int flag) { - size_t len = strlen(refname); -struct ref_array_item *ref =xcalloc (1 ,sizeof (struct ref_array_item ) + len +1 ); -memcpy (ref->refname, refname, len); - ref->refname[len] ='\0' ; +struct ref_array_item *ref; +FLEX_ALLOC_STR (ref, refname, refname);hashcpy (ref->objectname, objectname); ref->flag = flag;
refs.c +2 -4
@@ -761,10 +761,8 @@ void ref_transaction_free(struct ref_transaction *transaction)static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) { -size_t len =strlen (refname) +1 ; -struct ref_update *update =xcalloc (1 ,sizeof (*update) + len); - -memcpy ((char *)update->refname, refname, len);/* includes NUL */ +struct ref_update *update; +FLEX_ALLOC_STR (update, refname, refname);ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); transaction->updates[transaction->nr++] = update;return update;
refs/files-backend.c +5 -14
@@ -199,17 +199,14 @@ static struct ref_entry *create_ref_entry(const char *refname, const unsigned char *sha1, int flag, int check_name) { - int len; struct ref_entry *ref;if (check_name &&check_refname_format (refname,REFNAME_ALLOW_ONELEVEL ))die ("Reference has invalid format: '%s'" , refname); - len =strlen (refname) +1 ; - ref =xmalloc (sizeof (struct ref_entry ) + len); +FLEX_ALLOC_STR (ref, name, refname);hashcpy (ref->u.value.oid.hash, sha1);oidclr (&ref->u.value.peeled); -memcpy (ref->name, refname, len); ref->flag = flag;return ref; }
@@ -268,9 +265,7 @@ static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache, int incomplete) {struct ref_entry *direntry; - direntry =xcalloc (1 ,sizeof (struct ref_entry ) + len +1 ); -memcpy (direntry->name, dirname, len); - direntry->name[len] ='\0' ; +FLEX_ALLOC_MEM (direntry, name, dirname, len); direntry->u.subdir.ref_cache = ref_cache; direntry->flag =REF_DIR | (incomplete ?REF_INCOMPLETE :0 );return direntry;
@@ -939,13 +934,10 @@ static void clear_loose_ref_cache(struct ref_cache *refs)*/ static struct ref_cache *create_ref_cache (const char *submodule) { -int len;struct ref_cache *refs;if (!submodule) submodule ="" ; - len =strlen (submodule) +1 ; - refs =xcalloc (1 ,sizeof (struct ref_cache ) + len); -memcpy (refs->name, submodule, len); +FLEX_ALLOC_STR (refs, name, submodule); refs->next = submodule_ref_caches; submodule_ref_caches = refs;return refs;
@@ -2197,10 +2189,9 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)/* Schedule the loose reference for pruning if requested. */ if ((cb->flags &PACK_REFS_PRUNE )) { -int namelen =strlen (entry->name) +1 ; -struct ref_to_prune *n =xcalloc (1 ,sizeof (*n) + namelen); +struct ref_to_prune *n; +FLEX_ALLOC_STR (n, name, entry->name);hashcpy (n->sha1, entry->u.value.oid.hash); -memcpy (n->name, entry->name, namelen);/* includes NUL */ n->next = cb->ref_to_prune; cb->ref_to_prune = n; }
remote.c +1 -4
@@ -2136,16 +2136,13 @@ static int one_local_ref(const char *refname, const struct object_id *oid, {struct ref ***local_tail = cb_data;struct ref *ref; -int len;/* we already know it starts with refs/ to get here */ if (check_refname_format (refname +5 ,0 ))return 0 ; - len =strlen (refname) +1 ; - ref =xcalloc (1 ,sizeof (*ref) + len); + ref =alloc_ref (refname);oidcpy (&ref->new_oid, oid); -memcpy (ref->name, refname, len); **local_tail = ref; *local_tail = &ref->next;return 0 ;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 10/21 use st_add and st_mult for allocation size computation
In reply to Jeff KingIf our size computation overflows size_t, we may allocate a much smaller buffer than we expected and overflow it. It's probably impossible to trigger an overflow in most of these sites in practice, but it is easy enough convert their additions and multiplications into overflow-checking variants. This may be fixing real bugs, and it makes auditing the code easier.
--- archive.c | 4 ++-- builtin/apply.c | 2 +- builtin/clean.c | 2 +- builtin/fetch.c | 2 +- builtin/index-pack.c | 4 ++-- builtin/merge.c | 2 +- builtin/mv.c | 4 ++-- builtin/receive-pack.c | 2 +- combine-diff.c | 14 +++++++------- commit.c | 2 +- compat/mingw.c | 4 ++-- compat/qsort.c | 2 +- compat/setenv.c | 2 +- compat/win32/syslog.c | 4 ++-- diffcore-delta.c | 6 ++++-- diffcore-rename.c | 2 +- dir.c | 4 ++-- fast-import.c | 2 +- refs.c | 2 +- remote.c | 8 ++++---- revision.c | 2 +- sha1_file.c | 20 +++++++++++--------- sha1_name.c | 5 ++--- shallow.c | 2 +- submodule.c | 6 +++--- 25 files changed, 56 insertions(+), 53 deletions(-)
archive.c +2 -2
@@ -171,8 +171,8 @@ static void queue_directory(const unsigned char *sha1, unsigned mode, int stage, struct archiver_context *c) { struct directory *d; -size_t len = base->len +1 +strlen (filename) +1 ; - d =xmalloc (sizeof (*d) + len); +size_t len =st_add4 (base->len,1 ,strlen (filename),1 ); + d =xmalloc (st_add (sizeof (*d), len)); d->up = c->bottom; d->baselen = base->len; d->mode = mode;
builtin/apply.c +1 -1
@@ -2632,7 +2632,7 @@ static void update_image(struct image *img, insert_count = postimage->len;/* Adjust the contents */ - result =xmalloc (img->len + insert_count - remove_count +1 ); + result =xmalloc (st_add3 (st_sub (img->len, remove_count), insert_count,1 ));memcpy (result, img->buf, applied_at);memcpy (result + applied_at, postimage->buf, postimage->len); memcpy(result + applied_at + postimage->len,
builtin/clean.c +1 -1
@@ -615,7 +615,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff) nr += chosen[i]; } - result =xcalloc (nr +1 ,sizeof (int )); + result =xcalloc (st_add (nr,1 ),sizeof (int ));for (i =0 ; i < stuff->nr && j < nr; i++) {if (chosen[i]) result[j++] = i;
builtin/fetch.c +1 -1
@@ -1110,7 +1110,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)if (argc >0 ) {int j =0 ;int i; - refs =xcalloc (argc +1 ,sizeof (const char *)); + refs =xcalloc (st_add (argc,1 ),sizeof (const char *));for (i =0 ; i < argc; i++) {if (!strcmp (argv[i],"tag" )) { i++;
builtin/index-pack.c +2 -2
@@ -1744,9 +1744,9 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix) curr_pack =open_pack_file (pack_name);parse_pack_header (); - objects =xcalloc (nr_objects +1 ,sizeof (struct object_entry )); + objects =xcalloc (st_add (nr_objects,1 ),sizeof (struct object_entry ));if (show_stat) - obj_stat =xcalloc (nr_objects +1 ,sizeof (struct object_stat )); + obj_stat =xcalloc (st_add (nr_objects,1 ),sizeof (struct object_stat )); ofs_deltas =xcalloc (nr_objects,sizeof (struct ofs_delta_entry ));parse_pack_objects (pack_sha1);resolve_deltas ();
builtin/merge.c +1 -1
@@ -939,7 +939,7 @@ static int setup_with_upstream(const char ***argv)if (!branch->merge_nr)die (_ ("No default upstream defined for the current branch." )); - args =xcalloc (branch->merge_nr +1 ,sizeof (char *)); + args =xcalloc (st_add (branch->merge_nr,1 ),sizeof (char *));for (i =0 ; i < branch->merge_nr; i++) {if (!branch->merge[i]->dst) die(_("No remote-tracking branch for %s from %s"),
builtin/mv.c +2 -2
@@ -48,9 +48,9 @@ static const char **internal_copy_pathspec(const char *prefix,static const char *add_slash (const char *path) { -int len =strlen (path); +size_t len =strlen (path);if (path[len -1 ] !='/' ) { -char *with_slash =xmalloc (len +2 ); +char *with_slash =xmalloc (st_add (len,2 ));memcpy (with_slash, path, len); with_slash[len++] ='/' ; with_slash[len] =0 ;
builtin/receive-pack.c +1 -1
@@ -1372,7 +1372,7 @@ static struct command **queue_command(struct command **tail, refname = line +82 ; reflen = linelen -82 ; - cmd =xcalloc (1 ,sizeof (struct command ) + reflen +1 ); + cmd =xcalloc (1 ,st_add3 (sizeof (struct command ), reflen,1 ));hashcpy (cmd->old_sha1, old_sha1);hashcpy (cmd->new_sha1, new_sha1);memcpy (cmd->ref_name, refname, reflen);
combine-diff.c +7 -7
@@ -189,11 +189,11 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,* - Else if we have NEW, insert newend lline into base and * consume newend */ - lcs =xcalloc (origbaselen +1 ,sizeof (int *)); - directions =xcalloc (origbaselen +1 ,sizeof (enum coalesce_direction *)); + lcs =xcalloc (st_add (origbaselen,1 ),sizeof (int *)); + directions =xcalloc (st_add (origbaselen,1 ),sizeof (enum coalesce_direction *));for (i =0 ; i < origbaselen +1 ; i++) { - lcs[i] =xcalloc (lennew +1 ,sizeof (int )); - directions[i] =xcalloc (lennew +1 ,sizeof (enum coalesce_direction )); + lcs[i] =xcalloc (st_add (lennew,1 ),sizeof (int )); + directions[i] =xcalloc (st_add (lennew,1 ),sizeof (enum coalesce_direction )); directions[i][0 ] =BASE ; }for (j =1 ; j < lennew +1 ; j++)
@@ -1111,7 +1111,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,if (result_size && result[result_size-1 ] !='\n' ) cnt++;/* incomplete line */ - sline =xcalloc (cnt+2 ,sizeof (*sline)); + sline =xcalloc (st_add (cnt,2 ),sizeof (*sline)); sline[0 ].bol = result;for (lno =0 , cp = result; cp < result + result_size; cp++) {if (*cp =='\n' ) {
@@ -1130,7 +1130,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,/* Even p_lno[cnt+1] is valid -- that is for the end line number * for deletion hunk at the end. */ - sline[0 ].p_lno =xcalloc ((cnt+2 ) * num_parent,sizeof (unsigned long )); + sline[0 ].p_lno =xcalloc (st_mult (st_add (cnt,2 ), num_parent),sizeof (unsigned long ));for (lno =0 ; lno <= cnt; lno++) sline[lno+1 ].p_lno = sline[lno].p_lno + num_parent;
@@ -1262,7 +1262,7 @@ static struct diff_filepair *combined_pair(struct combine_diff_path *p,struct diff_filespec *pool; pair =xmalloc (sizeof (*pair)); - pool =xcalloc (num_parent +1 ,sizeof (struct diff_filespec )); + pool =xcalloc (st_add (num_parent,1 ),sizeof (struct diff_filespec )); pair->one = pool +1 ; pair->two = pool;
commit.c +1 -1
@@ -147,7 +147,7 @@ struct commit_graft *read_graft_line(char *buf, int len)if ((len +1 ) % entry_size) goto bad_graft_data; i = (len +1 ) / entry_size -1 ; - graft =xmalloc (sizeof (*graft) +GIT_SHA1_RAWSZ * i); + graft =xmalloc (st_add (sizeof (*graft),st_mult (GIT_SHA1_RAWSZ , i))); graft->nr_parent = i;if (get_oid_hex (buf, &graft->oid)) goto bad_graft_data;
compat/mingw.c +2 -2
@@ -810,7 +810,7 @@ static const char *quote_arg(const char *arg)return arg;/* insert \ where necessary */ - d = q =xmalloc (len+n+3 ); + d = q =xmalloc (st_add3 (len, n,3 )); *d++ ='"' ;while (*arg) {if (*arg =='"' )
@@ -1069,7 +1069,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenfree (quoted); } - wargs =xmalloc ((2 * args.len +1 ) *sizeof (wchar_t)); + wargs =xmalloc_array (st_add (st_mult (2 , args.len),1 ),sizeof (wchar_t));xutftowcs (wargs, args.buf,2 * args.len +1 );strbuf_release (&args);
compat/qsort.c +1 -1
@@ -47,7 +47,7 @@ static void msort_with_tmp(void *b, size_t n, size_t s,void git_qsort (void *b,size_t n,size_t s,int (*cmp)(const void *,const void *)) { -const size_t size = n * s; +const size_t size =st_mult (n, s);char buf[1024 ];if (size <sizeof (buf)) {
compat/setenv.c +1 -1
@@ -18,7 +18,7 @@ int gitsetenv(const char *name, const char *value, int replace) namelen =strlen (name); valuelen =strlen (value); - envstr =malloc ((namelen + valuelen +2 )); + envstr =malloc (st_add3 (namelen, valuelen,2 ));if (!envstr) { errno =ENOMEM ;return -1 ;
compat/win32/syslog.c +2 -2
@@ -32,7 +32,7 @@ void syslog(int priority, const char *fmt, ...)return ; } - str =malloc (str_len +1 ); + str =malloc (st_add (str_len,1 ));if (!str) {warning ("malloc failed: '%s'" ,strerror (errno));return ;
@@ -43,7 +43,7 @@ void syslog(int priority, const char *fmt, ...)va_end (ap);while ((pos =strstr (str,"%1" )) !=NULL ) { - str =realloc (str, ++str_len +1 ); + str =realloc (str,st_add (++str_len,1 ));if (!str) {warning ("realloc failed: '%s'" ,strerror (errno));return ;
diffcore-delta.c +4 -2
@@ -53,7 +53,8 @@ static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)int osz =1 << orig->alloc_log2;int sz = osz <<1 ; - new =xmalloc (sizeof (*orig) +sizeof (struct spanhash ) * sz); + new =xmalloc (st_add (sizeof (*orig), +st_mult (sizeof (struct spanhash ), sz))); new->alloc_log2 = orig->alloc_log2 +1 ; new->free =INITIAL_FREE (new->alloc_log2);memset (new->data,0 ,sizeof (struct spanhash ) * sz);
@@ -130,7 +131,8 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)int is_text = !diff_filespec_is_binary (one); i =INITIAL_HASH_SIZE ; - hash =xmalloc (sizeof (*hash) +sizeof (struct spanhash ) * (1 <<i)); + hash =xmalloc (st_add (sizeof (*hash), +st_mult (sizeof (struct spanhash ),1 <<i))); hash->alloc_log2 = i; hash->free =INITIAL_FREE (i);memset (hash->data,0 ,sizeof (struct spanhash ) * (1 <<i));
diffcore-rename.c +1 -1
@@ -537,7 +537,7 @@ void diffcore_rename(struct diff_options *options) rename_dst_nr * rename_src_nr, 50, 1); } - mx =xcalloc (num_create *NUM_CANDIDATE_PER_DST ,sizeof (*mx)); + mx =xcalloc (st_mult (num_create,NUM_CANDIDATE_PER_DST ),sizeof (*mx));for (dst_cnt = i =0 ; i < rename_dst_nr; i++) {struct diff_filespec *two = rename_dst[i].two;struct diff_score *m;
dir.c +2 -2
@@ -689,7 +689,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,return 0 ; }if (buf[size-1 ] !='\n' ) { - buf =xrealloc (buf, size+1 ); + buf =xrealloc (buf,st_add (size,1 )); buf[size++] ='\n' ; } }else {
@@ -2488,7 +2488,7 @@ static int read_one_dir(struct untracked_cache_dir **untracked_, next = data + len +1 ;if (next > rd->end)return -1 ; - *untracked_ = untracked =xmalloc (sizeof (*untracked) + len); + *untracked_ = untracked =xmalloc (st_add (sizeof (*untracked), len));memcpy (untracked, &ud,sizeof (ud));memcpy (untracked->name, data, len +1 ); data = next;
fast-import.c +1 -1
@@ -622,7 +622,7 @@ static void *pool_alloc(size_t len)return xmalloc (len); } total_allocd +=sizeof (struct mem_pool ) + mem_pool_alloc; - p =xmalloc (sizeof (struct mem_pool ) + mem_pool_alloc); + p =xmalloc (st_add (sizeof (struct mem_pool ), mem_pool_alloc)); p->next_pool = mem_pool; p->next_free = (char *) p->space; p->end = p->next_free + mem_pool_alloc;
refs.c +1 -1
@@ -906,7 +906,7 @@ char *shorten_unambiguous_ref(const char *refname, int strict)/* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */ total_len +=strlen (ref_rev_parse_rules[nr_rules]) -2 +1 ; - scanf_fmts =xmalloc (nr_rules *sizeof (char *) + total_len); + scanf_fmts =xmalloc (st_add (st_mult (nr_rules,sizeof (char *)), total_len)); offset =0 ;for (i =0 ; i < nr_rules; i++) {
remote.c +4 -4
@@ -931,7 +931,7 @@ static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, const char *name) {size_t len =strlen (name); -struct ref *ref =xcalloc (1 ,sizeof (struct ref ) + prefixlen + len +1 ); +struct ref *ref =xcalloc (1 ,st_add4 (sizeof (*ref), prefixlen, len,1 ));memcpy (ref->name, prefix, prefixlen);memcpy (ref->name + prefixlen, name, len);return ref;
@@ -948,9 +948,9 @@ struct ref *copy_ref(const struct ref *ref)size_t len;if (!ref)return NULL ; - len =strlen (ref->name); - cpy =xmalloc (sizeof (struct ref ) + len +1 ); -memcpy (cpy, ref,sizeof (struct ref ) + len +1 ); + len =st_add3 (sizeof (struct ref ),strlen (ref->name),1 ); + cpy =xmalloc (len); +memcpy (cpy, ref, len); cpy->next =NULL ; cpy->symref =xstrdup_or_null (ref->symref); cpy->remote_status =xstrdup_or_null (ref->remote_status);
revision.c +1 -1
@@ -540,7 +540,7 @@ struct treesame_state {static struct treesame_state *initialise_treesame (struct rev_info *revs,struct commit *commit) {unsigned n =commit_list_count (commit->parents); -struct treesame_state *st =xcalloc (1 ,sizeof (*st) + n); +struct treesame_state *st =xcalloc (1 ,st_add (sizeof (*st), n)); st->nparents = n;add_decoration (&revs->treesame, &commit->object, st);return st;
sha1_file.c +11 -9
@@ -253,7 +253,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base, {struct alternate_object_database *ent;struct alternate_object_database *alt; -int pfxlen, entlen; +size_t pfxlen, entlen;struct strbuf pathbuf =STRBUF_INIT ;if (!is_absolute_path (entry) && relative_base) {
@@ -273,8 +273,8 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,while (pfxlen && pathbuf.buf[pfxlen-1 ] =='/' ) pfxlen -=1 ; - entlen = pfxlen +43 ;/* '/' + 2 hex + '/' + 38 hex + NUL */ - ent =xmalloc (sizeof (*ent) + entlen); + entlen =st_add (pfxlen,43 );/* '/' + 2 hex + '/' + 38 hex + NUL */ + ent =xmalloc (st_add (sizeof (*ent), entlen));memcpy (ent->base, pathbuf.buf, pfxlen);strbuf_release (&pathbuf);
@@ -1134,7 +1134,7 @@ unsigned char *use_pack(struct packed_git *p,static struct packed_git *alloc_packed_git (int extra) { -struct packed_git *p =xmalloc (sizeof (*p) + extra); +struct packed_git *p =xmalloc (st_add (sizeof (*p), extra));memset (p,0 ,sizeof (*p)); p->pack_fd =-1 ;return p;
@@ -1168,7 +1168,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)* ".pack" is long enough to hold any suffix we're adding (and * the use xsnprintf double-checks that) */ - alloc = path_len +strlen (".pack" ) +1 ; + alloc =st_add3 (path_len,strlen (".pack" ),1 ); p =alloc_packed_git (alloc);memcpy (p->pack_name, path, path_len);
@@ -1196,7 +1196,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)struct packed_git *parse_pack_index (unsigned char *sha1,const char *idx_path) {const char *path =sha1_pack_name (sha1); -int alloc =strlen (path) +1 ; +size_t alloc =st_add (strlen (path),1 );struct packed_git *p =alloc_packed_git (alloc);memcpy (p->pack_name, path, alloc);/* includes NUL */
@@ -1413,10 +1413,12 @@ static void mark_bad_packed_object(struct packed_git *p, {unsigned i;for (i =0 ; i < p->num_bad_objects; i++) -if (!hashcmp (sha1, p->bad_object_sha1 +20 * i)) +if (!hashcmp (sha1, p->bad_object_sha1 +GIT_SHA1_RAWSZ * i))return ; - p->bad_object_sha1 =xrealloc (p->bad_object_sha1,20 * (p->num_bad_objects +1 )); -hashcpy (p->bad_object_sha1 +20 * p->num_bad_objects, sha1); + p->bad_object_sha1 =xrealloc (p->bad_object_sha1, +st_mult (GIT_SHA1_RAWSZ , +st_add (p->num_bad_objects,1 ))); +hashcpy (p->bad_object_sha1 +GIT_SHA1_RAWSZ * p->num_bad_objects, sha1); p->num_bad_objects++; }
sha1_name.c +2 -3
@@ -87,9 +87,8 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa* object databases including our own. */ const char *objdir =get_object_directory (); -int objdir_len =strlen (objdir); -int entlen = objdir_len +43 ; - fakeent =xmalloc (sizeof (*fakeent) + entlen); +size_t objdir_len =strlen (objdir); + fakeent =xmalloc (st_add3 (sizeof (*fakeent), objdir_len,43 ));memcpy (fakeent->base, objdir, objdir_len); fakeent->name = fakeent->base + objdir_len +1 ; fakeent->name[-1 ] ='/' ;
shallow.c +1 -1
@@ -389,7 +389,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,unsigned int i, nr;struct commit_list *head =NULL ;int bitmap_nr = (info->nr_bits +31 ) /32 ; -int bitmap_size = bitmap_nr *sizeof (uint32_t ); +size_t bitmap_size =st_mult (bitmap_nr,sizeof (uint32_t ));uint32_t *tmp =xmalloc (bitmap_size);/* to be freed before return */ uint32_t *bitmap =paint_alloc (info);struct commit *c =lookup_commit_reference_gently (sha1,1 );
submodule.c +3 -3
@@ -123,7 +123,7 @@ static int add_submodule_odb(const char *path)struct strbuf objects_directory =STRBUF_INIT ;struct alternate_object_database *alt_odb;int ret =0 ; -int alloc; +size_t alloc;strbuf_git_path_submodule (&objects_directory, path,"objects/" );if (!is_directory (objects_directory.buf)) {
@@ -138,8 +138,8 @@ static int add_submodule_odb(const char *path) objects_directory.len)) goto done; - alloc = objects_directory.len +42 ;/* for "12/345..." sha1 */ - alt_odb =xmalloc (sizeof (*alt_odb) + alloc); + alloc =st_add (objects_directory.len,42 );/* for "12/345..." sha1 */ + alt_odb =xmalloc (st_add (sizeof (*alt_odb), alloc)); alt_odb->next = alt_odb_list;xsnprintf (alt_odb->base, alloc,"%s" , objects_directory.buf); alt_odb->name = alt_odb->base + objects_directory.len;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 11/21 prepare_{git,shell}_cmd: use argv_array
In reply to Jeff KingThese functions transform an existing argv into one suitable for exec-ing or spawning via git or a shell. We can use an argv_array in each to avoid dealing with manual counting and allocation.
This also makes the memory allocation more clear and fixes some leaks. In prepare_shell_cmd, we would sometimes allocate a new string with "$@" in it and sometimes not, meaning the caller could not correctly free it. On the non-Windows side, we are in a child process which will exec() or exit() immediately, so the leak isn't a big deal. On Windows, though, we use spawn() from the parent process, and leak a string for each shell command we run. On top of that, the Windows code did not free the allocated argv array at all (but does for the prepare_git_cmd case!).
By switching both of these functions to write into an argv_array, we can consistently free the result as appropriate.
---
Note that I had to touch the Windows run-command code here, but I don't actually have a platform to test it on.
exec_cmd.c | 28 +++++++++++----------------- exec_cmd.h | 4 +++- run-command.c | 60 +++++++++++++++++++++++++---------------------------------- 3 files changed, 39 insertions(+), 53 deletions(-)
exec_cmd.c +11 -17
@@ -1,6 +1,7 @@#include "cache.h" #include "exec_cmd.h" #include "quote.h" +#include "argv-array.h" #define MAX_ARGS 32static const char *argv_exec_path;
@@ -107,32 +108,25 @@ void setup_path(void)strbuf_release (&new_path); } -const char **prepare_git_cmd (const char **argv) +const char **prepare_git_cmd (struct argv_array *out,const char **argv) { -int argc; -const char **nargv; - -for (argc =0 ; argv[argc]; argc++) - ;/* just counting */ - nargv =xmalloc (sizeof (*nargv) * (argc +2 )); - - nargv[0 ] ="git" ; -for (argc =0 ; argv[argc]; argc++) - nargv[argc +1 ] = argv[argc]; - nargv[argc +1 ] =NULL ; -return nargv; +argv_array_push (out,"git" ); +argv_array_pushv (out, argv); +return out->argv; }int execv_git_cmd (const char **argv) { -const char **nargv =prepare_git_cmd (argv); -trace_argv_printf (nargv,"trace: exec:" ); +struct argv_array nargv =ARGV_ARRAY_INIT ; + +prepare_git_cmd (&nargv, argv); +trace_argv_printf (nargv.argv,"trace: exec:" );/* execvp() can only ever return if it fails */ -sane_execvp ("git" , (char **)nargv); +sane_execvp ("git" , (char **)nargv.argv);trace_printf ("trace: exec failed: %s\n" ,strerror (errno)); -free (nargv); +argv_array_clear (&nargv);return -1 ; }
exec_cmd.h +3 -1
@@ -1,11 +1,13 @@#ifndef GIT_EXEC_CMD_H #define GIT_EXEC_CMD_H +struct argv_array ; +extern void git_set_argv_exec_path (const char *exec_path);extern const char *git_extract_argv0_path (const char *path);extern const char *git_exec_path (void );extern void setup_path (void ); -extern const char **prepare_git_cmd (const char **argv); +extern const char **prepare_git_cmd (struct argv_array *out,const char **argv);extern int execv_git_cmd (const char **argv);/* NULL terminated */ LAST_ARG_MUST_BE_NULL extern int execl_git_cmd(const char *cmd, ...);
run-command.c +25 -35
99 lines not on this page · raw message
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 12/21 write_untracked_extension: use FLEX_ALLOC helper
In reply to Jeff KingWe perform unchecked additions when computing the size of a "struct ondisk_untracked_cache". This is unlikely to have an integer overflow in practice, but we'd like to avoid this dangerous pattern to make further audits easier.
Note that there's one subtlety here, though. We protect ourselves against a NULL exclude_per_dir entry in our source, and avoid calling strlen() on it, keeping "len" at
0. But later, we unconditionally memcpy "len + 1" bytes to get the trailing NUL byte. If we did have a NULL exclude_per_dir, we would read from bogus memory.
As it turns out, though, we always create this field pointing to a string literal, so there's no bug. We can just get rid of the pointless extra conditional.
--- dir.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
dir.c +4 -5
@@ -2360,16 +2360,15 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untrastruct ondisk_untracked_cache *ouc;struct write_data wd;unsigned char varbuf[16 ]; -int len =0 , varint_len; -if (untracked->exclude_per_dir) - len =strlen (untracked->exclude_per_dir); - ouc =xmalloc (sizeof (*ouc) + len +1 ); +int varint_len; +size_t len =strlen (untracked->exclude_per_dir); + +FLEX_ALLOC_MEM (ouc, exclude_per_dir, untracked->exclude_per_dir, len);stat_data_to_disk (&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);stat_data_to_disk (&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);hashcpy (ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);hashcpy (ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1); ouc->dir_flags =htonl (untracked->dir_flags); -memcpy (ouc->exclude_per_dir, untracked->exclude_per_dir, len +1 ); varint_len =encode_varint (untracked->ident.len, varbuf);strbuf_add (out, varbuf, varint_len);
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 13/21 fast-import: simplify allocation in start_packfile
In reply to Jeff KingThis function allocate a packed_git flex-array, and adds a mysterious 2 bytes to the length of the pack_name field. One is for the trailing NUL, but the other has no purpose. This is probably cargo-culted from add_packed_git, which gets the ".idx" path and needed to allocate enough space to hold the matching ".pack" (though since 48bcc1c, we calculate the size there differently).
This site, however, is using the raw path of a tempfile, and does not need the extra byte. We can just replace the allocation with FLEX_ALLOC_STR, which handles the allocation and the NUL for us.
--- fast-import.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
fast-import.c +1 -4
@@ -865,15 +865,12 @@ static void start_packfile(void) {static char tmp_file[PATH_MAX ];struct packed_git *p; -int namelen;struct pack_header hdr;int pack_fd; pack_fd =odb_mkstemp (tmp_file,sizeof (tmp_file),"pack/tmp_pack_XXXXXX" ); - namelen =strlen (tmp_file) +2 ; - p =xcalloc (1 ,sizeof (*p) + namelen); -xsnprintf (p->pack_name, namelen,"%s" , tmp_file); +FLEX_ALLOC_STR (p, pack_name, tmp_file); p->pack_fd = pack_fd; p->do_not_close =1 ; pack_file =sha1fd (pack_fd, p->pack_name);
Signature · 1 line
2.7.1.577.gfed91b8
Re: [PATCH 13/21] fast-import: simplify allocation in start_packfile
Quoted 1 line · Jeff King
This function allocate a
packed_gitflex-array, and adds a
s/allocate/&s/, right?
Quoted 38 lines
mysterious 2 bytes to the length of the
pack_namefield. One is for the trailing NUL, but the other has no purpose. This is probably cargo-culted fromadd_packed_git, which gets the ".idx" path and needed to allocate enough space to hold the matching ".pack" (though since 48bcc1c, we calculate the size there differently).This site, however, is using the raw path of a tempfile, and does not need the extra byte. We can just replace the allocation with
FLEX_ALLOC_STR, which handles the allocation and the NUL for us.Signed-off-by Jeff King <[email protected]>--- fast-import.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)fast-import.c +1
-4@@ -865,15 +865,12 @@ static void start_packfile(void) {static char tmp_file[PATH_MAX ];struct packed_git *p; -int namelen;struct pack_header hdr;int pack_fd; pack_fd =odb_mkstemp (tmp_file,sizeof (tmp_file),"pack/tmp_pack_XXXXXX" ); - namelen =strlen (tmp_file) +2 ; - p =xcalloc (1 ,sizeof (*p) + namelen); -xsnprintf (p->pack_name, namelen,"%s" , tmp_file); +FLEX_ALLOC_STR (p, pack_name, tmp_file); p->pack_fd = pack_fd; p->do_not_close =1 ; pack_file =sha1fd (pack_fd, p->pack_name);
Re: [PATCH 13/21] fast-import: simplify allocation in start_packfile
Quoted 5 lines · Junio C Hamano
Quoted 1 line · Jeff King
This function allocate a
packed_gitflex-array, and adds as/allocate/&s/, right?
Yes, thanks.
-Peff
PATCH v2 14/21 fetch-pack: simplify add_sought_entry
In reply to Jeff KingWe have two variants of this function, one that takes a string and one that takes a ptr/len combo. But we only call the latter with the length of a NUL-terminated string, so our first simplification is to drop it in favor of the string variant.
Since we know we have a string, we can also replace the manual memory computation with a call to alloc_ref().
Furthermore, we can rely on get_oid_hex() to complain if it hits the end of the string. That means we can simplify the check for "<sha1> <ref>" versus just "<ref>". Rather than manage the ptr/len pair, we can just bump the start of our string forward. The original code over-allocated based on the original "namelen" (which wasn't _wrong_, but was simply wasteful and confusing).
--- builtin/fetch-pack.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
builtin/fetch-pack.c +9 -18
@@ -10,33 +10,24 @@ static const char fetch_pack_usage[] ="[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] " "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]" ; -static void add_sought_entry_mem (struct ref ***sought,int *nr,int *alloc, -const char *name,int namelen) +static void add_sought_entry (struct ref ***sought,int *nr,int *alloc, +const char *name) { -struct ref *ref =xcalloc (1 ,sizeof (*ref) + namelen +1 ); +struct ref *ref;struct object_id oid; -const int chunksz =GIT_SHA1_HEXSZ +1 ; -if (namelen > chunksz && name[chunksz -1 ] ==' ' && - !get_oid_hex (name, &oid)) { -oidcpy (&ref->old_oid, &oid); - name += chunksz; - namelen -= chunksz; - } +if (!get_oid_hex (name, &oid) && name[GIT_SHA1_HEXSZ ] ==' ' ) + name +=GIT_SHA1_HEXSZ +1 ; +else +oidclr (&oid); -memcpy (ref->name, name, namelen); - ref->name[namelen] ='\0' ; + ref =alloc_ref (name); +oidcpy (&ref->old_oid, &oid); (*nr)++;ALLOC_GROW (*sought, *nr, *alloc); (*sought)[*nr -1 ] = ref; } -static void add_sought_entry (struct ref ***sought,int *nr,int *alloc, -const char *string) -{ -add_sought_entry_mem (sought, nr, alloc, string,strlen (string)); -} -int cmd_fetch_pack (int argc,const char **argv,const char *prefix) {int i, ret;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 15/21 test-path-utils: fix normalize_path_copy output buffer size
In reply to Jeff KingThe normalize_path_copy function needs an output buffer that is at least as long as its input (it may shrink the path, but never expand it). However, this test program feeds it static PATH_MAX-sized buffers, which have no relation to the input size.
In the normalize_ceiling_entry case, we do at least check the size against PATH_MAX and die(), but that case is even more convoluted. We normalize into a fixed-size buffer, free the original, and then replace it with a strdup'd copy of the result. But normalize_path_copy explicitly allows normalizing in-place, so we can simply do that.
--- test-path-utils.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
test-path-utils.c +4 -11
@@ -8,21 +8,14 @@*/ static int normalize_ceiling_entry (struct string_list_item *item,void *unused) { -const char *ceil = item->string; -int len =strlen (ceil); -char buf[PATH_MAX +1 ]; +char *ceil = item->string; -if (len ==0 ) +if (!*ceil)die ("Empty path is not supported" ); -if (len >PATH_MAX ) -die ("Path \"%s\" is too long" , ceil);if (!is_absolute_path (ceil))die ("Path \"%s\" is not absolute" , ceil); -if (normalize_path_copy (buf, ceil) <0 ) +if (normalize_path_copy (ceil, ceil) <0 )die ("Path \"%s\" could not be normalized" , ceil); - len =strlen (buf); -free (item->string); - item->string =xstrdup (buf);return 1 ; }
@@ -166,7 +159,7 @@ static struct test_data dirname_data[] = {int main (int argc,char **argv) {if (argc ==3 && !strcmp (argv[1 ],"normalize_path_copy" )) { -char *buf =xmalloc (PATH_MAX +1 ); +char *buf =xmallocz (strlen (argv[2 ]));int rv =normalize_path_copy (buf, argv[2 ]);if (rv) buf ="++failed++" ;
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 16/21 sequencer: simplify memory allocation of get_message
In reply to Jeff KingFor a commit with sha1 "1234abcd" and subject "foo", this function produces a struct with three strings:
1. "foo"
2. "1234abcd... foo"
3. "parent of 1234abcd... foo"
It takes advantage of the fact that these strings are subsets of each other, and allocates only _one_ string, with pointers into the various parts. Unfortunately, this makes the string allocation complicated and hard to follow.
Since we keep only one of these in memory at a time, we can afford to simply allocate three strings. This lets us build on tools like xstrfmt and avoid manual computation.
While we're here, we can also drop the ad-hoc reimplementation of get_git_commit_encoding(), and simply call that function.
--- sequencer.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-)
sequencer.c +10 -19
@@ -124,42 +124,33 @@ static const char *action_name(const struct replay_opts *opts)struct commit_message {char *parent_label; -const char *label; -const char *subject; +char *label; +char *subject;const char *message; };static int get_message (struct commit *commit,struct commit_message *out) {const char *abbrev, *subject; -int abbrev_len, subject_len; -char *q; - -if (!git_commit_encoding) - git_commit_encoding ="UTF-8" ; +int subject_len; - out->message =logmsg_reencode (commit,NULL , git_commit_encoding); + out->message =logmsg_reencode (commit,NULL ,get_commit_output_encoding ()); abbrev =find_unique_abbrev (commit->object.oid.hash,DEFAULT_ABBREV ); - abbrev_len =strlen (abbrev); subject_len =find_commit_subject (out->message, &subject); - out->parent_label =xmalloc (strlen ("parent of " ) + abbrev_len + -strlen ("... " ) + subject_len +1 ); - q = out->parent_label; - q =mempcpy (q,"parent of " ,strlen ("parent of " )); - out->label = q; - q =mempcpy (q, abbrev, abbrev_len); - q =mempcpy (q,"... " ,strlen ("... " )); - out->subject = q; - q =mempcpy (q, subject, subject_len); - *q ='\0' ; + out->subject =xmemdupz (subject, subject_len); + out->label =xstrfmt ("%s... %s" , abbrev, out->subject); + out->parent_label =xstrfmt ("parent of %s" , out->label); +return 0 ; }static void free_message (struct commit *commit,struct commit_message *msg) {free (msg->parent_label); +free (msg->label); +free (msg->subject);unuse_commit_buffer (commit, msg->message); }
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 17/21 git-compat-util: drop mempcpy compat code
In reply to Jeff KingThere are no callers of this left, as the last one was dropped in the previous patch. And there are not likely to be new ones, as the function has been around since 2010 without gaining any new callers.
--- git-compat-util.h | 9 --------- 1 file changed, 9 deletions(-)
git-compat-util.h +0 -9
@@ -681,7 +681,6 @@ extern int git_vsnprintf(char *str, size_t maxsize,#ifdef __GLIBC_PREREQ#if __GLIBC_PREREQ (2 ,1 )#define HAVE_STRCHRNUL -#define HAVE_MEMPCPY #endif #endif
@@ -695,14 +694,6 @@ static inline char *gitstrchrnul(const char *s, int c) }#endif -#ifndef HAVE_MEMPCPY -#define mempcpy gitmempcpy -static inline void *gitmempcpy (void *dest,const void *src,size_t n) -{ -return (char *)memcpy (dest, src, n) + n; -} -#endif -#ifdef NO_INET_PTON int inet_pton (int af,const char *src,void *dst);#endif
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 18/21 transport_anonymize_url: use xstrfmt
In reply to Jeff KingThis function uses xcalloc and two memcpy calls to concatenate two strings. We can do this as an xstrfmt one-liner, and then it is more clear that we are allocating the correct amount of memory.
--- transport.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
transport.c +3 -5
@@ -1021,7 +1021,7 @@ int transport_disconnect(struct transport *transport)*/ char *transport_anonymize_url (const char *url) { -char *anon_url, *scheme_prefix, *anon_part; +char *scheme_prefix, *anon_part;size_t anon_len, prefix_len =0 ; anon_part =strchr (url,'@' );
@@ -1055,10 +1055,8 @@ char *transport_anonymize_url(const char *url) goto literal_copy; prefix_len = scheme_prefix - url +3 ; } - anon_url =xcalloc (1 ,1 + prefix_len + anon_len); -memcpy (anon_url, url, prefix_len); -memcpy (anon_url + prefix_len, anon_part, anon_len); -return anon_url; +return xstrfmt ("%.*s%.*s" , (int )prefix_len, url, + (int )anon_len, anon_part); literal_copy:return xstrdup (url); }
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 19/21 diff_populate_gitlink: use a strbuf
In reply to Jeff KingWe allocate 100 bytes to hold the "Submodule commit ..." text. This is enough, but it's not immediately obvious that this is the case, and we have to repeat the magic 100 twice.
We could get away with xstrfmt here, but we want to know the size, as well, so let's use a real strbuf. And while we're here, we can clean up the logic around size_only. It currently sets and clears the "data" field pointlessly, and leaves the "should_free" flag on even after we have cleared the data.
--- diff.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
diff.c +8 -8
@@ -2704,21 +2704,21 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, intstatic int diff_populate_gitlink (struct diff_filespec *s,int size_only) { -int len; -char *data =xmalloc (100 ), *dirty ="" ; +struct strbuf buf =STRBUF_INIT ; +char *dirty ="" ;/* Are we looking at the work tree? */ if (s->dirty_submodule) dirty ="-dirty" ; - len =snprintf (data,100 , -"Subproject commit %s%s\n" ,sha1_to_hex (s->sha1), dirty); - s->data = data; - s->size = len; - s->should_free =1 ; +strbuf_addf (&buf,"Subproject commit %s%s\n" ,sha1_to_hex (s->sha1), dirty); + s->size = buf.len;if (size_only) { s->data =NULL ; -free (data); +strbuf_release (&buf); + }else { + s->data =strbuf_detach (&buf,NULL ); + s->should_free =1 ; }return 0 ; }
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 20/21 convert ewah/bitmap code to use xmalloc
In reply to Jeff KingThis code was originally written with the idea that it could be spun off into its own ewah library, and uses the overrideable ewah_malloc to do allocations.
We plug in xmalloc as our ewah_malloc, of course. But over the years the ewah code itself has become more entangled with git, and the return value of many ewah_malloc sites is not checked.
Let's just drop the level of indirection and use xmalloc and friends directly. This saves a few lines, and will let us adapt these sites to our more advanced malloc helpers.
--- ewah/bitmap.c | 12 ++++++------ ewah/ewah_bitmap.c | 9 +++------ ewah/ewah_io.c | 10 ++-------- ewah/ewok.h | 10 ---------- 4 files changed, 11 insertions(+), 30 deletions(-)
ewah/bitmap.c +6 -6
@@ -25,8 +25,8 @@struct bitmap *bitmap_new (void ) { -struct bitmap *bitmap =ewah_malloc (sizeof (struct bitmap )); - bitmap->words =ewah_calloc (32 ,sizeof (eword_t)); +struct bitmap *bitmap =xmalloc (sizeof (struct bitmap )); + bitmap->words =xcalloc (32 ,sizeof (eword_t)); bitmap->word_alloc =32 ;return bitmap; }
@@ -38,8 +38,8 @@ void bitmap_set(struct bitmap *self, size_t pos)if (block >= self->word_alloc) {size_t old_size = self->word_alloc; self->word_alloc = block *2 ; - self->words =ewah_realloc (self->words, - self->word_alloc *sizeof (eword_t)); + self->words =xrealloc (self->words, + self->word_alloc *sizeof (eword_t));memset (self->words + old_size,0x0 , (self->word_alloc - old_size) *sizeof (eword_t));
@@ -102,7 +102,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)while (ewah_iterator_next (&blowup, &it)) {if (i >= bitmap->word_alloc) { bitmap->word_alloc *=1.5 ; - bitmap->words =ewah_realloc ( + bitmap->words =xrealloc ( bitmap->words, bitmap->word_alloc *sizeof (eword_t)); }
@@ -134,7 +134,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)if (self->word_alloc < other_final) { self->word_alloc = other_final; - self->words =ewah_realloc (self->words, + self->words =xrealloc (self->words, self->word_alloc *sizeof (eword_t));memset (self->words + original_size,0x0 , (self->word_alloc - original_size) *sizeof (eword_t));
ewah/ewah_bitmap.c +3 -6
@@ -39,7 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)return ; self->alloc_size = new_size; - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); self->rlw = self->buffer + (rlw_offset /sizeof (eword_t)); }
@@ -282,11 +282,8 @@ struct ewah_bitmap *ewah_new(void) {struct ewah_bitmap *self; - self =ewah_malloc (sizeof (struct ewah_bitmap )); -if (self ==NULL ) -return NULL ; - - self->buffer =ewah_malloc (32 *sizeof (eword_t)); + self =xmalloc (sizeof (struct ewah_bitmap )); + self->buffer =xmalloc (32 *sizeof (eword_t)); self->alloc_size =32 ;ewah_clear (self);
ewah/ewah_io.c +2 -8
@@ -134,12 +134,9 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len) self->buffer_size = self->alloc_size =get_be32 (ptr); ptr +=sizeof (uint32_t ); - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); -if (!self->buffer) -return -1 ; -/* * Copy the raw data for the bitmap as a whole chunk; * if we're in a little-endian platform, we'll perform
@@ -180,12 +177,9 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)return -1 ; self->buffer_size = self->alloc_size = (size_t )ntohl (word_count); - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); -if (!self->buffer) -return -1 ; -/** 64 bit x N -- compressed words */ buffer = self->buffer; words_left = self->buffer_size;
ewah/ewok.h +0 -10
@@ -20,16 +20,6 @@#ifndef __EWOK_BITMAP_H__#define __EWOK_BITMAP_H__ -#ifndef ewah_malloc -# define ewah_malloc xmalloc -#endif -#ifndef ewah_realloc -# define ewah_realloc xrealloc -#endif -#ifndef ewah_calloc -# define ewah_calloc xcalloc -#endif -struct strbuf ;typedef uint64_t eword_t ;#define BITS_IN_EWORD (sizeof(eword_t) * 8)
Signature · 1 line
2.7.1.577.gfed91b8
PATCH v2 21/21 ewah: convert to REALLOC_ARRAY, etc
In reply to Jeff KingNow that we're built around xmalloc and friends, we can use helpers like REALLOC_ARRAY, ALLOC_GROW, and so on to make the code shorter and protect against integer overflow.
--- ewah/bitmap.c | 16 ++++------------ ewah/ewah_bitmap.c | 5 ++--- ewah/ewah_io.c | 6 ++---- 3 files changed, 8 insertions(+), 19 deletions(-)
ewah/bitmap.c +4 -12
@@ -17,7 +17,7 @@* along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "git-compat-util.h" +#include "cache.h" #include "ewok.h" #define EWAH_MASK (x) ((eword_t)1 << (x % BITS_IN_EWORD))
@@ -38,9 +38,7 @@ void bitmap_set(struct bitmap *self, size_t pos)if (block >= self->word_alloc) {size_t old_size = self->word_alloc; self->word_alloc = block *2 ; - self->words =xrealloc (self->words, - self->word_alloc *sizeof (eword_t)); - +REALLOC_ARRAY (self->words, self->word_alloc);memset (self->words + old_size,0x0 , (self->word_alloc - old_size) *sizeof (eword_t)); }
@@ -100,12 +98,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)ewah_iterator_init (&it, ewah);while (ewah_iterator_next (&blowup, &it)) { -if (i >= bitmap->word_alloc) { - bitmap->word_alloc *=1.5 ; - bitmap->words =xrealloc ( - bitmap->words, bitmap->word_alloc *sizeof (eword_t)); - } - +ALLOC_GROW (bitmap->words, i +1 , bitmap->word_alloc); bitmap->words[i++] = blowup; }
@@ -134,8 +127,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)if (self->word_alloc < other_final) { self->word_alloc = other_final; - self->words =xrealloc (self->words, - self->word_alloc *sizeof (eword_t)); +REALLOC_ARRAY (self->words, self->word_alloc);memset (self->words + original_size,0x0 , (self->word_alloc - original_size) *sizeof (eword_t)); }
ewah/ewah_bitmap.c +2 -3
@@ -39,8 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)return ; self->alloc_size = new_size; - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size); self->rlw = self->buffer + (rlw_offset /sizeof (eword_t)); }
@@ -283,8 +282,8 @@ struct ewah_bitmap *ewah_new(void)struct ewah_bitmap *self; self =xmalloc (sizeof (struct ewah_bitmap )); - self->buffer =xmalloc (32 *sizeof (eword_t)); self->alloc_size =32 ; +ALLOC_ARRAY (self->buffer, self->alloc_size);ewah_clear (self);return self;
ewah/ewah_io.c +2 -4
@@ -134,8 +134,7 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len) self->buffer_size = self->alloc_size =get_be32 (ptr); ptr +=sizeof (uint32_t ); - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size);/* * Copy the raw data for the bitmap as a whole chunk;
@@ -177,8 +176,7 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)return -1 ; self->buffer_size = self->alloc_size = (size_t )ntohl (word_count); - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size);/** 64 bit x N -- compressed words */ buffer = self->buffer;
Signature · 1 line
2.7.1.577.gfed91b8
Re: [PATCH 04/21] harden REALLOC_ARRAY and xcalloc against size_t overflow
In reply to Jeff KingAm 19.02.2016 um 12:22 schrieb Jeff King:
Quoted 5 lines
REALLOC_ARRAYinherently involves a multiplication which can overflowsize_t, resulting in a much smaller buffer than we think we've allocated. We can easily harden it by usingst_mult()to check for overflow. Likewise, we can addALLOC_ARRAYto do the same thing for xmalloc calls.
Good idea!
Quoted 18 lines
xcalloc()should already be fine, because it takes the two factors separately, assuming the system calloc actually checks for overflow. However, before we even hit the systemcalloc(), we do ourmemory_limit_check, which involves a multiplication. Let's check for overflow ourselves so that this limit cannot be bypassed.Signed-off-by Jeff King <[email protected]>--- git-compat-util.h | 3 ++- wrapper.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-)git-compat-util.h +0
-0
git-compat-util.h extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1); · line 783
Quoted 6 lines
git-compat-util.h +2
-1@@ -779,7 +779,8 @@ extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);extern char *xgetcwd (void );extern FILE *fopen_for_writing (const char *path); -#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) +#define ALLOC_ARRAY (x, alloc) (x) = xmalloc(st_mult((alloc), sizeof(*(x)))) +#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), st_mult((alloc), sizeof(*(x))))
st_mult(x, y) calls unsigned_mult_overflows(x, y), which divides by x. This division can be done at compile time if x is a constant. This can be guaranteed for all users of the two macros above by reversing the arguments of st_mult(), so that sizeof comes first. Probably not a big win, but why not do it if it's that easy?
Or perhaps a macro like this could help here and in other places which use st_mult with sizeof:
#define SIZEOF_MULT(x, n) st_mult(sizeof(x), (n))
(I'd call it ARRAY_SIZE, but that name is already taken. :)
René
Re: [PATCH 04/21] harden REALLOC_ARRAY and xcalloc against size_t overflow
Quoted 9 lines · René Scharfe
Review
Quoted 3 lines
(quoted diff) +2
-1-#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) +#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult((alloc), sizeof(*(x)))) +#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult((alloc), sizeof(*(x))))
st_mult(x, y)callsunsigned_mult_overflows(x, y), which divides by x. This division can be done at compile time if x is a constant. This can be guaranteed for all users of the two macros above by reversing the arguments ofst_mult(), so that sizeof comes first. Probably not a big win, but why not do it if it's that easy?
I doubt it's even measurable, but as you say, it's easy enough to do, so why not.
If we really care about optimizing, I suspect that something like:
git-compat-util.h +4 -2
@@ -662,10 +662,12 @@ static inline size_t st_add(size_t a, size_t b)static inline size_t st_mult (size_t a,size_t b) { -if (unsigned_mult_overflows (a, b)) +size_t ret; + +if (__builtin_mul_overflow (a, b, &ret))die ("size_t overflow: %" PRIuMAX" * %" PRIuMAX, (uintmax_t )a, (uintmax_t )b); -return a * b; +return ret; }static inline size_t st_sub (size_t a,size_t b)
would do a lot more. But it needs #ifdefs for compilers besides gcc and clang.
Quoted 6 lines
Or perhaps a macro like this could help here and in other places which use
st_multwith sizeof:#define SIZEOF_MULT(x, n) st_mult(sizeof(x), (n))(I'd call it
ARRAY_SIZE, but that name is already taken. :)
I don't think we need that; we're really only checking allocations, which means ALLOC_ARRAY() and friends cover all the uses of sizeof (we might still use st_mult() for _another_ part of the computation, but it won't usually be a sizeof then).
We also may do follow-up multiplications, like:
ALLOC_ARRAY(foo, nr); memset(foo, 0, nr * sizeof(*foo));
but I didn't bother doing overflow checks for those. We know that they should be fine if the original allocation was successful (though of course, just using xcalloc here would be better still).
-Peff
Re: [PATCH 06/21] convert manual allocations to argv_array
In reply to Eric SunshineQuoted 3 lines · Eric Sunshine
I also had made the
strbuf_detach()analogy in my response but deleted it before sending; I do think it's a reasonable API template to mirror via newargv_array_detach().
That would look like this, which I think is not too bad (on top of my series for now; I'd do the API function as a separate patch at the beginning and then use it immediately).
argv-array.c +11 -0
@@ -74,3 +74,14 @@ void argv_array_clear(struct argv_array *array) }argv_array_init (array); } + +const char **argv_array_detach (struct argv_array *array) +{ +if (array->argv == empty_argv) +return xcalloc (1 ,sizeof (const char *)); +else { +const char **ret = array->argv; +argv_array_init (array); +return ret; + } +}
argv-array.h +1 -0
@@ -20,5 +20,6 @@ void argv_array_pushl(struct argv_array *, ...);void argv_array_pushv (struct argv_array *,const char **);void argv_array_pop (struct argv_array *);void argv_array_clear (struct argv_array *); +const char **argv_array_detach (struct argv_array *);#endif /* ARGV_ARRAY_H */
line-log.c +8 -6
@@ -748,15 +748,17 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_listif (!rev->diffopt.detect_rename) {struct line_log_data *r; -struct argv_array paths =ARGV_ARRAY_INIT ; +struct argv_array array =ARGV_ARRAY_INIT ; +const char **paths;for (r = range; r; r = r->next) -argv_array_push (&paths, r->path); +argv_array_push (&array, r->path); + paths =argv_array_detach (&array); +parse_pathspec (&rev->diffopt.pathspec,0 , -PATHSPEC_PREFER_FULL ,"" , paths.argv); -/* argv strings are now owned by pathspec */ - paths.argc =0 ; -argv_array_clear (&paths); +PATHSPEC_PREFER_FULL ,"" , paths); +/* strings are now owned by pathspec */ +free (paths); } }
Re: [PATCH 06/21] convert manual allocations to argv_array
Quoted 8 lines · Jeff King
Quoted 3 lines · Eric Sunshine
I also had made the
strbuf_detach()analogy in my response but deleted it before sending; I do think it's a reasonable API template to mirror via newargv_array_detach().That would look like this, which I think is not too bad (on top of my series for now; I'd do the API function as a separate patch at the beginning and then use it immediately).
Looks reasonable.
Quoted 57 lines
argv-array.c +11
-0@@ -74,3 +74,14 @@ void argv_array_clear(struct argv_array *array) }argv_array_init (array); } + +const char **argv_array_detach (struct argv_array *array) +{ +if (array->argv == empty_argv) +return xcalloc (1 ,sizeof (const char *)); +else { +const char **ret = array->argv; +argv_array_init (array); +return ret; + } +}argv-array.h +1
-0@@ -20,5 +20,6 @@ void argv_array_pushl(struct argv_array *, ...);void argv_array_pushv (struct argv_array *,const char **);void argv_array_pop (struct argv_array *);void argv_array_clear (struct argv_array *); +const char **argv_array_detach (struct argv_array *);#endif /* ARGV_ARRAY_H */ line-log.c +8
-6@@ -748,15 +748,17 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_listif (!rev->diffopt.detect_rename) {struct line_log_data *r; -struct argv_array paths =ARGV_ARRAY_INIT ; +struct argv_array array =ARGV_ARRAY_INIT ; +const char **paths;for (r = range; r; r = r->next) -argv_array_push (&paths, r->path); +argv_array_push (&array, r->path); + paths =argv_array_detach (&array); +parse_pathspec (&rev->diffopt.pathspec,0 , -PATHSPEC_PREFER_FULL ,"" , paths.argv); -/* argv strings are now owned by pathspec */ - paths.argc =0 ; -argv_array_clear (&paths); +PATHSPEC_PREFER_FULL ,"" , paths); +/* strings are now owned by pathspec */ +free (paths); } }
v3 cover hardening allocations against integer overflow
In reply to Jeff KingQuoted 3 lines · Jeff King
Here's a re-roll of jk/tighten-alloc series from:
http://thread.gmane.org/gmane.comp.version-control.git/286253
And here's v3. The changes this time (and philosophical rationalization of changes I didn't make) are pretty small:
- flip the order of arguments to st_mult() in ALLOC_ARRAY, et al, to get a probably-irrelevant-but-so-easy-why-not optimization
- mark new global var in daemon.c as static
- add argv_array_detach (new patch 6) to avoid gross memory management when converting line-log to use argv_array in the subsequent patch
So I hope this one is ready for 'next'. Thanks René, Ramsay, and Eric for reviewing.
[01/22]: reflog_expire_cfg: NUL-terminate pattern field [02/22]: add helpers for detecting size_t overflow
[03/22]: tree-diff: catch integer overflow in combine_diff_path allocation [04/22]: harden REALLOC_ARRAY and xcalloc against size_t overflow [05/22]: add helpers for allocating flex-array structs [06/22]: argv-array: add detach function
[07/22]: convert manual allocations to argv_array
[08/22]: convert trivial cases to ALLOC_ARRAY
[09/22]: use xmallocz to avoid size arithmetic
[10/22]: convert trivial cases to FLEX_ARRAY macros
[11/22]: use st_add and st_mult for allocation size computation [12/22]: prepare_{git,shell}_cmd: use argv_array
[13/22]: write_untracked_extension: use FLEX_ALLOC helper [14/22]: fast-import: simplify allocation in start_packfile [15/22]: fetch-pack: simplify add_sought_entry
[16/22]: test-path-utils: fix normalize_path_copy output buffer size [17/22]: sequencer: simplify memory allocation of get_message [18/22]: git-compat-util: drop mempcpy compat code
[19/22]: transport_anonymize_url: use xstrfmt
[20/22]: diff_populate_gitlink: use a strbuf
[21/22]: convert ewah/bitmap code to use xmalloc
[22/22]: ewah: convert to REALLOC_ARRAY, etc
-Peff
PATCH v3 1/22 reflog_expire_cfg: NUL-terminate pattern field
You can tweak the reflog expiration for a particular subset of refs by configuring gc.foo.reflogexpire. We keep a linked list of reflog_expire_cfg structs, each of which holds the pattern and a "len" field for the length of the pattern. The pattern itself is _not_ NUL-terminated.
However, we feed the pattern directly to wildmatch(), which expects a NUL-terminated string, meaning it may keep reading random junk after our struct.
We can fix this by allocating an extra byte for the NUL (which is already zero because we use xcalloc). Let's also drop the misleading "len" field, which is no longer necessary. The existing use of "len" can be converted to use strncmp().
--- builtin/reflog.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
builtin/reflog.c +3 -5
@@ -396,7 +396,6 @@ static struct reflog_expire_cfg {struct reflog_expire_cfg *next;unsigned long expire_total;unsigned long expire_unreachable; -size_t len;char pattern[FLEX_ARRAY ]; } *reflog_expire_cfg, **reflog_expire_cfg_tail;
@@ -408,13 +407,12 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) reflog_expire_cfg_tail = &reflog_expire_cfg;for (ent = reflog_expire_cfg; ent; ent = ent->next) -if (ent->len == len && - !memcmp (ent->pattern, pattern, len)) +if (!strncmp (ent->pattern, pattern, len) && + ent->pattern[len] =='\0' )return ent; - ent =xcalloc (1 , (sizeof (*ent) + len)); + ent =xcalloc (1 ,sizeof (*ent) + len +1 );memcpy (ent->pattern, pattern, len); - ent->len = len; *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);return ent;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 2/22 add helpers for detecting size_t overflow
In reply to Jeff KingPerforming computations on size_t variables that we feed to xmalloc and friends can be dangerous, as an integer overflow can cause us to allocate a much smaller chunk than we realized.
We already have unsigned_add_overflows(), but let's add unsigned_mult_overflows() to that. Furthermore, rather than have each site manually check and die on overflow, we can provide some helpers that will:
- promote the arguments to size_t, so that we know we are doing our computation in the same size of integer that will ultimately be fed to xmalloc
- check and die on overflow
- return the result so that computations can be done in the parameter list of xmalloc.
These functions are a lot uglier to use than normal arithmetic operators (you have to do "st_add(foo, bar)" instead of "foo + bar"). To at least limit the damage, we also provide multi-valued versions. So rather than:
st_add(st_add(a, b), st_add(c, d));
you can write:
st_add4(a, b, c, d);
This isn't nearly as elegant as a varargs function, but it's a lot harder to get it wrong. You don't have to remember to add a sentinel value at the end, and the compiler will complain if you get the number of arguments wrong. This patch adds only the numbered variants required to convert the current code base; we can easily add more later if needed.
--- git-compat-util.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
git-compat-util.h +34 -0
@@ -96,6 +96,14 @@#define unsigned_add_overflows (a, b) \ ((b) > maximum_unsigned_value_of_type(a) - (a)) +/* +* Returns true if the multiplication of "a" and "b" will +* overflow. The types of "a" and "b" must match and must be unsigned. +* Note that this macro evaluates "a" twice! +*/ +#define unsigned_mult_overflows (a, b) \ + ((a) && (b) > maximum_unsigned_value_of_type(a) / (a)) +#ifdef __GNUC__#define TYPEOF (x) (__typeof__(x))#else
@@ -713,6 +721,32 @@ extern void release_pack_memory(size_t);typedef void (*try_to_free_t )(size_t );extern try_to_free_t set_try_to_free_routine (try_to_free_t ); +static inline size_t st_add (size_t a,size_t b) +{ +if (unsigned_add_overflows (a, b)) +die ("size_t overflow: %" PRIuMAX" + %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a + b; +} +#define st_add3 (a,b,c) st_add((a),st_add((b),(c))) +#define st_add4 (a,b,c,d) st_add((a),st_add3((b),(c),(d))) + +static inline size_t st_mult (size_t a,size_t b) +{ +if (unsigned_mult_overflows (a, b)) +die ("size_t overflow: %" PRIuMAX" * %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a * b; +} + +static inline size_t st_sub (size_t a,size_t b) +{ +if (a < b) +die ("size_t underflow: %" PRIuMAX" - %" PRIuMAX, + (uintmax_t )a, (uintmax_t )b); +return a - b; +} +#ifdef HAVE_ALLOCA_H # include <alloca.h> # define xalloca (size) (alloca(size))
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 5/22 add helpers for allocating flex-array structs
In reply to Jeff KingAllocating a struct with a flex array is pretty simple in
practice: you over-allocate the struct, then copy some data into the over-allocation. But it can be a slight pain to make sure you're allocating and copying the right amounts.
This patch adds a few helpers to turn simple cases of flex-array struct allocation into a one-liner that properly checks for overflow. See the embedded documentation for details.
Ideally we could provide a more flexible version that could handle multiple strings, like:
FLEX_ALLOC_FMT(ref, name, "%s%s", prefix, name);
But we have to implement this as a macro (because of the offset calculation of the flex member), which means we would need all compilers to support variadic macros.
--- git-compat-util.h | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+)
git-compat-util.h +62 -0
@@ -782,6 +782,68 @@ extern FILE *fopen_for_writing(const char *path);#define ALLOC_ARRAY (x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) +/* +* These functions help you allocate structs with flex arrays, and copy +* the data directly into the array. For example, if you had: +* +* struct foo { +* int bar; +* char name[FLEX_ARRAY]; +* }; +* +* you can do: +* +* struct foo *f; +* FLEX_ALLOC_MEM(f, name, src, len); +* +* to allocate a "foo" with the contents of "src" in the "name" field. +* The resulting struct is automatically zero'd, and the flex-array field +* is NUL-terminated (whether the incoming src buffer was or not). +* +* The FLEXPTR_* variants operate on structs that don't use flex-arrays, +* but do want to store a pointer to some extra data in the same allocated +* block. For example, if you have: +* +* struct foo { +* char *name; +* int bar; +* }; +* +* you can do: +* +* struct foo *f; +* FLEX_ALLOC_STR(f, name, src); +* +* and "name" will point to a block of memory after the struct, which will be +* freed along with the struct (but the pointer can be repointed anywhere). +* +* The *_STR variants accept a string parameter rather than a ptr/len +* combination. +* +* Note that these macros will evaluate the first parameter multiple +* times, and it must be assignable as an lvalue. +*/ +#define FLEX_ALLOC_MEM (x, flexname, buf, len) do { \ + (x) = NULL;/* silence -Wuninitialized for offset calculation */ \ + (x) =xalloc_flex (sizeof (*(x)), (char *)(&((x)->flexname)) - (char *)(x), (buf), (len)); \ +} while (0) +#define FLEXPTR_ALLOC_MEM (x, ptrname, buf, len) do { \ + (x) = xalloc_flex(sizeof(*(x)), sizeof(*(x)), (buf), (len)); \ + (x)->ptrname = (void *)((x)+1); \ +} while(0) +#define FLEX_ALLOC_STR (x, flexname, str) \ + FLEX_ALLOC_MEM((x), flexname, (str), strlen(str)) +#define FLEXPTR_ALLOC_STR (x, ptrname, str) \ + FLEXPTR_ALLOC_MEM((x), ptrname, (str), strlen(str)) + +static inline void *xalloc_flex (size_t base_len,size_t offset, +const void *src,size_t src_len) +{ +unsigned char *ret =xcalloc (1 ,st_add3 (base_len, src_len,1 )); +memcpy (ret + offset, src, src_len); +return ret; +} +static inline char *xstrdup_or_null (const char *str) {return str ?xstrdup (str) :NULL ;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 3/22 tree-diff: catch integer overflow in combine_diff_path allocation
In reply to Jeff KingA combine_diff_path struct has two "flex" members allocated alongside the struct: a string to hold the pathname, and an array of parent pointers. We use an "int" to compute this, meaning we may easily overflow it if the pathname is extremely long.
We can fix this by using size_t, and checking for overflow with the st_add helper.
--- diff.h | 4 ++-- tree-diff.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
diff.h +2 -2
@@ -222,8 +222,8 @@ struct combine_diff_path { } parent[FLEX_ARRAY ]; };#define combine_diff_path_size (n, l) \ - (sizeof(struct combine_diff_path) + \ - sizeof(struct combine_diff_parent) * (n) + (l) + 1) + st_add4(sizeof(struct combine_diff_path), (l), 1, \ + st_mult(sizeof(struct combine_diff_parent), (n)))extern void show_combined_diff (struct combine_diff_path *elem,int num_parent,int dense,struct rev_info *);
tree-diff.c +2 -2
@@ -124,8 +124,8 @@ static struct combine_diff_path *path_appendnew(struct combine_diff_path *last, unsigned mode, const unsigned char *sha1) { struct combine_diff_path *p; -int len = base->len + pathlen; -int alloclen =combine_diff_path_size (nparent, len); +size_t len =st_add (base->len, pathlen); +size_t alloclen =combine_diff_path_size (nparent, len);/* if last->next is !NULL - it is a pre-allocated memory, we can reuse */ p = last->next;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 4/22 harden REALLOC_ARRAY and xcalloc against size_t overflow
In reply to Jeff KingREALLOC_ARRAY inherently involves a multiplication which can overflow size_t, resulting in a much smaller buffer than we think we've allocated. We can easily harden it by using st_mult() to check for overflow. Likewise, we can add ALLOC_ARRAY to do the same thing for xmalloc calls.
xcalloc() should already be fine, because it takes the two factors separately, assuming the system calloc actually checks for overflow. However, before we even hit the system calloc(), we do our memory_limit_check, which involves a multiplication. Let's check for overflow ourselves so that this limit cannot be bypassed.
--- git-compat-util.h | 3 ++- wrapper.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-)
git-compat-util.h +2 -1
@@ -779,7 +779,8 @@ extern int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1);extern char *xgetcwd (void );extern FILE *fopen_for_writing (const char *path); -#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), (alloc) * sizeof(*(x))) +#define ALLOC_ARRAY (x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc))) +#define REALLOC_ARRAY (x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))static inline char *xstrdup_or_null (const char *str) {
wrapper.c +3 -0
@@ -152,6 +152,9 @@ void *xcalloc(size_t nmemb, size_t size) {void *ret; +if (unsigned_mult_overflows (nmemb, size)) +die ("data too large to fit into virtual memory space" ); +memory_limit_check (size * nmemb,0 ); ret =calloc (nmemb, size);if (!ret && (!nmemb || !size))
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 6/22 argv-array: add detach function
In reply to Jeff KingThe usual pattern for an argv array is to initialize it, push in some strings, and then clear it when done. Very occasionally, though, we must do other exotic things with the memory, like freeing the list but keeping the strings. Let's provide a detach function so that callers can make use of our API to build up the array, and then take ownership of it.
--- Documentation/technical/api-argv-array.txt | 7 +++++++ argv-array.c | 11 +++++++++++ argv-array.h | 1 + 3 files changed, 19 insertions(+)
Documentation/technical/api-argv-array.txt +7 -0
@@ -56,3 +56,10 @@ Functions `argv_array_clear`:: Free all memory associated with the array and return it to the initial, empty state. + +`argv_array_detach`:: + Disconnect the `argv` member from the `argv_array` struct and + return it. The caller is responsible for freeing the memory used + by the array, and by the strings it references. After detaching, + the `argv_array` is in a reinitialized state and can be pushed + into again.
argv-array.c +11 -0
@@ -74,3 +74,14 @@ void argv_array_clear(struct argv_array *array) }argv_array_init (array); } + +const char **argv_array_detach (struct argv_array *array) +{ +if (array->argv == empty_argv) +return xcalloc (1 ,sizeof (const char *)); +else { +const char **ret = array->argv; +argv_array_init (array); +return ret; + } +}
argv-array.h +1 -0
@@ -20,5 +20,6 @@ void argv_array_pushl(struct argv_array *, ...);void argv_array_pushv (struct argv_array *,const char **);void argv_array_pop (struct argv_array *);void argv_array_clear (struct argv_array *); +const char **argv_array_detach (struct argv_array *);#endif /* ARGV_ARRAY_H */
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 7/22 convert manual allocations to argv_array
In reply to Jeff KingThere are many manual argv allocations that predate the argv_array API. Switching to that API brings a few advantages:
1. We no longer have to manually compute the correct final array size (so it's one less thing we can screw up).
2. In many cases we had to make a separate pass to count, then allocate, then fill in the array. Now we can do it in one pass, making the code shorter and easier to follow.
3. argv_array handles memory ownership for us, making it more obvious when things should be free()d and and when not.
Most of these cases are pretty straightforward. In some, we switch from "run_command_v" to "run_command" which lets us directly use the argv_array embedded in "struct child_process".
--- builtin/grep.c | 10 +++++----- builtin/receive-pack.c | 12 +++--------- builtin/remote-ext.c | 26 +++++--------------------- daemon.c | 12 +++++------- git.c | 14 +++++--------- line-log.c | 22 +++++++++------------- remote-curl.c | 23 +++++++++++------------ 7 files changed, 43 insertions(+), 76 deletions(-)
builtin/grep.c +5 -5
@@ -365,17 +365,17 @@ static void append_path(struct grep_opt *opt, const void *data, size_t len)static void run_pager (struct grep_opt *opt,const char *prefix) {struct string_list *path_list = opt->output_priv; -const char **argv =xmalloc (sizeof (const char *) * (path_list->nr +1 )); +struct child_process child =CHILD_PROCESS_INIT ;int i, status;for (i =0 ; i < path_list->nr; i++) - argv[i] = path_list->items[i].string; - argv[path_list->nr] =NULL ; +argv_array_push (&child.args, path_list->items[i].string); + child.dir = prefix; + child.use_shell =1 ; - status =run_command_v_opt_cd_env (argv,RUN_USING_SHELL , prefix,NULL ); + status =run_command (&child);if (status)exit (status); -free (argv); }static int grep_cache (struct grep_opt *opt,const struct pathspec *pathspec,int cached)
builtin/receive-pack.c +3 -9
@@ -1031,7 +1031,6 @@ static void run_update_post_hook(struct command *commands) {struct command *cmd;int argc; -const char **argv;struct child_process proc =CHILD_PROCESS_INIT ;const char *hook;
@@ -1044,21 +1043,16 @@ static void run_update_post_hook(struct command *commands)if (!argc || !hook)return ; - argv =xmalloc (sizeof (*argv) * (2 + argc)); - argv[0 ] = hook; - -for (argc =1 , cmd = commands; cmd; cmd = cmd->next) { +argv_array_push (&proc.args, hook); +for (cmd = commands; cmd; cmd = cmd->next) {if (cmd->error_string || cmd->did_not_exist)continue ; - argv[argc] =xstrdup (cmd->ref_name); - argc++; +argv_array_push (&proc.args, cmd->ref_name); } - argv[argc] =NULL ; proc.no_stdin =1 ; proc.stdout_to_stderr =1 ; proc.err = use_sideband ?-1 :0 ; - proc.argv = argv;if (!start_command (&proc)) {if (use_sideband)
builtin/remote-ext.c +5 -21
@@ -114,30 +114,14 @@ static char *strip_escapes(const char *str, const char *service, } } -/* Should be enough... */ -#define MAXARGUMENTS 256 - -static const char **parse_argv (const char *arg,const char *service) +static void parse_argv (struct argv_array *out,const char *arg,const char *service) { -int arguments =0 ; -int i; -const char **ret; -char *temparray[MAXARGUMENTS +1 ]; -while (*arg) { -char *expanded; -if (arguments ==MAXARGUMENTS ) -die ("remote-ext command has too many arguments" ); - expanded =strip_escapes (arg, service, &arg); +char *expanded =strip_escapes (arg, service, &arg);if (expanded) - temparray[arguments++] = expanded; +argv_array_push (out, expanded); +free (expanded); } - - ret =xmalloc ((arguments +1 ) *sizeof (char *)); -for (i =0 ; i < arguments; i++) - ret[i] = temparray[i]; - ret[arguments] =NULL ; -return ret; } static void send_git_request(int stdin_fd, const char *serv, const char *repo,
@@ -158,7 +142,7 @@ static int run_child(const char *arg, const char *service) child.in =-1 ; child.out =-1 ; child.err =0 ; - child.argv =parse_argv (arg, service); +parse_argv (&child.args, arg, service);if (start_command (&child) <0 )die ("Can't run specified command" );
daemon.c +5 -7
@@ -808,7 +808,7 @@ static void check_dead_children(void) cradle = &blanket->next; } -static char **cld_argv; +static struct argv_array cld_argv =ARGV_ARRAY_INIT ;static void handle (int incoming,struct sockaddr *addr,socklen_t addrlen) {struct child_process cld =CHILD_PROCESS_INIT ;
@@ -842,7 +842,7 @@ static void handle(int incoming, struct sockaddr *addr, socklen_t addrlen)#endif } - cld.argv = (const char **)cld_argv; + cld.argv = cld_argv.argv; cld.in = incoming; cld.out =dup (incoming);
@@ -1374,12 +1374,10 @@ int main(int argc, char **argv)write_file (pid_file,"%" PRIuMAX, (uintmax_t )getpid ());/* prepare argv for serving-processes */ - cld_argv =xmalloc (sizeof (char *) * (argc +2 )); - cld_argv[0 ] = argv[0 ];/* git-daemon */ - cld_argv[1 ] ="--serve" ; +argv_array_push (&cld_argv, argv[0 ]);/* git-daemon */ +argv_array_push (&cld_argv,"--serve" );for (i =1 ; i < argc; ++i) - cld_argv[i+1 ] = argv[i]; - cld_argv[argc+1 ] =NULL ; +argv_array_push (&cld_argv, argv[i]);return serve (&listen_addr, listen_port, cred); }
git.c +5 -9
@@ -247,20 +247,16 @@ static int handle_alias(int *argcp, const char ***argv) alias_string =alias_lookup (alias_command);if (alias_string) {if (alias_string[0 ] =='!' ) { -const char **alias_argv; -int argc = *argcp, i; +struct child_process child =CHILD_PROCESS_INIT ;commit_pager_choice ();restore_env (1 ); -/* build alias_argv */ - alias_argv =xmalloc (sizeof (*alias_argv) * (argc +1 )); - alias_argv[0 ] = alias_string +1 ; -for (i =1 ; i < argc; ++i) - alias_argv[i] = (*argv)[i]; - alias_argv[argc] =NULL ; + child.use_shell =1 ; +argv_array_push (&child.args, alias_string +1 ); +argv_array_pushv (&child.args, (*argv) +1 ); - ret =run_command_v_opt (alias_argv,RUN_USING_SHELL ); + ret =run_command (&child);if (ret >=0 )/* normal exit */ exit (ret);
line-log.c +9 -13
@@ -14,6 +14,7 @@#include "graph.h" #include "userdiff.h" #include "line-log.h" +#include "argv-array.h" static void range_set_grow (struct range_set *rs,size_t extra) {
@@ -746,22 +747,17 @@ void line_log_init(struct rev_info *rev, const char *prefix, struct string_listadd_line_range (rev, commit, range);if (!rev->diffopt.detect_rename) { -int i, count =0 ; -struct line_log_data *r = range; +struct line_log_data *r; +struct argv_array array =ARGV_ARRAY_INIT ;const char **paths; -while (r) { - count++; - r = r->next; - } - paths =xmalloc ((count+1 )*sizeof (char *)); - r = range; -for (i =0 ; i < count; i++) { - paths[i] =xstrdup (r->path); - r = r->next; - } - paths[count] =NULL ; + +for (r = range; r; r = r->next) +argv_array_push (&array, r->path); + paths =argv_array_detach (&array); +parse_pathspec (&rev->diffopt.pathspec,0 ,PATHSPEC_PREFER_FULL ,"" , paths); +/* strings are now owned by pathspec */ free (paths); } }
remote-curl.c +11 -12
@@ -845,23 +845,22 @@ static void parse_fetch(struct strbuf *buf)static int push_dav (int nr_spec,char **specs) { -const char **argv =xmalloc ((10 + nr_spec) *sizeof (char *)); -int argc =0 , i; +struct child_process child =CHILD_PROCESS_INIT ; +size_t i; - argv[argc++] ="http-push" ; - argv[argc++] ="--helper-status" ; + child.git_cmd =1 ; +argv_array_push (&child.args,"http-push" ); +argv_array_push (&child.args,"--helper-status" );if (options.dry_run) - argv[argc++] ="--dry-run" ; +argv_array_push (&child.args,"--dry-run" );if (options.verbosity >1 ) - argv[argc++] ="--verbose" ; - argv[argc++] = url.buf; +argv_array_push (&child.args,"--verbose" ); +argv_array_push (&child.args, url.buf);for (i =0 ; i < nr_spec; i++) - argv[argc++] = specs[i]; - argv[argc++] =NULL ; +argv_array_push (&child.args, specs[i]); -if (run_command_v_opt (argv,RUN_GIT_CMD )) -die ("git-%s failed" , argv[0 ]); -free (argv); +if (run_command (&child)) +die ("git-http-push failed" );return 0 ; }
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 8/22 convert trivial cases to ALLOC_ARRAY
In reply to Jeff KingEach of these cases can be converted to use ALLOC_ARRAY or REALLOC_ARRAY, which has two advantages:
1. It automatically checks the array-size multiplication for overflow.
2. It always uses sizeof(*array) for the element-size, so that it can never go out of sync with the declared type of the array.
--- alias.c | 2 +- attr.c | 2 +- bisect.c | 4 ++-- builtin/blame.c | 3 ++- builtin/clean.c | 2 +- builtin/fast-export.c | 2 +- builtin/index-pack.c | 4 ++-- builtin/merge-base.c | 2 +- builtin/mv.c | 3 ++- builtin/pack-objects.c | 7 ++++--- builtin/pack-redundant.c | 2 +- builtin/receive-pack.c | 5 ++--- column.c | 2 +- combine-diff.c | 4 ++-- commit.c | 2 +- compat/mingw.c | 6 +++--- diffcore-order.c | 4 ++-- dir.c | 6 +++--- fast-import.c | 5 +++-- fsck.c | 3 ++- graph.c | 10 ++++------ khash.h | 2 +- levenshtein.c | 8 +++++--- line-log.c | 8 ++++---- notes.c | 2 +- pack-check.c | 2 +- pack-revindex.c | 12 ++++++++---- pathspec.c | 5 +++-- remote-curl.c | 3 ++- sha1_file.c | 4 ++-- shallow.c | 6 +++--- show-index.c | 3 ++- transport.c | 2 +- xdiff-interface.c | 2 +- 34 files changed, 75 insertions(+), 64 deletions(-)
alias.c +1 -1
@@ -23,7 +23,7 @@ int split_cmdline(char *cmdline, const char ***argv)int src, dst, count =0 , size =16 ;char quoted =0 ; - *argv =xmalloc (sizeof (**argv) * size); +ALLOC_ARRAY (*argv, size);/* split alias_string */ (*argv)[count++] = cmdline;
attr.c +1 -1
@@ -799,7 +799,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check) ++count; } *num = count; - *check =xmalloc (sizeof (**check) * count); +ALLOC_ARRAY (*check, count); j =0 ;for (i =0 ; i < attr_nr; i++) {const char *value = check_all_attr[i].value;
bisect.c +2 -2
@@ -708,10 +708,10 @@ static struct commit *get_commit_reference(const unsigned char *sha1)static struct commit **get_bad_and_good_commits (int *rev_nr) { -int len =1 + good_revs.nr; -struct commit **rev =xmalloc (len *sizeof (*rev)); +struct commit **rev;int i, n =0 ; +ALLOC_ARRAY (rev,1 + good_revs.nr); rev[n++] =get_commit_reference (current_bad_oid->hash);for (i =0 ; i < good_revs.nr; i++) rev[n++] =get_commit_reference (good_revs.sha1[i]);
builtin/blame.c +2 -1
@@ -2059,7 +2059,8 @@ static int prepare_lines(struct scoreboard *sb)for (p = buf; p < end; p =get_next_line (p, end)) num++; - sb->lineno = lineno =xmalloc (sizeof (*sb->lineno) * (num +1 )); +ALLOC_ARRAY (sb->lineno, num +1 ); + lineno = sb->lineno;for (p = buf; p < end; p =get_next_line (p, end)) *lineno++ = p - buf;
builtin/clean.c +1 -1
@@ -543,7 +543,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)int eof =0 ;int i; - chosen =xmalloc (sizeof (int ) * stuff->nr); +ALLOC_ARRAY (chosen, stuff->nr);/* set chosen as uninitialized */ for (i =0 ; i < stuff->nr; i++) chosen[i] =-1 ;
builtin/fast-export.c +1 -1
@@ -1021,7 +1021,7 @@ int cmd_fast_export(int argc, const char **argv, const char *prefix)const char **refspecs_str;int i; - refspecs_str =xmalloc (sizeof (*refspecs_str) * refspecs_list.nr); +ALLOC_ARRAY (refspecs_str, refspecs_list.nr);for (i =0 ; i < refspecs_list.nr; i++) refspecs_str[i] = refspecs_list.items[i].string;
builtin/index-pack.c +2 -2
@@ -1346,7 +1346,7 @@ static void fix_unresolved_deltas(struct sha1file *f)* before deltas depending on them, a good heuristic is to start * resolving deltas in the same order as their position in the pack. */ - sorted_by_pos =xmalloc (nr_ref_deltas *sizeof (*sorted_by_pos)); +ALLOC_ARRAY (sorted_by_pos, nr_ref_deltas);for (i =0 ; i < nr_ref_deltas; i++) sorted_by_pos[i] = &ref_deltas[i];qsort (sorted_by_pos, nr_ref_deltas,sizeof (*sorted_by_pos), delta_pos_compare);
@@ -1759,7 +1759,7 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)if (show_stat)show_pack_info (stat_only); - idx_objects =xmalloc ((nr_objects) *sizeof (struct pack_idx_entry *)); +ALLOC_ARRAY (idx_objects, nr_objects);for (i =0 ; i < nr_objects; i++) idx_objects[i] = &objects[i].idx; curr_index =write_idx_file (index_name, idx_objects, nr_objects, &opts, pack_sha1);
builtin/merge-base.c +1 -1
@@ -252,7 +252,7 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)if (argc <2 )usage_with_options (merge_base_usage, options); - rev =xmalloc (argc *sizeof (*rev)); +ALLOC_ARRAY (rev, argc);while (argc-- >0 ) rev[rev_nr++] =get_commit_reference (*argv++);return show_merge_base (rev, rev_nr, show_all);
builtin/mv.c +2 -1
@@ -24,7 +24,8 @@ static const char **internal_copy_pathspec(const char *prefix, int count, unsigned flags) { int i; -const char **result =xmalloc ((count +1 ) *sizeof (const char *)); +const char **result; +ALLOC_ARRAY (result, count +1 );memcpy (result, pathspec, count *sizeof (const char *)); result[count] =NULL ;for (i =0 ; i < count; i++) {
builtin/pack-objects.c +4 -3
@@ -624,7 +624,7 @@ static struct object_entry **compute_write_order(void) {unsigned int i, wo_end, last_untagged; -struct object_entry **wo =xmalloc (to_pack.nr_objects *sizeof (*wo)); +struct object_entry **wo;struct object_entry *objects = to_pack.objects;for (i =0 ; i < to_pack.nr_objects; i++) {
@@ -657,6 +657,7 @@ static struct object_entry **compute_write_order(void)* Give the objects in the original recency order until * we see a tagged tip. */ +ALLOC_ARRAY (wo, to_pack.nr_objects);for (i = wo_end =0 ; i < to_pack.nr_objects; i++) {if (objects[i].tagged)break ;
@@ -769,7 +770,7 @@ static void write_pack_file(void)if (progress > pack_to_stdout) progress_state =start_progress (_ ("Writing objects" ), nr_result); - written_list =xmalloc (to_pack.nr_objects *sizeof (*written_list)); +ALLOC_ARRAY (written_list, to_pack.nr_objects); write_order =compute_write_order (); do {
@@ -2129,7 +2130,7 @@ static void prepare_pack(int window, int depth)if (!to_pack.nr_objects || !window || !depth)return ; - delta_list =xmalloc (to_pack.nr_objects *sizeof (*delta_list)); +ALLOC_ARRAY (delta_list, to_pack.nr_objects); nr_deltas = n =0 ;for (i =0 ; i < to_pack.nr_objects; i++) {
builtin/pack-redundant.c +1 -1
@@ -53,7 +53,7 @@ static inline struct llist_item *llist_item_get(void) free_nodes = free_nodes->next; }else {int i =1 ; - new =xmalloc (sizeof (struct llist_item ) *BLKSIZE ); +ALLOC_ARRAY (new,BLKSIZE );for (; i <BLKSIZE ; i++)llist_item_put (&new[i]); }
builtin/receive-pack.c +2 -3
@@ -1591,8 +1591,7 @@ static void prepare_shallow_update(struct command *commands, {int i, j, k, bitmap_size = (si->ref->nr +31 ) /32 ; - si->used_shallow =xmalloc (sizeof (*si->used_shallow) * - si->shallow->nr); +ALLOC_ARRAY (si->used_shallow, si->shallow->nr);assign_shallow_commits_to_refs (si, si->used_shallow,NULL ); si->need_reachability_test =
@@ -1658,7 +1657,7 @@ static void update_shallow_info(struct command *commands,return ; } - ref_status =xmalloc (sizeof (*ref_status) * ref->nr); +ALLOC_ARRAY (ref_status, ref->nr);assign_shallow_commits_to_refs (si,NULL , ref_status);for (cmd = commands; cmd; cmd = cmd->next) {if (is_null_sha1 (cmd->new_sha1))
column.c +1 -1
@@ -164,7 +164,7 @@ static void display_table(const struct string_list *list, data.colopts = colopts; data.opts = *opts; - data.len =xmalloc (sizeof (*data.len) * list->nr); +ALLOC_ARRAY (data.len, list->nr);for (i =0 ; i < list->nr; i++) data.len[i] =item_length (colopts, list->items[i].string);
combine-diff.c +2 -2
@@ -1372,7 +1372,7 @@ static struct combine_diff_path *find_paths_multitree(struct combine_diff_path paths_head;struct strbuf base; - parents_sha1 =xmalloc (nparent *sizeof (parents_sha1[0 ])); +ALLOC_ARRAY (parents_sha1, nparent);for (i =0 ; i < nparent; i++) parents_sha1[i] = parents->sha1[i];
@@ -1483,7 +1483,7 @@ void diff_tree_combined(const unsigned char *sha1,if (opt->orderfile && num_paths) {struct obj_order *o; - o =xmalloc (sizeof (*o) * num_paths); +ALLOC_ARRAY (o, num_paths);for (i =0 , p = paths; p; p = p->next, i++) o[i].obj = p;order_objects (opt->orderfile, path_path, o, num_paths);
commit.c +1 -1
@@ -903,7 +903,7 @@ static int remove_redundant(struct commit **array, int cnt) work =xcalloc (cnt,sizeof (*work)); redundant =xcalloc (cnt,1 ); - filled_index =xmalloc (sizeof (*filled_index) * (cnt -1 )); +ALLOC_ARRAY (filled_index, cnt -1 );for (i =0 ; i < cnt; i++)parse_commit (array[i]);
compat/mingw.c +3 -3
@@ -893,7 +893,7 @@ static char **get_path_split(void)if (!n)return NULL ; - path =xmalloc ((n+1 )*sizeof (char *)); +ALLOC_ARRAY (path, n +1 ); p = envpath; i =0 ; do {
@@ -978,7 +978,7 @@ static wchar_t *make_environment_block(char **deltaenv) i++;/* copy the environment, leaving space for changes */ - tmpenv =xmalloc ((size + i) *sizeof (char *)); +ALLOC_ARRAY (tmpenv, size + i);memcpy (tmpenv, environ, size *sizeof (char *));/* merge supplied environment changes into the temporary environment */
@@ -1168,7 +1168,7 @@ static int try_shell_exec(const char *cmd, char *const *argv)int argc =0 ;const char **argv2;while (argv[argc]) argc++; - argv2 =xmalloc (sizeof (*argv) * (argc+1 )); +ALLOC_ARRAY (argv2, argc +1 ); argv2[0 ] = (char *)cmd;/* full path to the script file */ memcpy (&argv2[1 ], &argv[1 ],sizeof (*argv) * argc); pid =mingw_spawnv (prog, argv2,1 );
diffcore-order.c +2 -2
@@ -52,7 +52,7 @@ static void prepare_order(const char *orderfile) }if (pass ==0 ) { order_cnt = cnt; - order =xmalloc (sizeof (*order) * cnt); +ALLOC_ARRAY (order, cnt); } } }
@@ -120,7 +120,7 @@ void diffcore_order(const char *orderfile)if (!q->nr)return ; - o =xmalloc (sizeof (*o) * q->nr); +ALLOC_ARRAY (o, q->nr);for (i =0 ; i < q->nr; i++) o[i].obj = q->queue[i];order_objects (orderfile, pair_pathtwo, o, q->nr);
dir.c +3 -3
@@ -2484,14 +2484,14 @@ static int read_one_dir(struct untracked_cache_dir **untracked_, ud.untracked_alloc = value; ud.untracked_nr = value;if (ud.untracked_nr) - ud.untracked =xmalloc (sizeof (*ud.untracked) * ud.untracked_nr); +ALLOC_ARRAY (ud.untracked, ud.untracked_nr); data = next; next = data; ud.dirs_alloc = ud.dirs_nr =decode_varint (&next);if (next > end)return -1 ; - ud.dirs =xmalloc (sizeof (*ud.dirs) * ud.dirs_nr); +ALLOC_ARRAY (ud.dirs, ud.dirs_nr); data = next; len =strlen ((const char *)data);
@@ -2611,7 +2611,7 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long rd.data = next; rd.end = end; rd.index =0 ; - rd.ucd =xmalloc (sizeof (*rd.ucd) * len); +ALLOC_ARRAY (rd.ucd, len);if (read_one_dir (&uc->root, &rd) || rd.index != len) goto done;
fast-import.c +3 -2
@@ -814,7 +814,8 @@ static struct tree_entry *new_tree_entry(void)if (!avail_tree_entry) {unsigned int n = tree_entry_alloc; total_allocd += n *sizeof (struct tree_entry ); - avail_tree_entry = e =xmalloc (n *sizeof (struct tree_entry )); +ALLOC_ARRAY (e, n); + avail_tree_entry = e;while (n-- >1 ) { *((void **)e) = e +1 ; e++;
@@ -898,7 +899,7 @@ static const char *create_index(void)struct object_entry_pool *o;/* Build the table of object IDs. */ - idx =xmalloc (object_count *sizeof (*idx)); +ALLOC_ARRAY (idx, object_count); c = idx;for (o = blocks; o; o = o->next_pool)for (e = o->next_free; e-- != o->entries;)
fsck.c +2 -1
@@ -199,7 +199,8 @@ void fsck_set_msg_type(struct fsck_options *options,if (!options->msg_type) {int i; -int *msg_type =xmalloc (sizeof (int ) *FSCK_MSG_MAX ); +int *msg_type; +ALLOC_ARRAY (msg_type,FSCK_MSG_MAX );for (i =0 ; i <FSCK_MSG_MAX ; i++) msg_type[i] =fsck_msg_type (i, options); options->msg_type = msg_type;
graph.c +4 -6
@@ -234,12 +234,10 @@ struct git_graph *graph_init(struct rev_info *opt)* We'll automatically grow columns later if we need more room. */ graph->column_capacity =30 ; - graph->columns =xmalloc (sizeof (struct column ) * - graph->column_capacity); - graph->new_columns =xmalloc (sizeof (struct column ) * - graph->column_capacity); - graph->mapping =xmalloc (sizeof (int ) *2 * graph->column_capacity); - graph->new_mapping =xmalloc (sizeof (int ) *2 * graph->column_capacity); +ALLOC_ARRAY (graph->columns, graph->column_capacity); +ALLOC_ARRAY (graph->new_columns, graph->column_capacity); +ALLOC_ARRAY (graph->mapping,2 * graph->column_capacity); +ALLOC_ARRAY (graph->new_mapping,2 * graph->column_capacity);/* * The diff output prefix callback, with this we can make
khash.h +1 -1
@@ -117,7 +117,7 @@ static const double __ac_HASH_UPPER = 0.77;if (new_n_buckets <4 ) new_n_buckets =4 ; \if (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER +0.5 )) j =0 ;/* requested size is too small */ \else {/* hash table size to be changed (shrink or expand); rehash */ \ - new_flags = (khint32_t *)xmalloc (__ac_fsize (new_n_buckets) *sizeof (khint32_t)); \ +ALLOC_ARRAY (new_flags,__ac_fsize (new_n_buckets)); \if (!new_flags)return -1 ; \memset (new_flags,0xaa ,__ac_fsize (new_n_buckets) *sizeof (khint32_t)); \if (h->n_buckets < new_n_buckets) {/* expand */ \
levenshtein.c +5 -3
@@ -42,11 +42,13 @@ int levenshtein(const char *string1, const char *string2, int w, int s, int a, int d) { int len1 = strlen(string1), len2 = strlen(string2); -int *row0 =xmalloc (sizeof (int ) * (len2 +1 )); -int *row1 =xmalloc (sizeof (int ) * (len2 +1 )); -int *row2 =xmalloc (sizeof (int ) * (len2 +1 )); +int *row0, *row1, *row2;int i, j; +ALLOC_ARRAY (row0, len2 +1 ); +ALLOC_ARRAY (row1, len2 +1 ); +ALLOC_ARRAY (row2, len2 +1 ); +for (j =0 ; j <= len2; j++) row1[j] = j * a;for (i =0 ; i < len1; i++) {
line-log.c +4 -4
@@ -522,7 +522,7 @@ static void fill_line_ends(struct diff_filespec *spec, long *lines,if (diff_populate_filespec (spec,0 ))die ("Cannot read blob %s" ,sha1_to_hex (spec->sha1)); - ends =xmalloc (size *sizeof (*ends)); +ALLOC_ARRAY (ends, size); ends[cur++] =0 ; data = spec->data;while (num < spec->size) {
@@ -1142,9 +1142,9 @@ static int process_ranges_merge_commit(struct rev_info *rev, struct commit *commif (nparents >1 && rev->first_parent_only) nparents =1 ; - diffqueues =xmalloc (nparents *sizeof (*diffqueues)); - cand =xmalloc (nparents *sizeof (*cand)); - parents =xmalloc (nparents *sizeof (*parents)); +ALLOC_ARRAY (diffqueues, nparents); +ALLOC_ARRAY (cand, nparents); +ALLOC_ARRAY (parents, nparents); p = commit->parents;for (i =0 ; i < nparents; i++) {
notes.c +1 -1
@@ -1035,7 +1035,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs, int flags)struct string_list_item *item;int counter =0 ;struct notes_tree **trees; - trees =xmalloc ((refs->nr+1 ) *sizeof (struct notes_tree *)); +ALLOC_ARRAY (trees, refs->nr +1 );for_each_string_list_item (item, refs) {struct notes_tree *t =xcalloc (1 ,sizeof (struct notes_tree ));init_notes (t, item->string, combine_notes_ignore, flags);
pack-check.c +1 -1
@@ -89,7 +89,7 @@ static int verify_packfile(struct packed_git *p,* we do not do scan-streaming check on the pack file. */ nr_objects = p->num_objects; - entries =xmalloc ((nr_objects +1 ) *sizeof (*entries)); +ALLOC_ARRAY (entries, nr_objects +1 ); entries[nr_objects].offset = pack_sig_ofs;/* first sort entries by pack offset, since unpacking them is more efficient that way */ for (i =0 ; i < nr_objects; i++) {
pack-revindex.c +8 -4
@@ -44,10 +44,14 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)* keep track of them with alias pointers, always sorting from "from" * to "to". */ -struct revindex_entry *tmp =xmalloc (n *sizeof (*tmp)); -struct revindex_entry *from = entries, *to = tmp; +struct revindex_entry *tmp, *from, *to;int bits; -unsigned *pos =xmalloc (BUCKETS *sizeof (*pos)); +unsigned *pos; + +ALLOC_ARRAY (pos,BUCKETS ); +ALLOC_ARRAY (tmp, n); + from = entries; + to = tmp;/* * If (max >> bits) is zero, then we know that the radix digit we are
@@ -121,7 +125,7 @@ static void create_pack_revindex(struct packed_git *p)unsigned i;const char *index = p->index_data; - p->revindex =xmalloc (sizeof (*p->revindex) * (num_ent +1 )); +ALLOC_ARRAY (p->revindex, num_ent +1 ); index +=4 *256 ;if (p->index_version >1 ) {
pathspec.c +3 -2
@@ -406,7 +406,8 @@ void parse_pathspec(struct pathspec *pathspec, n++; pathspec->nr = n; - pathspec->items = item =xmalloc (sizeof (*item) * n); +ALLOC_ARRAY (pathspec->items, n); + item = pathspec->items; pathspec->_raw = argv; prefixlen = prefix ?strlen (prefix) :0 ;
@@ -483,7 +484,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)void copy_pathspec (struct pathspec *dst,const struct pathspec *src) { *dst = *src; - dst->items =xmalloc (sizeof (struct pathspec_item ) * dst->nr); +ALLOC_ARRAY (dst->items, dst->nr);memcpy (dst->items, src->items,sizeof (struct pathspec_item ) * dst->nr); }
remote-curl.c +2 -1
@@ -696,9 +696,10 @@ static int rpc_service(struct rpc_state *rpc, struct discovery *heads)static int fetch_dumb (int nr_heads,struct ref **to_fetch) {struct walker *walker; -char **targets =xmalloc (nr_heads *sizeof (char *)); +char **targets;int ret, i; +ALLOC_ARRAY (targets, nr_heads);if (options.depth)die ("dumb http transport does not support --depth" );for (i =0 ; i < nr_heads; i++)
sha1_file.c +2 -2
@@ -1942,7 +1942,7 @@ static enum object_type packed_to_object_type(struct packed_git *p,/* Push the object we're going to leave behind */ if (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) { poi_stack_alloc =alloc_nr (poi_stack_nr); - poi_stack =xmalloc (sizeof (off_t)*poi_stack_alloc); +ALLOC_ARRAY (poi_stack, poi_stack_alloc);memcpy (poi_stack, small_poi_stack,sizeof (off_t)*poi_stack_nr); }else {ALLOC_GROW (poi_stack, poi_stack_nr+1 , poi_stack_alloc);
@@ -2308,7 +2308,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,if (delta_stack_nr >= delta_stack_alloc && delta_stack == small_delta_stack) { delta_stack_alloc =alloc_nr (delta_stack_nr); - delta_stack =xmalloc (sizeof (*delta_stack)*delta_stack_alloc); +ALLOC_ARRAY (delta_stack, delta_stack_alloc);memcpy (delta_stack, small_delta_stack,sizeof (*delta_stack)*delta_stack_nr); }else {
shallow.c +3 -3
@@ -315,8 +315,8 @@ void prepare_shallow_info(struct shallow_info *info, struct sha1_array *sa) info->shallow = sa;if (!sa)return ; - info->ours =xmalloc (sizeof (*info->ours) * sa->nr); - info->theirs =xmalloc (sizeof (*info->theirs) * sa->nr); +ALLOC_ARRAY (info->ours, sa->nr); +ALLOC_ARRAY (info->theirs, sa->nr);for (i =0 ; i < sa->nr; i++) {if (has_sha1_file (sa->sha1[i])) {struct commit_graft *graft;
@@ -487,7 +487,7 @@ void assign_shallow_commits_to_refs(struct shallow_info *info,struct paint_info pi;trace_printf_key (&trace_shallow,"shallow: assign_shallow_commits_to_refs\n" ); - shallow =xmalloc (sizeof (*shallow) * (info->nr_ours + info->nr_theirs)); +ALLOC_ARRAY (shallow, info->nr_ours + info->nr_theirs);for (i =0 ; i < info->nr_ours; i++) shallow[nr_shallow++] = info->ours[i];for (i =0 ; i < info->nr_theirs; i++)
show-index.c +2 -1
@@ -50,7 +50,8 @@ int main(int argc, char **argv)unsigned char sha1[20 ];uint32_t crc;uint32_t off; - } *entries =xmalloc (nr *sizeof (entries[0 ])); + } *entries; +ALLOC_ARRAY (entries, nr);for (i =0 ; i < nr; i++)if (fread (entries[i].sha1,20 ,1 , stdin) !=1 )die ("unable to read sha1 %u/%u" , i, nr);
transport.c +1 -1
@@ -977,7 +977,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)* This condition shouldn't be met in a non-deepening fetch * (see builtin/fetch.c:quickfetch()). */ - heads =xmalloc (nr_refs *sizeof (*heads)); +ALLOC_ARRAY (heads, nr_refs);for (rm = refs; rm; rm = rm->next) heads[nr_heads++] = rm; }
xdiff-interface.c +1 -1
@@ -265,7 +265,7 @@ void xdiff_set_find_func(xdemitconf_t *xecfg, const char *value, int cflags)for (i =0 , regs->nr =1 ; value[i]; i++)if (value[i] =='\n' ) regs->nr++; - regs->array =xmalloc (regs->nr *sizeof (struct ff_reg )); +ALLOC_ARRAY (regs->array, regs->nr);for (i =0 ; i < regs->nr; i++) {struct ff_reg *reg = regs->array + i;const char *ep =strchr (value,'\n' ), *expression;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 9/22 use xmallocz to avoid size arithmetic
In reply to Jeff KingWe frequently allocate strings as xmalloc(len + 1), where the extra 1 is for the NUL terminator. This can be done more simply with xmallocz, which also checks for integer overflow.
There's no case where switching xmalloc(n+1) to xmallocz(n) is wrong; the result is the same length, and malloc made no guarantees about what was in the buffer anyway. But in some cases, we can stop manually placing NUL at the end of the allocated buffer. But that's only safe if it's clear that the contents will always fill the buffer.
In each case where this patch does so, I manually examined the control flow, and I tried to err on the side of caution.
--- builtin/check-ref-format.c | 2 +- builtin/merge-tree.c | 2 +- builtin/worktree.c | 2 +- column.c | 3 +-- combine-diff.c | 4 +--- config.c | 4 +--- dir.c | 2 +- entry.c | 2 +- grep.c | 3 +-- imap-send.c | 5 ++--- ll-merge.c | 2 +- progress.c | 2 +- refs.c | 2 +- setup.c | 5 ++--- strbuf.c | 2 +- 15 files changed, 17 insertions(+), 25 deletions(-)
builtin/check-ref-format.c +1 -1
@@ -20,7 +20,7 @@ static const char builtin_check_ref_format_usage[] =*/ static char *collapse_slashes (const char *refname) { -char *ret =xmalloc (strlen (refname) +1 ); +char *ret =xmallocz (strlen (refname));char ch;char prev ='/' ;char *cp = ret;
builtin/merge-tree.c +1 -1
@@ -174,7 +174,7 @@ static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsistatic char *traverse_path (const struct traverse_info *info,const struct name_entry *n) { -char *path =xmalloc (traverse_path_len (info, n) +1 ); +char *path =xmallocz (traverse_path_len (info, n));return make_traverse_path (path, info, n); }
builtin/worktree.c +1 -1
@@ -52,7 +52,7 @@ static int prune_worktree(const char *id, struct strbuf *reason)return 1 ; } len = st.st_size; - path =xmalloc (len +1 ); + path =xmallocz (len);read_in_full (fd, path, len);close (fd);while (len && (path[len -1 ] =='\n' || path[len -1 ] =='\r' ))
column.c +1 -2
@@ -173,9 +173,8 @@ static void display_table(const struct string_list *list,if (colopts &COL_DENSE )shrink_columns (&data); - empty_cell =xmalloc (initial_width +1 ); + empty_cell =xmallocz (initial_width);memset (empty_cell,' ' , initial_width); - empty_cell[initial_width] ='\0' ;for (y =0 ; y < data.rows; y++) {for (x =0 ; x < data.cols; x++)if (display_cell (&data, initial_width, empty_cell, x, y))
combine-diff.c +1 -3
@@ -1043,7 +1043,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, elem->mode =canon_mode (S_IFLNK ); result_size = len; - result =xmalloc (len +1 ); + result =xmallocz (len); done =read_in_full (fd, result, len);if (done <0 )
@@ -1051,8 +1051,6 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,else if (done < len)die ("early EOF '%s'" , elem->path); - result[len] =0 ; -/* If not a fake symlink, apply filters, e.g. autocrlf */ if (is_file) {struct strbuf buf =STRBUF_INIT ;
config.c +1 -3
@@ -1902,7 +1902,7 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *basele* Validate the key and while at it, lower case it for matching. */ if (store_key) - *store_key =xmalloc (strlen (key) +1 ); + *store_key =xmallocz (strlen (key)); dot =0 ;for (i =0 ; key[i]; i++) {
@@ -1926,8 +1926,6 @@ static int git_config_parse_key_1(const char *key, char **store_key, int *baseleif (store_key) (*store_key)[i] = c; } -if (store_key) - (*store_key)[i] =0 ;return 0 ;
dir.c +1 -1
@@ -711,7 +711,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,close (fd);return 0 ; } - buf =xmalloc (size+1 ); + buf =xmallocz (size);if (read_in_full (fd, buf, size) != size) {free (buf);close (fd);
entry.c +1 -1
@@ -6,7 +6,7 @@static void create_directories (const char *path,int path_len,const struct checkout *state) { -char *buf =xmalloc (path_len +1 ); +char *buf =xmallocz (path_len);int len =0 ;while (len < path_len) {
grep.c +1 -2
@@ -1741,7 +1741,7 @@ static int grep_source_load_file(struct grep_source *gs) i =open (filename,O_RDONLY );if (i <0 ) goto err_ret; - data =xmalloc (size +1 ); + data =xmallocz (size);if (st.st_size !=read_in_full (i, data, size)) {error (_ ("'%s': short read %s" ), filename,strerror (errno));close (i);
@@ -1749,7 +1749,6 @@ static int grep_source_load_file(struct grep_source *gs)return -1 ; }close (i); - data[size] =0 ; gs->buf = data; gs->size = size;
imap-send.c +2 -3
@@ -892,12 +892,11 @@ static char *cram(const char *challenge_64, const char *user, const char *pass) response =xstrfmt ("%s %s" , user, hex); resp_len =strlen (response) +1 ; - response_64 =xmalloc (ENCODED_SIZE (resp_len) +1 ); + response_64 =xmallocz (ENCODED_SIZE (resp_len)); encoded_len =EVP_EncodeBlock ((unsigned char *)response_64, (unsigned char *)response, resp_len);if (encoded_len <0 )die ("EVP_EncodeBlock error" ); - response_64[encoded_len] ='\0' ;return (char *)response_64; }
@@ -1188,7 +1187,7 @@ static void lf_to_crlf(struct strbuf *msg) j++; } - new =xmalloc (j +1 ); + new =xmallocz (j);/* * Second pass: write the new string. Note that this loop is
ll-merge.c +1 -1
@@ -205,7 +205,7 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,if (fstat (fd, &st)) goto close_bad; result->size = st.st_size; - result->ptr =xmalloc (result->size +1 ); + result->ptr =xmallocz (result->size);if (read_in_full (fd, result->ptr, result->size) != result->size) {free (result->ptr); result->ptr =NULL ;
progress.c +1 -1
@@ -247,7 +247,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)size_t len =strlen (msg) +5 ;struct throughput *tp = progress->throughput; - bufp = (len <sizeof (buf)) ? buf :xmalloc (len +1 ); + bufp = (len <sizeof (buf)) ? buf :xmallocz (len);if (tp) {unsigned int rate = !tp->avg_misecs ?0 : tp->avg_bytes / tp->avg_misecs;
refs.c +1 -1
@@ -124,7 +124,7 @@ int refname_is_safe(const char *refname)char *buf;int result; - buf =xmalloc (strlen (refname) +1 ); + buf =xmallocz (strlen (refname));/* * Does the refname try to escape refs/? * For example: refs/foo/../bar is safe but refs/foo/../../bar
setup.c +2 -3
@@ -88,7 +88,7 @@ char *prefix_path_gently(const char *prefix, int len,const char *orig = path;char *sanitized;if (is_absolute_path (orig)) { - sanitized =xmalloc (strlen (path) +1 ); + sanitized =xmallocz (strlen (path));if (remaining_prefix) *remaining_prefix =0 ;if (normalize_path_copy_len (sanitized, path, remaining_prefix)) {
@@ -488,14 +488,13 @@ const char *read_gitfile_gently(const char *path, int *return_error_code) error_code =READ_GITFILE_ERR_OPEN_FAILED ; goto cleanup_return; } - buf =xmalloc (st.st_size +1 ); + buf =xmallocz (st.st_size); len =read_in_full (fd, buf, st.st_size);close (fd);if (len != st.st_size) { error_code =READ_GITFILE_ERR_READ_FAILED ; goto cleanup_return; } - buf[len] ='\0' ;if (!starts_with (buf,"gitdir: " )) { error_code =READ_GITFILE_ERR_INVALID_FORMAT ; goto cleanup_return;
strbuf.c +1 -1
@@ -718,7 +718,7 @@ char *xstrdup_tolower(const char *string)size_t len, i; len =strlen (string); - result =xmalloc (len +1 ); + result =xmallocz (len);for (i =0 ; i < len; i++) result[i] =tolower (string[i]); result[i] ='\0' ;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 10/22 convert trivial cases to FLEX_ARRAY macros
In reply to Jeff KingUsing FLEX_ARRAY macros reduces the amount of manual computation size we have to do. It also ensures we don't overflow size_t, and it makes sure we write the same number of bytes that we allocated.
--- attr.c | 4 +--- builtin/blame.c | 4 +--- builtin/help.c | 9 +++------ builtin/mktree.c | 9 +++++---- builtin/reflog.c | 7 ++----- cache-tree.c | 4 +--- combine-diff.c | 4 +--- diff.c | 7 ++----- dir.c | 16 +++------------- hashmap.c | 3 +-- help.c | 6 ++---- log-tree.c | 5 ++--- name-hash.c | 3 +-- ref-filter.c | 6 ++---- refs.c | 6 ++---- refs/files-backend.c | 19 +++++-------------- remote.c | 5 +---- 17 files changed, 35 insertions(+), 82 deletions(-)
attr.c +1 -3
@@ -93,9 +93,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)if (invalid_attr_name (name, len))return NULL ; - a =xmalloc (sizeof (*a) + len +1 ); -memcpy (a->name, name, len); - a->name[len] =0 ; +FLEX_ALLOC_MEM (a, name, name, len); a->h = hval; a->next = git_attr_hash[pos]; a->attr_nr = attr_nr++;
builtin/blame.c +1 -3
@@ -466,13 +466,11 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,static struct origin *make_origin (struct commit *commit,const char *path) {struct origin *o; -size_t pathlen =strlen (path) +1 ; - o =xcalloc (1 ,sizeof (*o) + pathlen); +FLEX_ALLOC_STR (o, path, path); o->commit = commit; o->refcnt =1 ; o->next = commit->util; commit->util = o; -memcpy (o->path, path, pathlen);/* includes NUL */ return o; }
builtin/help.c +3 -6
@@ -171,12 +171,10 @@ static void exec_man_cmd(const char *cmd, const char *page)static void add_man_viewer (const char *name) {struct man_viewer_list **p = &man_viewer_list; -size_t len =strlen (name);while (*p) p = &((*p)->next); - *p =xcalloc (1 , (sizeof (**p) + len +1 )); -memcpy ((*p)->name, name, len);/* NUL-terminated by xcalloc */ +FLEX_ALLOC_STR (*p, name, name); }static int supported_man_viewer (const char *name,size_t len)
@@ -190,9 +188,8 @@ static void do_add_man_viewer_info(const char *name,size_t len, const char *value) { - struct man_viewer_info_list *new = xcalloc(1, sizeof(*new) + len + 1); - -memcpy (new->name, name, len);/* NUL-terminated by xcalloc */ + struct man_viewer_info_list *new; +FLEX_ALLOC_MEM (new, name, name, len); new->info =xstrdup (value); new->next = man_viewer_info_list; man_viewer_info_list = new;
builtin/mktree.c +5 -4
@@ -19,16 +19,17 @@ static int alloc, used;static void append_to_tree (unsigned mode,unsigned char *sha1,char *path) {struct treeent *ent; -int len =strlen (path); +size_t len =strlen (path);if (strchr (path,'/' ))die ("path %s contains slash" , path); -ALLOC_GROW (entries, used +1 , alloc); - ent = entries[used++] =xmalloc (sizeof (**entries) + len +1 ); +FLEX_ALLOC_MEM (ent, name, path, len); ent->mode = mode; ent->len = len;hashcpy (ent->sha1, sha1); -memcpy (ent->name, path, len+1 ); + +ALLOC_GROW (entries, used +1 , alloc); + entries[used++] = ent; }static int ent_compare (const void *a_,const void *b_)
builtin/reflog.c +2 -5
@@ -382,11 +382,9 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus {struct collected_reflog *e;struct collect_reflog_cb *cb = cb_data; -size_t namelen =strlen (ref); - e =xmalloc (sizeof (*e) + namelen +1 ); +FLEX_ALLOC_STR (e, reflog, ref);hashcpy (e->sha1, oid->hash); -memcpy (e->reflog, ref, namelen +1 );ALLOC_GROW (cb->e, cb->nr +1 , cb->alloc); cb->e[cb->nr++] = e;return 0 ;
@@ -411,8 +409,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) ent->pattern[len] == '\0')return ent; - ent =xcalloc (1 ,sizeof (*ent) + len +1 ); -memcpy (ent->pattern, pattern, len); +FLEX_ALLOC_MEM (ent, pattern, pattern, len); *reflog_expire_cfg_tail = ent; reflog_expire_cfg_tail = &(ent->next);return ent;
cache-tree.c +1 -3
@@ -79,11 +79,9 @@ static struct cache_tree_sub *find_subtree(struct cache_tree *it,ALLOC_GROW (it->down, it->subtree_nr +1 , it->subtree_alloc); it->subtree_nr++; - down =xmalloc (sizeof (*down) + pathlen +1 ); +FLEX_ALLOC_MEM (down, name, path, pathlen); down->cache_tree =NULL ; down->namelen = pathlen; -memcpy (down->name, path, pathlen); - down->name[pathlen] =0 ;if (pos < it->subtree_nr) memmove(it->down + pos + 1,
combine-diff.c +1 -3
@@ -319,7 +319,7 @@ static void append_lost(struct sline *sline, int n, const char *line, int len)if (line[len-1 ] =='\n' ) len--; - lline =xmalloc (sizeof (*lline) + len +1 ); +FLEX_ALLOC_MEM (lline, line, line, len); lline->len = len; lline->next =NULL ; lline->prev = sline->plost.lost_tail;
@@ -330,8 +330,6 @@ static void append_lost(struct sline *sline, int n, const char *line, int len) sline->plost.lost_tail = lline; sline->plost.len++; lline->parent_map = this_mask; -memcpy (lline->line, line, len); - lline->line[len] =0 ; }struct combine_diff_state {
diff.c +2 -5
@@ -2607,12 +2607,9 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,struct diff_filespec *alloc_filespec (const char *path) { -int namelen =strlen (path); -struct diff_filespec *spec =xmalloc (sizeof (*spec) + namelen +1 ); +struct diff_filespec *spec; -memset (spec,0 ,sizeof (*spec)); - spec->path = (char *)(spec +1 ); -memcpy (spec->path, path, namelen+1 ); +FLEXPTR_ALLOC_STR (spec, path, path); spec->count =1 ; spec->is_binary =-1 ;return spec;
dir.c +3 -13
@@ -503,12 +503,7 @@ void add_exclude(const char *string, const char *base,parse_exclude_pattern (&string, &patternlen, &flags, &nowildcardlen);if (flags &EXC_FLAG_MUSTBEDIR ) { -char *s; - x =xmalloc (sizeof (*x) + patternlen +1 ); - s = (char *)(x+1 ); -memcpy (s, string, patternlen); - s[patternlen] ='\0' ; - x->pattern = s; +FLEXPTR_ALLOC_MEM (x, pattern, string, patternlen); }else { x =xmalloc (sizeof (*x)); x->pattern = string;
@@ -625,10 +620,7 @@ static struct untracked_cache_dir *lookup_untracked(struct untracked_cache *uc, } uc->dir_created++; - d =xmalloc (sizeof (*d) + len +1 ); -memset (d,0 ,sizeof (*d)); -memcpy (d->name, name, len); - d->name[len] ='\0' ; +FLEX_ALLOC_MEM (d, name, name, len);ALLOC_GROW (dir->dirs, dir->dirs_nr +1 , dir->dirs_alloc); memmove(dir->dirs + first + 1, dir->dirs + first,
@@ -1167,10 +1159,8 @@ static struct dir_entry *dir_entry_new(const char *pathname, int len) {struct dir_entry *ent; - ent =xmalloc (sizeof (*ent) + len +1 ); +FLEX_ALLOC_MEM (ent, name, pathname, len); ent->len = len; -memcpy (ent->name, pathname, len); - ent->name[len] =0 ;return ent; }
hashmap.c +1 -2
@@ -256,10 +256,9 @@ const void *memintern(const void *data, size_t len) e =hashmap_get (&map, &key, data);if (!e) {/* not found: create it */ - e =xmallocz (sizeof (struct pool_entry ) + len); +FLEX_ALLOC_MEM (e, data, data, len);hashmap_entry_init (e, key.ent.hash); e->len = len; -memcpy (e->data, data, len);hashmap_add (&map, e); }return e->data;
help.c +2 -4
@@ -11,11 +11,9 @@void add_cmdname (struct cmdnames *cmds,const char *name,int len) { -struct cmdname *ent =xmalloc (sizeof (*ent) + len +1 ); - +struct cmdname *ent; +FLEX_ALLOC_MEM (ent, name, name, len); ent->len = len; -memcpy (ent->name, name, len); - ent->name[len] =0 ;ALLOC_GROW (cmds->names, cmds->cnt +1 , cmds->alloc); cmds->names[cmds->cnt++] = ent;
log-tree.c +2 -3
@@ -77,9 +77,8 @@ int parse_decorate_color_config(const char *var, const char *slot_name, const chvoid add_name_decoration (enum decoration_type type,const char *name,struct object *obj) { -int nlen =strlen (name); -struct name_decoration *res =xmalloc (sizeof (*res) + nlen +1 ); -memcpy (res->name, name, nlen +1 ); +struct name_decoration *res; +FLEX_ALLOC_STR (res, name, name); res->type = type; res->next =add_decoration (&name_decoration, obj, res); }
name-hash.c +1 -2
@@ -55,10 +55,9 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate, dir =find_dir_entry (istate, ce->name, namelen);if (!dir) {/* not found, create it and add to hash table */ - dir =xcalloc (1 ,sizeof (struct dir_entry ) + namelen +1 ); +FLEX_ALLOC_MEM (dir, name, ce->name, namelen);hashmap_entry_init (dir,memihash (ce->name, namelen)); dir->namelen = namelen; -strncpy (dir->name, ce->name, namelen);hashmap_add (&istate->dir_hash, dir);/* recursively add missing parent directories */
ref-filter.c +2 -4
@@ -1255,10 +1255,8 @@ static struct ref_array_item *new_ref_array_item(const char *refname, const unsigned char *objectname, int flag) { - size_t len = strlen(refname); -struct ref_array_item *ref =xcalloc (1 ,sizeof (struct ref_array_item ) + len +1 ); -memcpy (ref->refname, refname, len); - ref->refname[len] ='\0' ; +struct ref_array_item *ref; +FLEX_ALLOC_STR (ref, refname, refname);hashcpy (ref->objectname, objectname); ref->flag = flag;
refs.c +2 -4
@@ -761,10 +761,8 @@ void ref_transaction_free(struct ref_transaction *transaction)static struct ref_update *add_update (struct ref_transaction *transaction,const char *refname) { -size_t len =strlen (refname) +1 ; -struct ref_update *update =xcalloc (1 ,sizeof (*update) + len); - -memcpy ((char *)update->refname, refname, len);/* includes NUL */ +struct ref_update *update; +FLEX_ALLOC_STR (update, refname, refname);ALLOC_GROW (transaction->updates, transaction->nr +1 , transaction->alloc); transaction->updates[transaction->nr++] = update;return update;
refs/files-backend.c +5 -14
@@ -199,17 +199,14 @@ static struct ref_entry *create_ref_entry(const char *refname, const unsigned char *sha1, int flag, int check_name) { - int len; struct ref_entry *ref;if (check_name &&check_refname_format (refname,REFNAME_ALLOW_ONELEVEL ))die ("Reference has invalid format: '%s'" , refname); - len =strlen (refname) +1 ; - ref =xmalloc (sizeof (struct ref_entry ) + len); +FLEX_ALLOC_STR (ref, name, refname);hashcpy (ref->u.value.oid.hash, sha1);oidclr (&ref->u.value.peeled); -memcpy (ref->name, refname, len); ref->flag = flag;return ref; }
@@ -268,9 +265,7 @@ static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache, int incomplete) {struct ref_entry *direntry; - direntry =xcalloc (1 ,sizeof (struct ref_entry ) + len +1 ); -memcpy (direntry->name, dirname, len); - direntry->name[len] ='\0' ; +FLEX_ALLOC_MEM (direntry, name, dirname, len); direntry->u.subdir.ref_cache = ref_cache; direntry->flag =REF_DIR | (incomplete ?REF_INCOMPLETE :0 );return direntry;
@@ -939,13 +934,10 @@ static void clear_loose_ref_cache(struct ref_cache *refs)*/ static struct ref_cache *create_ref_cache (const char *submodule) { -int len;struct ref_cache *refs;if (!submodule) submodule ="" ; - len =strlen (submodule) +1 ; - refs =xcalloc (1 ,sizeof (struct ref_cache ) + len); -memcpy (refs->name, submodule, len); +FLEX_ALLOC_STR (refs, name, submodule); refs->next = submodule_ref_caches; submodule_ref_caches = refs;return refs;
@@ -2197,10 +2189,9 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)/* Schedule the loose reference for pruning if requested. */ if ((cb->flags &PACK_REFS_PRUNE )) { -int namelen =strlen (entry->name) +1 ; -struct ref_to_prune *n =xcalloc (1 ,sizeof (*n) + namelen); +struct ref_to_prune *n; +FLEX_ALLOC_STR (n, name, entry->name);hashcpy (n->sha1, entry->u.value.oid.hash); -memcpy (n->name, entry->name, namelen);/* includes NUL */ n->next = cb->ref_to_prune; cb->ref_to_prune = n; }
remote.c +1 -4
@@ -2136,16 +2136,13 @@ static int one_local_ref(const char *refname, const struct object_id *oid, {struct ref ***local_tail = cb_data;struct ref *ref; -int len;/* we already know it starts with refs/ to get here */ if (check_refname_format (refname +5 ,0 ))return 0 ; - len =strlen (refname) +1 ; - ref =xcalloc (1 ,sizeof (*ref) + len); + ref =alloc_ref (refname);oidcpy (&ref->new_oid, oid); -memcpy (ref->name, refname, len); **local_tail = ref; *local_tail = &ref->next;return 0 ;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 11/22 use st_add and st_mult for allocation size computation
In reply to Jeff KingIf our size computation overflows size_t, we may allocate a much smaller buffer than we expected and overflow it. It's probably impossible to trigger an overflow in most of these sites in practice, but it is easy enough convert their additions and multiplications into overflow-checking variants. This may be fixing real bugs, and it makes auditing the code easier.
--- archive.c | 4 ++-- builtin/apply.c | 2 +- builtin/clean.c | 2 +- builtin/fetch.c | 2 +- builtin/index-pack.c | 4 ++-- builtin/merge.c | 2 +- builtin/mv.c | 4 ++-- builtin/receive-pack.c | 2 +- combine-diff.c | 14 +++++++------- commit.c | 2 +- compat/mingw.c | 4 ++-- compat/qsort.c | 2 +- compat/setenv.c | 2 +- compat/win32/syslog.c | 4 ++-- diffcore-delta.c | 6 ++++-- diffcore-rename.c | 2 +- dir.c | 4 ++-- fast-import.c | 2 +- refs.c | 2 +- remote.c | 8 ++++---- revision.c | 2 +- sha1_file.c | 20 +++++++++++--------- sha1_name.c | 5 ++--- shallow.c | 2 +- submodule.c | 6 +++--- 25 files changed, 56 insertions(+), 53 deletions(-)
archive.c +2 -2
@@ -171,8 +171,8 @@ static void queue_directory(const unsigned char *sha1, unsigned mode, int stage, struct archiver_context *c) { struct directory *d; -size_t len = base->len +1 +strlen (filename) +1 ; - d =xmalloc (sizeof (*d) + len); +size_t len =st_add4 (base->len,1 ,strlen (filename),1 ); + d =xmalloc (st_add (sizeof (*d), len)); d->up = c->bottom; d->baselen = base->len; d->mode = mode;
builtin/apply.c +1 -1
@@ -2632,7 +2632,7 @@ static void update_image(struct image *img, insert_count = postimage->len;/* Adjust the contents */ - result =xmalloc (img->len + insert_count - remove_count +1 ); + result =xmalloc (st_add3 (st_sub (img->len, remove_count), insert_count,1 ));memcpy (result, img->buf, applied_at);memcpy (result + applied_at, postimage->buf, postimage->len); memcpy(result + applied_at + postimage->len,
builtin/clean.c +1 -1
@@ -615,7 +615,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff) nr += chosen[i]; } - result =xcalloc (nr +1 ,sizeof (int )); + result =xcalloc (st_add (nr,1 ),sizeof (int ));for (i =0 ; i < stuff->nr && j < nr; i++) {if (chosen[i]) result[j++] = i;
builtin/fetch.c +1 -1
@@ -1110,7 +1110,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv)if (argc >0 ) {int j =0 ;int i; - refs =xcalloc (argc +1 ,sizeof (const char *)); + refs =xcalloc (st_add (argc,1 ),sizeof (const char *));for (i =0 ; i < argc; i++) {if (!strcmp (argv[i],"tag" )) { i++;
builtin/index-pack.c +2 -2
@@ -1744,9 +1744,9 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix) curr_pack =open_pack_file (pack_name);parse_pack_header (); - objects =xcalloc (nr_objects +1 ,sizeof (struct object_entry )); + objects =xcalloc (st_add (nr_objects,1 ),sizeof (struct object_entry ));if (show_stat) - obj_stat =xcalloc (nr_objects +1 ,sizeof (struct object_stat )); + obj_stat =xcalloc (st_add (nr_objects,1 ),sizeof (struct object_stat )); ofs_deltas =xcalloc (nr_objects,sizeof (struct ofs_delta_entry ));parse_pack_objects (pack_sha1);resolve_deltas ();
builtin/merge.c +1 -1
@@ -939,7 +939,7 @@ static int setup_with_upstream(const char ***argv)if (!branch->merge_nr)die (_ ("No default upstream defined for the current branch." )); - args =xcalloc (branch->merge_nr +1 ,sizeof (char *)); + args =xcalloc (st_add (branch->merge_nr,1 ),sizeof (char *));for (i =0 ; i < branch->merge_nr; i++) {if (!branch->merge[i]->dst) die(_("No remote-tracking branch for %s from %s"),
builtin/mv.c +2 -2
@@ -48,9 +48,9 @@ static const char **internal_copy_pathspec(const char *prefix,static const char *add_slash (const char *path) { -int len =strlen (path); +size_t len =strlen (path);if (path[len -1 ] !='/' ) { -char *with_slash =xmalloc (len +2 ); +char *with_slash =xmalloc (st_add (len,2 ));memcpy (with_slash, path, len); with_slash[len++] ='/' ; with_slash[len] =0 ;
builtin/receive-pack.c +1 -1
@@ -1372,7 +1372,7 @@ static struct command **queue_command(struct command **tail, refname = line +82 ; reflen = linelen -82 ; - cmd =xcalloc (1 ,sizeof (struct command ) + reflen +1 ); + cmd =xcalloc (1 ,st_add3 (sizeof (struct command ), reflen,1 ));hashcpy (cmd->old_sha1, old_sha1);hashcpy (cmd->new_sha1, new_sha1);memcpy (cmd->ref_name, refname, reflen);
combine-diff.c +7 -7
@@ -189,11 +189,11 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,* - Else if we have NEW, insert newend lline into base and * consume newend */ - lcs =xcalloc (origbaselen +1 ,sizeof (int *)); - directions =xcalloc (origbaselen +1 ,sizeof (enum coalesce_direction *)); + lcs =xcalloc (st_add (origbaselen,1 ),sizeof (int *)); + directions =xcalloc (st_add (origbaselen,1 ),sizeof (enum coalesce_direction *));for (i =0 ; i < origbaselen +1 ; i++) { - lcs[i] =xcalloc (lennew +1 ,sizeof (int )); - directions[i] =xcalloc (lennew +1 ,sizeof (enum coalesce_direction )); + lcs[i] =xcalloc (st_add (lennew,1 ),sizeof (int )); + directions[i] =xcalloc (st_add (lennew,1 ),sizeof (enum coalesce_direction )); directions[i][0 ] =BASE ; }for (j =1 ; j < lennew +1 ; j++)
@@ -1111,7 +1111,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,if (result_size && result[result_size-1 ] !='\n' ) cnt++;/* incomplete line */ - sline =xcalloc (cnt+2 ,sizeof (*sline)); + sline =xcalloc (st_add (cnt,2 ),sizeof (*sline)); sline[0 ].bol = result;for (lno =0 , cp = result; cp < result + result_size; cp++) {if (*cp =='\n' ) {
@@ -1130,7 +1130,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,/* Even p_lno[cnt+1] is valid -- that is for the end line number * for deletion hunk at the end. */ - sline[0 ].p_lno =xcalloc ((cnt+2 ) * num_parent,sizeof (unsigned long )); + sline[0 ].p_lno =xcalloc (st_mult (st_add (cnt,2 ), num_parent),sizeof (unsigned long ));for (lno =0 ; lno <= cnt; lno++) sline[lno+1 ].p_lno = sline[lno].p_lno + num_parent;
@@ -1262,7 +1262,7 @@ static struct diff_filepair *combined_pair(struct combine_diff_path *p,struct diff_filespec *pool; pair =xmalloc (sizeof (*pair)); - pool =xcalloc (num_parent +1 ,sizeof (struct diff_filespec )); + pool =xcalloc (st_add (num_parent,1 ),sizeof (struct diff_filespec )); pair->one = pool +1 ; pair->two = pool;
commit.c +1 -1
@@ -147,7 +147,7 @@ struct commit_graft *read_graft_line(char *buf, int len)if ((len +1 ) % entry_size) goto bad_graft_data; i = (len +1 ) / entry_size -1 ; - graft =xmalloc (sizeof (*graft) +GIT_SHA1_RAWSZ * i); + graft =xmalloc (st_add (sizeof (*graft),st_mult (GIT_SHA1_RAWSZ , i))); graft->nr_parent = i;if (get_oid_hex (buf, &graft->oid)) goto bad_graft_data;
compat/mingw.c +2 -2
@@ -810,7 +810,7 @@ static const char *quote_arg(const char *arg)return arg;/* insert \ where necessary */ - d = q =xmalloc (len+n+3 ); + d = q =xmalloc (st_add3 (len, n,3 )); *d++ ='"' ;while (*arg) {if (*arg =='"' )
@@ -1069,7 +1069,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenfree (quoted); } - wargs =xmalloc ((2 * args.len +1 ) *sizeof (wchar_t)); + wargs =xmalloc_array (st_add (st_mult (2 , args.len),1 ),sizeof (wchar_t));xutftowcs (wargs, args.buf,2 * args.len +1 );strbuf_release (&args);
compat/qsort.c +1 -1
@@ -47,7 +47,7 @@ static void msort_with_tmp(void *b, size_t n, size_t s,void git_qsort (void *b,size_t n,size_t s,int (*cmp)(const void *,const void *)) { -const size_t size = n * s; +const size_t size =st_mult (n, s);char buf[1024 ];if (size <sizeof (buf)) {
compat/setenv.c +1 -1
@@ -18,7 +18,7 @@ int gitsetenv(const char *name, const char *value, int replace) namelen =strlen (name); valuelen =strlen (value); - envstr =malloc ((namelen + valuelen +2 )); + envstr =malloc (st_add3 (namelen, valuelen,2 ));if (!envstr) { errno =ENOMEM ;return -1 ;
compat/win32/syslog.c +2 -2
@@ -32,7 +32,7 @@ void syslog(int priority, const char *fmt, ...)return ; } - str =malloc (str_len +1 ); + str =malloc (st_add (str_len,1 ));if (!str) {warning ("malloc failed: '%s'" ,strerror (errno));return ;
@@ -43,7 +43,7 @@ void syslog(int priority, const char *fmt, ...)va_end (ap);while ((pos =strstr (str,"%1" )) !=NULL ) { - str =realloc (str, ++str_len +1 ); + str =realloc (str,st_add (++str_len,1 ));if (!str) {warning ("realloc failed: '%s'" ,strerror (errno));return ;
diffcore-delta.c +4 -2
@@ -53,7 +53,8 @@ static struct spanhash_top *spanhash_rehash(struct spanhash_top *orig)int osz =1 << orig->alloc_log2;int sz = osz <<1 ; - new =xmalloc (sizeof (*orig) +sizeof (struct spanhash ) * sz); + new =xmalloc (st_add (sizeof (*orig), +st_mult (sizeof (struct spanhash ), sz))); new->alloc_log2 = orig->alloc_log2 +1 ; new->free =INITIAL_FREE (new->alloc_log2);memset (new->data,0 ,sizeof (struct spanhash ) * sz);
@@ -130,7 +131,8 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)int is_text = !diff_filespec_is_binary (one); i =INITIAL_HASH_SIZE ; - hash =xmalloc (sizeof (*hash) +sizeof (struct spanhash ) * (1 <<i)); + hash =xmalloc (st_add (sizeof (*hash), +st_mult (sizeof (struct spanhash ),1 <<i))); hash->alloc_log2 = i; hash->free =INITIAL_FREE (i);memset (hash->data,0 ,sizeof (struct spanhash ) * (1 <<i));
diffcore-rename.c +1 -1
@@ -537,7 +537,7 @@ void diffcore_rename(struct diff_options *options) rename_dst_nr * rename_src_nr, 50, 1); } - mx =xcalloc (num_create *NUM_CANDIDATE_PER_DST ,sizeof (*mx)); + mx =xcalloc (st_mult (num_create,NUM_CANDIDATE_PER_DST ),sizeof (*mx));for (dst_cnt = i =0 ; i < rename_dst_nr; i++) {struct diff_filespec *two = rename_dst[i].two;struct diff_score *m;
dir.c +2 -2
@@ -689,7 +689,7 @@ static int add_excludes(const char *fname, const char *base, int baselen,return 0 ; }if (buf[size-1 ] !='\n' ) { - buf =xrealloc (buf, size+1 ); + buf =xrealloc (buf,st_add (size,1 )); buf[size++] ='\n' ; } }else {
@@ -2488,7 +2488,7 @@ static int read_one_dir(struct untracked_cache_dir **untracked_, next = data + len +1 ;if (next > rd->end)return -1 ; - *untracked_ = untracked =xmalloc (sizeof (*untracked) + len); + *untracked_ = untracked =xmalloc (st_add (sizeof (*untracked), len));memcpy (untracked, &ud,sizeof (ud));memcpy (untracked->name, data, len +1 ); data = next;
fast-import.c +1 -1
@@ -622,7 +622,7 @@ static void *pool_alloc(size_t len)return xmalloc (len); } total_allocd +=sizeof (struct mem_pool ) + mem_pool_alloc; - p =xmalloc (sizeof (struct mem_pool ) + mem_pool_alloc); + p =xmalloc (st_add (sizeof (struct mem_pool ), mem_pool_alloc)); p->next_pool = mem_pool; p->next_free = (char *) p->space; p->end = p->next_free + mem_pool_alloc;
refs.c +1 -1
@@ -906,7 +906,7 @@ char *shorten_unambiguous_ref(const char *refname, int strict)/* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */ total_len +=strlen (ref_rev_parse_rules[nr_rules]) -2 +1 ; - scanf_fmts =xmalloc (nr_rules *sizeof (char *) + total_len); + scanf_fmts =xmalloc (st_add (st_mult (nr_rules,sizeof (char *)), total_len)); offset =0 ;for (i =0 ; i < nr_rules; i++) {
remote.c +4 -4
@@ -931,7 +931,7 @@ static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, const char *name) {size_t len =strlen (name); -struct ref *ref =xcalloc (1 ,sizeof (struct ref ) + prefixlen + len +1 ); +struct ref *ref =xcalloc (1 ,st_add4 (sizeof (*ref), prefixlen, len,1 ));memcpy (ref->name, prefix, prefixlen);memcpy (ref->name + prefixlen, name, len);return ref;
@@ -948,9 +948,9 @@ struct ref *copy_ref(const struct ref *ref)size_t len;if (!ref)return NULL ; - len =strlen (ref->name); - cpy =xmalloc (sizeof (struct ref ) + len +1 ); -memcpy (cpy, ref,sizeof (struct ref ) + len +1 ); + len =st_add3 (sizeof (struct ref ),strlen (ref->name),1 ); + cpy =xmalloc (len); +memcpy (cpy, ref, len); cpy->next =NULL ; cpy->symref =xstrdup_or_null (ref->symref); cpy->remote_status =xstrdup_or_null (ref->remote_status);
revision.c +1 -1
@@ -540,7 +540,7 @@ struct treesame_state {static struct treesame_state *initialise_treesame (struct rev_info *revs,struct commit *commit) {unsigned n =commit_list_count (commit->parents); -struct treesame_state *st =xcalloc (1 ,sizeof (*st) + n); +struct treesame_state *st =xcalloc (1 ,st_add (sizeof (*st), n)); st->nparents = n;add_decoration (&revs->treesame, &commit->object, st);return st;
sha1_file.c +11 -9
@@ -253,7 +253,7 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base, {struct alternate_object_database *ent;struct alternate_object_database *alt; -int pfxlen, entlen; +size_t pfxlen, entlen;struct strbuf pathbuf =STRBUF_INIT ;if (!is_absolute_path (entry) && relative_base) {
@@ -273,8 +273,8 @@ static int link_alt_odb_entry(const char *entry, const char *relative_base,while (pfxlen && pathbuf.buf[pfxlen-1 ] =='/' ) pfxlen -=1 ; - entlen = pfxlen +43 ;/* '/' + 2 hex + '/' + 38 hex + NUL */ - ent =xmalloc (sizeof (*ent) + entlen); + entlen =st_add (pfxlen,43 );/* '/' + 2 hex + '/' + 38 hex + NUL */ + ent =xmalloc (st_add (sizeof (*ent), entlen));memcpy (ent->base, pathbuf.buf, pfxlen);strbuf_release (&pathbuf);
@@ -1134,7 +1134,7 @@ unsigned char *use_pack(struct packed_git *p,static struct packed_git *alloc_packed_git (int extra) { -struct packed_git *p =xmalloc (sizeof (*p) + extra); +struct packed_git *p =xmalloc (st_add (sizeof (*p), extra));memset (p,0 ,sizeof (*p)); p->pack_fd =-1 ;return p;
@@ -1168,7 +1168,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)* ".pack" is long enough to hold any suffix we're adding (and * the use xsnprintf double-checks that) */ - alloc = path_len +strlen (".pack" ) +1 ; + alloc =st_add3 (path_len,strlen (".pack" ),1 ); p =alloc_packed_git (alloc);memcpy (p->pack_name, path, path_len);
@@ -1196,7 +1196,7 @@ struct packed_git *add_packed_git(const char *path, size_t path_len, int local)struct packed_git *parse_pack_index (unsigned char *sha1,const char *idx_path) {const char *path =sha1_pack_name (sha1); -int alloc =strlen (path) +1 ; +size_t alloc =st_add (strlen (path),1 );struct packed_git *p =alloc_packed_git (alloc);memcpy (p->pack_name, path, alloc);/* includes NUL */
@@ -1413,10 +1413,12 @@ static void mark_bad_packed_object(struct packed_git *p, {unsigned i;for (i =0 ; i < p->num_bad_objects; i++) -if (!hashcmp (sha1, p->bad_object_sha1 +20 * i)) +if (!hashcmp (sha1, p->bad_object_sha1 +GIT_SHA1_RAWSZ * i))return ; - p->bad_object_sha1 =xrealloc (p->bad_object_sha1,20 * (p->num_bad_objects +1 )); -hashcpy (p->bad_object_sha1 +20 * p->num_bad_objects, sha1); + p->bad_object_sha1 =xrealloc (p->bad_object_sha1, +st_mult (GIT_SHA1_RAWSZ , +st_add (p->num_bad_objects,1 ))); +hashcpy (p->bad_object_sha1 +GIT_SHA1_RAWSZ * p->num_bad_objects, sha1); p->num_bad_objects++; }
sha1_name.c +2 -3
@@ -87,9 +87,8 @@ static void find_short_object_filename(int len, const char *hex_pfx, struct disa* object databases including our own. */ const char *objdir =get_object_directory (); -int objdir_len =strlen (objdir); -int entlen = objdir_len +43 ; - fakeent =xmalloc (sizeof (*fakeent) + entlen); +size_t objdir_len =strlen (objdir); + fakeent =xmalloc (st_add3 (sizeof (*fakeent), objdir_len,43 ));memcpy (fakeent->base, objdir, objdir_len); fakeent->name = fakeent->base + objdir_len +1 ; fakeent->name[-1 ] ='/' ;
shallow.c +1 -1
@@ -389,7 +389,7 @@ static void paint_down(struct paint_info *info, const unsigned char *sha1,unsigned int i, nr;struct commit_list *head =NULL ;int bitmap_nr = (info->nr_bits +31 ) /32 ; -int bitmap_size = bitmap_nr *sizeof (uint32_t ); +size_t bitmap_size =st_mult (bitmap_nr,sizeof (uint32_t ));uint32_t *tmp =xmalloc (bitmap_size);/* to be freed before return */ uint32_t *bitmap =paint_alloc (info);struct commit *c =lookup_commit_reference_gently (sha1,1 );
submodule.c +3 -3
@@ -123,7 +123,7 @@ static int add_submodule_odb(const char *path)struct strbuf objects_directory =STRBUF_INIT ;struct alternate_object_database *alt_odb;int ret =0 ; -int alloc; +size_t alloc;strbuf_git_path_submodule (&objects_directory, path,"objects/" );if (!is_directory (objects_directory.buf)) {
@@ -138,8 +138,8 @@ static int add_submodule_odb(const char *path) objects_directory.len)) goto done; - alloc = objects_directory.len +42 ;/* for "12/345..." sha1 */ - alt_odb =xmalloc (sizeof (*alt_odb) + alloc); + alloc =st_add (objects_directory.len,42 );/* for "12/345..." sha1 */ + alt_odb =xmalloc (st_add (sizeof (*alt_odb), alloc)); alt_odb->next = alt_odb_list;xsnprintf (alt_odb->base, alloc,"%s" , objects_directory.buf); alt_odb->name = alt_odb->base + objects_directory.len;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 12/22 prepare_{git,shell}_cmd: use argv_array
In reply to Jeff KingThese functions transform an existing argv into one suitable for exec-ing or spawning via git or a shell. We can use an argv_array in each to avoid dealing with manual counting and allocation.
This also makes the memory allocation more clear and fixes some leaks. In prepare_shell_cmd, we would sometimes allocate a new string with "$@" in it and sometimes not, meaning the caller could not correctly free it. On the non-Windows side, we are in a child process which will exec() or exit() immediately, so the leak isn't a big deal. On Windows, though, we use spawn() from the parent process, and leak a string for each shell command we run. On top of that, the Windows code did not free the allocated argv array at all (but does for the prepare_git_cmd case!).
By switching both of these functions to write into an argv_array, we can consistently free the result as appropriate.
--- exec_cmd.c | 28 +++++++++++----------------- exec_cmd.h | 4 +++- run-command.c | 60 +++++++++++++++++++++++++---------------------------------- 3 files changed, 39 insertions(+), 53 deletions(-)
exec_cmd.c +11 -17
@@ -1,6 +1,7 @@#include "cache.h" #include "exec_cmd.h" #include "quote.h" +#include "argv-array.h" #define MAX_ARGS 32static const char *argv_exec_path;
@@ -107,32 +108,25 @@ void setup_path(void)strbuf_release (&new_path); } -const char **prepare_git_cmd (const char **argv) +const char **prepare_git_cmd (struct argv_array *out,const char **argv) { -int argc; -const char **nargv; - -for (argc =0 ; argv[argc]; argc++) - ;/* just counting */ - nargv =xmalloc (sizeof (*nargv) * (argc +2 )); - - nargv[0 ] ="git" ; -for (argc =0 ; argv[argc]; argc++) - nargv[argc +1 ] = argv[argc]; - nargv[argc +1 ] =NULL ; -return nargv; +argv_array_push (out,"git" ); +argv_array_pushv (out, argv); +return out->argv; }int execv_git_cmd (const char **argv) { -const char **nargv =prepare_git_cmd (argv); -trace_argv_printf (nargv,"trace: exec:" ); +struct argv_array nargv =ARGV_ARRAY_INIT ; + +prepare_git_cmd (&nargv, argv); +trace_argv_printf (nargv.argv,"trace: exec:" );/* execvp() can only ever return if it fails */ -sane_execvp ("git" , (char **)nargv); +sane_execvp ("git" , (char **)nargv.argv);trace_printf ("trace: exec failed: %s\n" ,strerror (errno)); -free (nargv); +argv_array_clear (&nargv);return -1 ; }
exec_cmd.h +3 -1
@@ -1,11 +1,13 @@#ifndef GIT_EXEC_CMD_H #define GIT_EXEC_CMD_H +struct argv_array ; +extern void git_set_argv_exec_path (const char *exec_path);extern const char *git_extract_argv0_path (const char *path);extern const char *git_exec_path (void );extern void setup_path (void ); -extern const char **prepare_git_cmd (const char **argv); +extern const char **prepare_git_cmd (struct argv_array *out,const char **argv);extern int execv_git_cmd (const char **argv);/* NULL terminated */ LAST_ARG_MUST_BE_NULL extern int execl_git_cmd(const char *cmd, ...);
run-command.c +25 -35
99 lines not on this page · raw message
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 13/22 write_untracked_extension: use FLEX_ALLOC helper
In reply to Jeff KingWe perform unchecked additions when computing the size of a "struct ondisk_untracked_cache". This is unlikely to have an integer overflow in practice, but we'd like to avoid this dangerous pattern to make further audits easier.
Note that there's one subtlety here, though. We protect ourselves against a NULL exclude_per_dir entry in our source, and avoid calling strlen() on it, keeping "len" at
0. But later, we unconditionally memcpy "len + 1" bytes to get the trailing NUL byte. If we did have a NULL exclude_per_dir, we would read from bogus memory.
As it turns out, though, we always create this field pointing to a string literal, so there's no bug. We can just get rid of the pointless extra conditional.
--- dir.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
dir.c +4 -5
@@ -2360,16 +2360,15 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untrastruct ondisk_untracked_cache *ouc;struct write_data wd;unsigned char varbuf[16 ]; -int len =0 , varint_len; -if (untracked->exclude_per_dir) - len =strlen (untracked->exclude_per_dir); - ouc =xmalloc (sizeof (*ouc) + len +1 ); +int varint_len; +size_t len =strlen (untracked->exclude_per_dir); + +FLEX_ALLOC_MEM (ouc, exclude_per_dir, untracked->exclude_per_dir, len);stat_data_to_disk (&ouc->info_exclude_stat, &untracked->ss_info_exclude.stat);stat_data_to_disk (&ouc->excludes_file_stat, &untracked->ss_excludes_file.stat);hashcpy (ouc->info_exclude_sha1, untracked->ss_info_exclude.sha1);hashcpy (ouc->excludes_file_sha1, untracked->ss_excludes_file.sha1); ouc->dir_flags =htonl (untracked->dir_flags); -memcpy (ouc->exclude_per_dir, untracked->exclude_per_dir, len +1 ); varint_len =encode_varint (untracked->ident.len, varbuf);strbuf_add (out, varbuf, varint_len);
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 14/22 fast-import: simplify allocation in start_packfile
In reply to Jeff KingThis function allocate a packed_git flex-array, and adds a mysterious 2 bytes to the length of the pack_name field. One is for the trailing NUL, but the other has no purpose. This is probably cargo-culted from add_packed_git, which gets the ".idx" path and needed to allocate enough space to hold the matching ".pack" (though since 48bcc1c, we calculate the size there differently).
This site, however, is using the raw path of a tempfile, and does not need the extra byte. We can just replace the allocation with FLEX_ALLOC_STR, which handles the allocation and the NUL for us.
--- fast-import.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-)
fast-import.c +1 -4
@@ -865,15 +865,12 @@ static void start_packfile(void) {static char tmp_file[PATH_MAX ];struct packed_git *p; -int namelen;struct pack_header hdr;int pack_fd; pack_fd =odb_mkstemp (tmp_file,sizeof (tmp_file),"pack/tmp_pack_XXXXXX" ); - namelen =strlen (tmp_file) +2 ; - p =xcalloc (1 ,sizeof (*p) + namelen); -xsnprintf (p->pack_name, namelen,"%s" , tmp_file); +FLEX_ALLOC_STR (p, pack_name, tmp_file); p->pack_fd = pack_fd; p->do_not_close =1 ; pack_file =sha1fd (pack_fd, p->pack_name);
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 15/22 fetch-pack: simplify add_sought_entry
In reply to Jeff KingWe have two variants of this function, one that takes a string and one that takes a ptr/len combo. But we only call the latter with the length of a NUL-terminated string, so our first simplification is to drop it in favor of the string variant.
Since we know we have a string, we can also replace the manual memory computation with a call to alloc_ref().
Furthermore, we can rely on get_oid_hex() to complain if it hits the end of the string. That means we can simplify the check for "<sha1> <ref>" versus just "<ref>". Rather than manage the ptr/len pair, we can just bump the start of our string forward. The original code over-allocated based on the original "namelen" (which wasn't _wrong_, but was simply wasteful and confusing).
--- builtin/fetch-pack.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
builtin/fetch-pack.c +9 -18
@@ -10,33 +10,24 @@ static const char fetch_pack_usage[] ="[--include-tag] [--upload-pack=<git-upload-pack>] [--depth=<n>] " "[--no-progress] [--diag-url] [-v] [<host>:]<directory> [<refs>...]" ; -static void add_sought_entry_mem (struct ref ***sought,int *nr,int *alloc, -const char *name,int namelen) +static void add_sought_entry (struct ref ***sought,int *nr,int *alloc, +const char *name) { -struct ref *ref =xcalloc (1 ,sizeof (*ref) + namelen +1 ); +struct ref *ref;struct object_id oid; -const int chunksz =GIT_SHA1_HEXSZ +1 ; -if (namelen > chunksz && name[chunksz -1 ] ==' ' && - !get_oid_hex (name, &oid)) { -oidcpy (&ref->old_oid, &oid); - name += chunksz; - namelen -= chunksz; - } +if (!get_oid_hex (name, &oid) && name[GIT_SHA1_HEXSZ ] ==' ' ) + name +=GIT_SHA1_HEXSZ +1 ; +else +oidclr (&oid); -memcpy (ref->name, name, namelen); - ref->name[namelen] ='\0' ; + ref =alloc_ref (name); +oidcpy (&ref->old_oid, &oid); (*nr)++;ALLOC_GROW (*sought, *nr, *alloc); (*sought)[*nr -1 ] = ref; } -static void add_sought_entry (struct ref ***sought,int *nr,int *alloc, -const char *string) -{ -add_sought_entry_mem (sought, nr, alloc, string,strlen (string)); -} -int cmd_fetch_pack (int argc,const char **argv,const char *prefix) {int i, ret;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 16/22 test-path-utils: fix normalize_path_copy output buffer size
In reply to Jeff KingThe normalize_path_copy function needs an output buffer that is at least as long as its input (it may shrink the path, but never expand it). However, this test program feeds it static PATH_MAX-sized buffers, which have no relation to the input size.
In the normalize_ceiling_entry case, we do at least check the size against PATH_MAX and die(), but that case is even more convoluted. We normalize into a fixed-size buffer, free the original, and then replace it with a strdup'd copy of the result. But normalize_path_copy explicitly allows normalizing in-place, so we can simply do that.
--- test-path-utils.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
test-path-utils.c +4 -11
@@ -8,21 +8,14 @@*/ static int normalize_ceiling_entry (struct string_list_item *item,void *unused) { -const char *ceil = item->string; -int len =strlen (ceil); -char buf[PATH_MAX +1 ]; +char *ceil = item->string; -if (len ==0 ) +if (!*ceil)die ("Empty path is not supported" ); -if (len >PATH_MAX ) -die ("Path \"%s\" is too long" , ceil);if (!is_absolute_path (ceil))die ("Path \"%s\" is not absolute" , ceil); -if (normalize_path_copy (buf, ceil) <0 ) +if (normalize_path_copy (ceil, ceil) <0 )die ("Path \"%s\" could not be normalized" , ceil); - len =strlen (buf); -free (item->string); - item->string =xstrdup (buf);return 1 ; }
@@ -166,7 +159,7 @@ static struct test_data dirname_data[] = {int main (int argc,char **argv) {if (argc ==3 && !strcmp (argv[1 ],"normalize_path_copy" )) { -char *buf =xmalloc (PATH_MAX +1 ); +char *buf =xmallocz (strlen (argv[2 ]));int rv =normalize_path_copy (buf, argv[2 ]);if (rv) buf ="++failed++" ;
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 17/22 sequencer: simplify memory allocation of get_message
In reply to Jeff KingFor a commit with sha1 "1234abcd" and subject "foo", this function produces a struct with three strings:
1. "foo"
2. "1234abcd... foo"
3. "parent of 1234abcd... foo"
It takes advantage of the fact that these strings are subsets of each other, and allocates only _one_ string, with pointers into the various parts. Unfortunately, this makes the string allocation complicated and hard to follow.
Since we keep only one of these in memory at a time, we can afford to simply allocate three strings. This lets us build on tools like xstrfmt and avoid manual computation.
While we're here, we can also drop the ad-hoc reimplementation of get_git_commit_encoding(), and simply call that function.
--- sequencer.c | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-)
sequencer.c +10 -19
@@ -124,42 +124,33 @@ static const char *action_name(const struct replay_opts *opts)struct commit_message {char *parent_label; -const char *label; -const char *subject; +char *label; +char *subject;const char *message; };static int get_message (struct commit *commit,struct commit_message *out) {const char *abbrev, *subject; -int abbrev_len, subject_len; -char *q; - -if (!git_commit_encoding) - git_commit_encoding ="UTF-8" ; +int subject_len; - out->message =logmsg_reencode (commit,NULL , git_commit_encoding); + out->message =logmsg_reencode (commit,NULL ,get_commit_output_encoding ()); abbrev =find_unique_abbrev (commit->object.oid.hash,DEFAULT_ABBREV ); - abbrev_len =strlen (abbrev); subject_len =find_commit_subject (out->message, &subject); - out->parent_label =xmalloc (strlen ("parent of " ) + abbrev_len + -strlen ("... " ) + subject_len +1 ); - q = out->parent_label; - q =mempcpy (q,"parent of " ,strlen ("parent of " )); - out->label = q; - q =mempcpy (q, abbrev, abbrev_len); - q =mempcpy (q,"... " ,strlen ("... " )); - out->subject = q; - q =mempcpy (q, subject, subject_len); - *q ='\0' ; + out->subject =xmemdupz (subject, subject_len); + out->label =xstrfmt ("%s... %s" , abbrev, out->subject); + out->parent_label =xstrfmt ("parent of %s" , out->label); +return 0 ; }static void free_message (struct commit *commit,struct commit_message *msg) {free (msg->parent_label); +free (msg->label); +free (msg->subject);unuse_commit_buffer (commit, msg->message); }
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 18/22 git-compat-util: drop mempcpy compat code
In reply to Jeff KingThere are no callers of this left, as the last one was dropped in the previous patch. And there are not likely to be new ones, as the function has been around since 2010 without gaining any new callers.
--- git-compat-util.h | 9 --------- 1 file changed, 9 deletions(-)
git-compat-util.h +0 -9
@@ -681,7 +681,6 @@ extern int git_vsnprintf(char *str, size_t maxsize,#ifdef __GLIBC_PREREQ#if __GLIBC_PREREQ (2 ,1 )#define HAVE_STRCHRNUL -#define HAVE_MEMPCPY #endif #endif
@@ -695,14 +694,6 @@ static inline char *gitstrchrnul(const char *s, int c) }#endif -#ifndef HAVE_MEMPCPY -#define mempcpy gitmempcpy -static inline void *gitmempcpy (void *dest,const void *src,size_t n) -{ -return (char *)memcpy (dest, src, n) + n; -} -#endif -#ifdef NO_INET_PTON int inet_pton (int af,const char *src,void *dst);#endif
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 19/22 transport_anonymize_url: use xstrfmt
In reply to Jeff KingThis function uses xcalloc and two memcpy calls to concatenate two strings. We can do this as an xstrfmt one-liner, and then it is more clear that we are allocating the correct amount of memory.
--- transport.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
transport.c +3 -5
@@ -1021,7 +1021,7 @@ int transport_disconnect(struct transport *transport)*/ char *transport_anonymize_url (const char *url) { -char *anon_url, *scheme_prefix, *anon_part; +char *scheme_prefix, *anon_part;size_t anon_len, prefix_len =0 ; anon_part =strchr (url,'@' );
@@ -1055,10 +1055,8 @@ char *transport_anonymize_url(const char *url) goto literal_copy; prefix_len = scheme_prefix - url +3 ; } - anon_url =xcalloc (1 ,1 + prefix_len + anon_len); -memcpy (anon_url, url, prefix_len); -memcpy (anon_url + prefix_len, anon_part, anon_len); -return anon_url; +return xstrfmt ("%.*s%.*s" , (int )prefix_len, url, + (int )anon_len, anon_part); literal_copy:return xstrdup (url); }
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 20/22 diff_populate_gitlink: use a strbuf
In reply to Jeff KingWe allocate 100 bytes to hold the "Submodule commit ..." text. This is enough, but it's not immediately obvious that this is the case, and we have to repeat the magic 100 twice.
We could get away with xstrfmt here, but we want to know the size, as well, so let's use a real strbuf. And while we're here, we can clean up the logic around size_only. It currently sets and clears the "data" field pointlessly, and leaves the "should_free" flag on even after we have cleared the data.
--- diff.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
diff.c +8 -8
@@ -2704,21 +2704,21 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, intstatic int diff_populate_gitlink (struct diff_filespec *s,int size_only) { -int len; -char *data =xmalloc (100 ), *dirty ="" ; +struct strbuf buf =STRBUF_INIT ; +char *dirty ="" ;/* Are we looking at the work tree? */ if (s->dirty_submodule) dirty ="-dirty" ; - len =snprintf (data,100 , -"Subproject commit %s%s\n" ,sha1_to_hex (s->sha1), dirty); - s->data = data; - s->size = len; - s->should_free =1 ; +strbuf_addf (&buf,"Subproject commit %s%s\n" ,sha1_to_hex (s->sha1), dirty); + s->size = buf.len;if (size_only) { s->data =NULL ; -free (data); +strbuf_release (&buf); + }else { + s->data =strbuf_detach (&buf,NULL ); + s->should_free =1 ; }return 0 ; }
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 21/22 convert ewah/bitmap code to use xmalloc
In reply to Jeff KingThis code was originally written with the idea that it could be spun off into its own ewah library, and uses the overrideable ewah_malloc to do allocations.
We plug in xmalloc as our ewah_malloc, of course. But over the years the ewah code itself has become more entangled with git, and the return value of many ewah_malloc sites is not checked.
Let's just drop the level of indirection and use xmalloc and friends directly. This saves a few lines, and will let us adapt these sites to our more advanced malloc helpers.
--- ewah/bitmap.c | 12 ++++++------ ewah/ewah_bitmap.c | 9 +++------ ewah/ewah_io.c | 10 ++-------- ewah/ewok.h | 10 ---------- 4 files changed, 11 insertions(+), 30 deletions(-)
ewah/bitmap.c +6 -6
@@ -25,8 +25,8 @@struct bitmap *bitmap_new (void ) { -struct bitmap *bitmap =ewah_malloc (sizeof (struct bitmap )); - bitmap->words =ewah_calloc (32 ,sizeof (eword_t)); +struct bitmap *bitmap =xmalloc (sizeof (struct bitmap )); + bitmap->words =xcalloc (32 ,sizeof (eword_t)); bitmap->word_alloc =32 ;return bitmap; }
@@ -38,8 +38,8 @@ void bitmap_set(struct bitmap *self, size_t pos)if (block >= self->word_alloc) {size_t old_size = self->word_alloc; self->word_alloc = block *2 ; - self->words =ewah_realloc (self->words, - self->word_alloc *sizeof (eword_t)); + self->words =xrealloc (self->words, + self->word_alloc *sizeof (eword_t));memset (self->words + old_size,0x0 , (self->word_alloc - old_size) *sizeof (eword_t));
@@ -102,7 +102,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)while (ewah_iterator_next (&blowup, &it)) {if (i >= bitmap->word_alloc) { bitmap->word_alloc *=1.5 ; - bitmap->words =ewah_realloc ( + bitmap->words =xrealloc ( bitmap->words, bitmap->word_alloc *sizeof (eword_t)); }
@@ -134,7 +134,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)if (self->word_alloc < other_final) { self->word_alloc = other_final; - self->words =ewah_realloc (self->words, + self->words =xrealloc (self->words, self->word_alloc *sizeof (eword_t));memset (self->words + original_size,0x0 , (self->word_alloc - original_size) *sizeof (eword_t));
ewah/ewah_bitmap.c +3 -6
@@ -39,7 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)return ; self->alloc_size = new_size; - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); self->rlw = self->buffer + (rlw_offset /sizeof (eword_t)); }
@@ -282,11 +282,8 @@ struct ewah_bitmap *ewah_new(void) {struct ewah_bitmap *self; - self =ewah_malloc (sizeof (struct ewah_bitmap )); -if (self ==NULL ) -return NULL ; - - self->buffer =ewah_malloc (32 *sizeof (eword_t)); + self =xmalloc (sizeof (struct ewah_bitmap )); + self->buffer =xmalloc (32 *sizeof (eword_t)); self->alloc_size =32 ;ewah_clear (self);
ewah/ewah_io.c +2 -8
@@ -134,12 +134,9 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len) self->buffer_size = self->alloc_size =get_be32 (ptr); ptr +=sizeof (uint32_t ); - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); -if (!self->buffer) -return -1 ; -/* * Copy the raw data for the bitmap as a whole chunk; * if we're in a little-endian platform, we'll perform
@@ -180,12 +177,9 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)return -1 ; self->buffer_size = self->alloc_size = (size_t )ntohl (word_count); - self->buffer =ewah_realloc (self->buffer, + self->buffer =xrealloc (self->buffer, self->alloc_size *sizeof (eword_t)); -if (!self->buffer) -return -1 ; -/** 64 bit x N -- compressed words */ buffer = self->buffer; words_left = self->buffer_size;
ewah/ewok.h +0 -10
@@ -20,16 +20,6 @@#ifndef __EWOK_BITMAP_H__#define __EWOK_BITMAP_H__ -#ifndef ewah_malloc -# define ewah_malloc xmalloc -#endif -#ifndef ewah_realloc -# define ewah_realloc xrealloc -#endif -#ifndef ewah_calloc -# define ewah_calloc xcalloc -#endif -struct strbuf ;typedef uint64_t eword_t ;#define BITS_IN_EWORD (sizeof(eword_t) * 8)
Signature · 1 line
2.7.2.645.g4e1306c
PATCH v3 22/22 ewah: convert to REALLOC_ARRAY, etc
In reply to Jeff KingNow that we're built around xmalloc and friends, we can use helpers like REALLOC_ARRAY, ALLOC_GROW, and so on to make the code shorter and protect against integer overflow.
--- ewah/bitmap.c | 16 ++++------------ ewah/ewah_bitmap.c | 5 ++--- ewah/ewah_io.c | 6 ++---- 3 files changed, 8 insertions(+), 19 deletions(-)
ewah/bitmap.c +4 -12
@@ -17,7 +17,7 @@* along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "git-compat-util.h" +#include "cache.h" #include "ewok.h" #define EWAH_MASK (x) ((eword_t)1 << (x % BITS_IN_EWORD))
@@ -38,9 +38,7 @@ void bitmap_set(struct bitmap *self, size_t pos)if (block >= self->word_alloc) {size_t old_size = self->word_alloc; self->word_alloc = block *2 ; - self->words =xrealloc (self->words, - self->word_alloc *sizeof (eword_t)); - +REALLOC_ARRAY (self->words, self->word_alloc);memset (self->words + old_size,0x0 , (self->word_alloc - old_size) *sizeof (eword_t)); }
@@ -100,12 +98,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)ewah_iterator_init (&it, ewah);while (ewah_iterator_next (&blowup, &it)) { -if (i >= bitmap->word_alloc) { - bitmap->word_alloc *=1.5 ; - bitmap->words =xrealloc ( - bitmap->words, bitmap->word_alloc *sizeof (eword_t)); - } - +ALLOC_GROW (bitmap->words, i +1 , bitmap->word_alloc); bitmap->words[i++] = blowup; }
@@ -134,8 +127,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)if (self->word_alloc < other_final) { self->word_alloc = other_final; - self->words =xrealloc (self->words, - self->word_alloc *sizeof (eword_t)); +REALLOC_ARRAY (self->words, self->word_alloc);memset (self->words + original_size,0x0 , (self->word_alloc - original_size) *sizeof (eword_t)); }
ewah/ewah_bitmap.c +2 -3
@@ -39,8 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)return ; self->alloc_size = new_size; - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size); self->rlw = self->buffer + (rlw_offset /sizeof (eword_t)); }
@@ -283,8 +282,8 @@ struct ewah_bitmap *ewah_new(void)struct ewah_bitmap *self; self =xmalloc (sizeof (struct ewah_bitmap )); - self->buffer =xmalloc (32 *sizeof (eword_t)); self->alloc_size =32 ; +ALLOC_ARRAY (self->buffer, self->alloc_size);ewah_clear (self);return self;
ewah/ewah_io.c +2 -4
@@ -134,8 +134,7 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len) self->buffer_size = self->alloc_size =get_be32 (ptr); ptr +=sizeof (uint32_t ); - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size);/* * Copy the raw data for the bitmap as a whole chunk;
@@ -177,8 +176,7 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)return -1 ; self->buffer_size = self->alloc_size = (size_t )ntohl (word_count); - self->buffer =xrealloc (self->buffer, - self->alloc_size *sizeof (eword_t)); +REALLOC_ARRAY (self->buffer, self->alloc_size);/** 64 bit x N -- compressed words */ buffer = self->buffer;
Signature · 1 line
2.7.2.645.g4e1306c
Quoted 34 lines · Jeff King
Quoted 3 lines · Jeff King
Here's a re-roll of jk/tighten-alloc series from:
http://thread.gmane.org/gmane.comp.version-control.git/286253
And here's v3. The changes this time (and philosophical rationalization of changes I didn't make) are pretty small:
- flip the order of arguments to
st_mult()inALLOC_ARRAY, et al, to get a probably-irrelevant-but-so-easy-why-not optimization- mark new global var in
daemon.cas static- add
argv_array_detach(new patch 6) to avoid gross memory management when converting line-log to useargv_arrayin the subsequent patchSo I hope this one is ready for 'next'. Thanks René, Ramsay, and Eric for reviewing.
[01/22]:
reflog_expire_cfg: NUL-terminate pattern field [02/22]: add helpers for detectingsize_toverflow
[03/22]: tree-diff: catch integer overflow incombine_diff_pathallocation [04/22]: hardenREALLOC_ARRAYand xcalloc againstsize_toverflow [05/22]: add helpers for allocating flex-array structs [06/22]: argv-array: add detach function
[07/22]: convert manual allocations toargv_array
[08/22]: convert trivial cases toALLOC_ARRAY
[09/22]: use xmallocz to avoid size arithmetic
[10/22]: convert trivial cases toFLEX_ARRAYmacros
[11/22]: usest_addandst_multfor allocation size computation [12/22]: prepare_{git,shell}_cmd: useargv_array
[13/22]:write_untracked_extension: useFLEX_ALLOChelper [14/22]: fast-import: simplify allocation instart_packfile
Quoted 8 lines
[15/22]: fetch-pack: simplify
add_sought_entry
[16/22]: test-path-utils: fixnormalize_path_copyoutput buffer size [17/22]: sequencer: simplify memory allocation ofget_message[18/22]: git-compat-util: drop mempcpy compat code [19/22]:transport_anonymize_url: use xstrfmt
[20/22]:diff_populate_gitlink: use a strbuf
[21/22]: convert ewah/bitmap code to use xmalloc
[22/22]: ewah: convert toREALLOC_ARRAY, etc
I admit that I only looked at tbdiff output, but what I saw looked very sensible.
Thanks.