summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar Junio C Hamano <gitster@pobox.com>2016-03-10 11:13:43 -0800
committerLibravatar Junio C Hamano <gitster@pobox.com>2016-03-10 11:13:43 -0800
commitb7a6ec609ff10652541e7f716fcecf7865b94b23 (patch)
tree92c449ecb681311c3713a1d2be178776b05bd259
parentMerge branch 'jk/more-comments-on-textconv' into maint (diff)
parentcompat/mingw: brown paper bag fix for 50a6c8e (diff)
downloadtgif-b7a6ec609ff10652541e7f716fcecf7865b94b23.tar.xz
Merge branch 'jk/tighten-alloc' into maint
* jk/tighten-alloc: (23 commits) compat/mingw: brown paper bag fix for 50a6c8e ewah: convert to REALLOC_ARRAY, etc convert ewah/bitmap code to use xmalloc diff_populate_gitlink: use a strbuf transport_anonymize_url: use xstrfmt git-compat-util: drop mempcpy compat code sequencer: simplify memory allocation of get_message test-path-utils: fix normalize_path_copy output buffer size fetch-pack: simplify add_sought_entry fast-import: simplify allocation in start_packfile write_untracked_extension: use FLEX_ALLOC helper prepare_{git,shell}_cmd: use argv_array use st_add and st_mult for allocation size computation convert trivial cases to FLEX_ARRAY macros use xmallocz to avoid size arithmetic convert trivial cases to ALLOC_ARRAY convert manual allocations to argv_array argv-array: add detach function add helpers for allocating flex-array structs harden REALLOC_ARRAY and xcalloc against size_t overflow ...
-rw-r--r--Documentation/technical/api-argv-array.txt7
-rw-r--r--alias.c2
-rw-r--r--archive.c4
-rw-r--r--argv-array.c11
-rw-r--r--argv-array.h1
-rw-r--r--attr.c6
-rw-r--r--bisect.c4
-rw-r--r--builtin/apply.c2
-rw-r--r--builtin/blame.c7
-rw-r--r--builtin/check-ref-format.c2
-rw-r--r--builtin/clean.c4
-rw-r--r--builtin/fast-export.c2
-rw-r--r--builtin/fetch-pack.c27
-rw-r--r--builtin/fetch.c2
-rw-r--r--builtin/grep.c10
-rw-r--r--builtin/help.c9
-rw-r--r--builtin/index-pack.c8
-rw-r--r--builtin/merge-base.c2
-rw-r--r--builtin/merge-tree.c2
-rw-r--r--builtin/merge.c2
-rw-r--r--builtin/mktree.c9
-rw-r--r--builtin/mv.c7
-rw-r--r--builtin/pack-objects.c7
-rw-r--r--builtin/pack-redundant.c2
-rw-r--r--builtin/receive-pack.c19
-rw-r--r--builtin/reflog.c13
-rw-r--r--builtin/remote-ext.c26
-rw-r--r--builtin/worktree.c2
-rw-r--r--cache-tree.c4
-rw-r--r--column.c5
-rw-r--r--combine-diff.c26
-rw-r--r--commit.c4
-rw-r--r--compat/mingw.c10
-rw-r--r--compat/qsort.c2
-rw-r--r--compat/setenv.c2
-rw-r--r--compat/win32/syslog.c4
-rw-r--r--config.c4
-rw-r--r--daemon.c12
-rw-r--r--diff.c23
-rw-r--r--diff.h4
-rw-r--r--diffcore-delta.c6
-rw-r--r--diffcore-order.c4
-rw-r--r--diffcore-rename.c2
-rw-r--r--dir.c37
-rw-r--r--entry.c2
-rw-r--r--ewah/bitmap.c20
-rw-r--r--ewah/ewah_bitmap.c10
-rw-r--r--ewah/ewah_io.c12
-rw-r--r--ewah/ewok.h10
-rw-r--r--exec_cmd.c28
-rw-r--r--exec_cmd.h4
-rw-r--r--fast-import.c12
-rw-r--r--fsck.c3
-rw-r--r--git-compat-util.h108
-rw-r--r--git.c14
-rw-r--r--graph.c10
-rw-r--r--grep.c3
-rw-r--r--hashmap.c3
-rw-r--r--help.c6
-rw-r--r--imap-send.c5
-rw-r--r--khash.h2
-rw-r--r--levenshtein.c8
-rw-r--r--line-log.c30
-rw-r--r--ll-merge.c2
-rw-r--r--log-tree.c5
-rw-r--r--name-hash.c3
-rw-r--r--notes.c2
-rw-r--r--pack-check.c2
-rw-r--r--pack-revindex.c12
-rw-r--r--pathspec.c5
-rw-r--r--progress.c2
-rw-r--r--ref-filter.c6
-rw-r--r--refs.c10
-rw-r--r--refs/files-backend.c19
-rw-r--r--remote-curl.c26
-rw-r--r--remote.c13
-rw-r--r--revision.c2
-rw-r--r--run-command.c60
-rw-r--r--sequencer.c29
-rw-r--r--setup.c5
-rw-r--r--sha1_file.c24
-rw-r--r--sha1_name.c5
-rw-r--r--shallow.c8
-rw-r--r--show-index.c3
-rw-r--r--strbuf.c2
-rw-r--r--submodule.c6
-rw-r--r--test-path-utils.c15
-rw-r--r--transport.c10
-rw-r--r--tree-diff.c4
-rw-r--r--wrapper.c3
-rw-r--r--xdiff-interface.c2
91 files changed, 441 insertions, 482 deletions
diff --git a/Documentation/technical/api-argv-array.txt b/Documentation/technical/api-argv-array.txt
index 8076172a08..cfc063018c 100644
--- a/Documentation/technical/api-argv-array.txt
+++ b/Documentation/technical/api-argv-array.txt
@@ -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.
diff --git a/alias.c b/alias.c
index a11229db9e..3b90397a99 100644
--- a/alias.c
+++ b/alias.c
@@ -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;
diff --git a/archive.c b/archive.c
index 0687afae43..5d735ae603 100644
--- a/archive.c
+++ b/archive.c
@@ -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;
diff --git a/argv-array.c b/argv-array.c
index eaed47712b..5d370fa336 100644
--- a/argv-array.c
+++ b/argv-array.c
@@ -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;
+ }
+}
diff --git a/argv-array.h b/argv-array.h
index a2fa0aa606..29056e49a1 100644
--- a/argv-array.h
+++ b/argv-array.h
@@ -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 */
diff --git a/attr.c b/attr.c
index 086c08dcfa..6537a433da 100644
--- a/attr.c
+++ b/attr.c
@@ -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++;
@@ -799,7 +797,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;
diff --git a/bisect.c b/bisect.c
index 42aa7aa6aa..6d0793b9fc 100644
--- a/bisect.c
+++ b/bisect.c
@@ -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]);
diff --git a/builtin/apply.c b/builtin/apply.c
index deb1364fa8..0db6d14cc2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -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,
diff --git a/builtin/blame.c b/builtin/blame.c
index 5265f79edc..0b4f0bbb53 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -459,13 +459,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;
}
@@ -2042,7 +2040,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;
diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c
index fd915d5984..eac499450f 100644
--- a/builtin/check-ref-format.c
+++ b/builtin/check-ref-format.c
@@ -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;
diff --git a/builtin/clean.c b/builtin/clean.c
index 919157bc2f..fb1824ce95 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -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;
@@ -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;
diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index 2471297f71..8164b581a6 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -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;
diff --git a/builtin/fetch-pack.c b/builtin/fetch-pack.c
index cf3019e05b..7d5914f921 100644
--- a/builtin/fetch-pack.c
+++ b/builtin/fetch-pack.c
@@ -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;
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 17f40e10f6..683f08ec91 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -1107,7 +1107,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++;
diff --git a/builtin/grep.c b/builtin/grep.c
index 3ba35ecf93..65c02010c7 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -354,17 +354,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)
diff --git a/builtin/help.c b/builtin/help.c
index 1cd0c1ee44..3c55ce4563 100644
--- a/builtin/help.c
+++ b/builtin/help.c
@@ -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;
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 6a01509587..193908a619 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -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);
@@ -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_o