Skip to content

Commit b7a6ec6

Browse files
committed
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 ...
2 parents aa6c22e + 8d5b332 commit b7a6ec6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+441
-482
lines changed

Documentation/technical/api-argv-array.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ Functions
5656
`argv_array_clear`::
5757
Free all memory associated with the array and return it to the
5858
initial, empty state.
59+
60+
`argv_array_detach`::
61+
Disconnect the `argv` member from the `argv_array` struct and
62+
return it. The caller is responsible for freeing the memory used
63+
by the array, and by the strings it references. After detaching,
64+
the `argv_array` is in a reinitialized state and can be pushed
65+
into again.

alias.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ int split_cmdline(char *cmdline, const char ***argv)
2323
int src, dst, count = 0, size = 16;
2424
char quoted = 0;
2525

26-
*argv = xmalloc(sizeof(**argv) * size);
26+
ALLOC_ARRAY(*argv, size);
2727

2828
/* split alias_string */
2929
(*argv)[count++] = cmdline;

archive.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ static void queue_directory(const unsigned char *sha1,
171171
unsigned mode, int stage, struct archiver_context *c)
172172
{
173173
struct directory *d;
174-
size_t len = base->len + 1 + strlen(filename) + 1;
175-
d = xmalloc(sizeof(*d) + len);
174+
size_t len = st_add4(base->len, 1, strlen(filename), 1);
175+
d = xmalloc(st_add(sizeof(*d), len));
176176
d->up = c->bottom;
177177
d->baselen = base->len;
178178
d->mode = mode;

argv-array.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,14 @@ void argv_array_clear(struct argv_array *array)
7474
}
7575
argv_array_init(array);
7676
}
77+
78+
const char **argv_array_detach(struct argv_array *array)
79+
{
80+
if (array->argv == empty_argv)
81+
return xcalloc(1, sizeof(const char *));
82+
else {
83+
const char **ret = array->argv;
84+
argv_array_init(array);
85+
return ret;
86+
}
87+
}

argv-array.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ void argv_array_pushl(struct argv_array *, ...);
2020
void argv_array_pushv(struct argv_array *, const char **);
2121
void argv_array_pop(struct argv_array *);
2222
void argv_array_clear(struct argv_array *);
23+
const char **argv_array_detach(struct argv_array *);
2324

2425
#endif /* ARGV_ARRAY_H */

attr.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,7 @@ static struct git_attr *git_attr_internal(const char *name, int len)
9393
if (invalid_attr_name(name, len))
9494
return NULL;
9595

96-
a = xmalloc(sizeof(*a) + len + 1);
97-
memcpy(a->name, name, len);
98-
a->name[len] = 0;
96+
FLEX_ALLOC_MEM(a, name, name, len);
9997
a->h = hval;
10098
a->next = git_attr_hash[pos];
10199
a->attr_nr = attr_nr++;
@@ -799,7 +797,7 @@ int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
799797
++count;
800798
}
801799
*num = count;
802-
*check = xmalloc(sizeof(**check) * count);
800+
ALLOC_ARRAY(*check, count);
803801
j = 0;
804802
for (i = 0; i < attr_nr; i++) {
805803
const char *value = check_all_attr[i].value;

bisect.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,10 @@ static struct commit *get_commit_reference(const unsigned char *sha1)
708708

709709
static struct commit **get_bad_and_good_commits(int *rev_nr)
710710
{
711-
int len = 1 + good_revs.nr;
712-
struct commit **rev = xmalloc(len * sizeof(*rev));
711+
struct commit **rev;
713712
int i, n = 0;
714713

714+
ALLOC_ARRAY(rev, 1 + good_revs.nr);
715715
rev[n++] = get_commit_reference(current_bad_oid->hash);
716716
for (i = 0; i < good_revs.nr; i++)
717717
rev[n++] = get_commit_reference(good_revs.sha1[i]);

builtin/apply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,7 @@ static void update_image(struct image *img,
26322632
insert_count = postimage->len;
26332633

26342634
/* Adjust the contents */
2635-
result = xmalloc(img->len + insert_count - remove_count + 1);
2635+
result = xmalloc(st_add3(st_sub(img->len, remove_count), insert_count, 1));
26362636
memcpy(result, img->buf, applied_at);
26372637
memcpy(result + applied_at, postimage->buf, postimage->len);
26382638
memcpy(result + applied_at + postimage->len,

builtin/blame.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,11 @@ static void queue_blames(struct scoreboard *sb, struct origin *porigin,
459459
static struct origin *make_origin(struct commit *commit, const char *path)
460460
{
461461
struct origin *o;
462-
size_t pathlen = strlen(path) + 1;
463-
o = xcalloc(1, sizeof(*o) + pathlen);
462+
FLEX_ALLOC_STR(o, path, path);
464463
o->commit = commit;
465464
o->refcnt = 1;
466465
o->next = commit->util;
467466
commit->util = o;
468-
memcpy(o->path, path, pathlen); /* includes NUL */
469467
return o;
470468
}
471469

@@ -2042,7 +2040,8 @@ static int prepare_lines(struct scoreboard *sb)
20422040
for (p = buf; p < end; p = get_next_line(p, end))
20432041
num++;
20442042

2045-
sb->lineno = lineno = xmalloc(sizeof(*sb->lineno) * (num + 1));
2043+
ALLOC_ARRAY(sb->lineno, num + 1);
2044+
lineno = sb->lineno;
20462045

20472046
for (p = buf; p < end; p = get_next_line(p, end))
20482047
*lineno++ = p - buf;

builtin/check-ref-format.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ static const char builtin_check_ref_format_usage[] =
2020
*/
2121
static char *collapse_slashes(const char *refname)
2222
{
23-
char *ret = xmalloc(strlen(refname) + 1);
23+
char *ret = xmallocz(strlen(refname));
2424
char ch;
2525
char prev = '/';
2626
char *cp = ret;

0 commit comments

Comments
 (0)