Skip to content

Commit 60ce816

Browse files
committed
Merge branch 'rs/dup-array'
Code cleaning. * rs/dup-array: use DUP_ARRAY add DUP_ARRAY do full type check in BARF_UNLESS_COPYABLE factor out BARF_UNLESS_COPYABLE mingw: make argv2 in try_shell_exec() non-const
2 parents 90c47b3 + 6e57841 commit 60ce816

File tree

9 files changed

+30
-23
lines changed

9 files changed

+30
-23
lines changed

attr.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,7 @@ struct attr_check *attr_check_dup(const struct attr_check *check)
603603

604604
ret->nr = check->nr;
605605
ret->alloc = check->alloc;
606-
ALLOC_ARRAY(ret->items, ret->nr);
607-
COPY_ARRAY(ret->items, check->items, ret->nr);
606+
DUP_ARRAY(ret->items, check->items, ret->nr);
608607

609608
return ret;
610609
}

builtin/am.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,8 +1492,7 @@ static int run_apply(const struct am_state *state, const char *index_file)
14921492
* apply_opts.v keeps referencing the allocated strings for
14931493
* strvec_clear() to release.
14941494
*/
1495-
ALLOC_ARRAY(apply_argv, apply_opts.nr);
1496-
COPY_ARRAY(apply_argv, apply_opts.v, apply_opts.nr);
1495+
DUP_ARRAY(apply_argv, apply_opts.v, apply_opts.nr);
14971496

14981497
opts_left = apply_parse_options(apply_opts.nr, apply_argv,
14991498
&apply_state, &force_apply, &options,

commit-graph.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,8 +1594,7 @@ static void compute_bloom_filters(struct write_commit_graph_context *ctx)
15941594
_("Computing commit changed paths Bloom filters"),
15951595
ctx->commits.nr);
15961596

1597-
ALLOC_ARRAY(sorted_commits, ctx->commits.nr);
1598-
COPY_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
1597+
DUP_ARRAY(sorted_commits, ctx->commits.list, ctx->commits.nr);
15991598

16001599
if (ctx->order_by_pack)
16011600
QSORT(sorted_commits, ctx->commits.nr, commit_pos_cmp);

commit-reach.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ static int remove_redundant_with_gen(struct repository *r,
245245
* min_gen_pos points to the current position within 'array'
246246
* that is not yet known to be STALE.
247247
*/
248-
ALLOC_ARRAY(sorted, cnt);
249-
COPY_ARRAY(sorted, array, cnt);
248+
DUP_ARRAY(sorted, array, cnt);
250249
QSORT(sorted, cnt, compare_commits_by_gen);
251250
min_generation = commit_graph_generation(sorted[0]);
252251

compat/mingw.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,8 +1396,7 @@ static wchar_t *make_environment_block(char **deltaenv)
13961396
p += s;
13971397
}
13981398

1399-
ALLOC_ARRAY(result, size);
1400-
COPY_ARRAY(result, wenv, size);
1399+
DUP_ARRAY(result, wenv, size);
14011400
FreeEnvironmentStringsW(wenv);
14021401
return result;
14031402
}
@@ -1839,16 +1838,13 @@ static int try_shell_exec(const char *cmd, char *const *argv)
18391838
if (prog) {
18401839
int exec_id;
18411840
int argc = 0;
1842-
#ifndef _MSC_VER
1843-
const
1844-
#endif
18451841
char **argv2;
18461842
while (argv[argc]) argc++;
18471843
ALLOC_ARRAY(argv2, argc + 1);
18481844
argv2[0] = (char *)cmd; /* full path to the script file */
18491845
COPY_ARRAY(&argv2[1], &argv[1], argc);
1850-
exec_id = trace2_exec(prog, argv2);
1851-
pid = mingw_spawnv(prog, argv2, 1);
1846+
exec_id = trace2_exec(prog, (const char **)argv2);
1847+
pid = mingw_spawnv(prog, (const char **)argv2, 1);
18521848
if (pid >= 0) {
18531849
int status;
18541850
if (waitpid(pid, &status, 0) < 0)

contrib/coccinelle/array.cocci

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,10 @@ expression n != 1;
9494
@@
9595
- ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) )
9696
+ CALLOC_ARRAY(ptr, n)
97+
98+
@@
99+
expression dst, src, n;
100+
@@
101+
-ALLOC_ARRAY(dst, n);
102+
-COPY_ARRAY(dst, src, n);
103+
+DUP_ARRAY(dst, src, n);

git-compat-util.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,14 @@ struct strbuf;
9797
# define BARF_UNLESS_AN_ARRAY(arr) \
9898
BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \
9999
__typeof__(&(arr)[0])))
100+
# define BARF_UNLESS_COPYABLE(dst, src) \
101+
BUILD_ASSERT_OR_ZERO(__builtin_types_compatible_p(__typeof__(*(dst)), \
102+
__typeof__(*(src))))
100103
#else
101104
# define BARF_UNLESS_AN_ARRAY(arr) 0
105+
# define BARF_UNLESS_COPYABLE(dst, src) \
106+
BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \
107+
sizeof(*(dst)) == sizeof(*(src)))
102108
#endif
103109
/*
104110
* ARRAY_SIZE - get the number of elements in a visible array
@@ -1102,21 +1108,26 @@ int xstrncmpz(const char *s, const char *t, size_t len);
11021108
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
11031109

11041110
#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
1105-
BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
1111+
BARF_UNLESS_COPYABLE((dst), (src)))
11061112
static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
11071113
{
11081114
if (n)
11091115
memcpy(dst, src, st_mult(size, n));
11101116
}
11111117

11121118
#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
1113-
BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
1119+
BARF_UNLESS_COPYABLE((dst), (src)))
11141120
static inline void move_array(void *dst, const void *src, size_t n, size_t size)
11151121
{
11161122
if (n)
11171123
memmove(dst, src, st_mult(size, n));
11181124
}
11191125

1126+
#define DUP_ARRAY(dst, src, n) do { \
1127+
size_t dup_array_n_ = (n); \
1128+
COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \
1129+
} while (0)
1130+
11201131
/*
11211132
* These functions help you allocate structs with flex arrays, and copy
11221133
* the data directly into the array. For example, if you had:

parse-options.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,7 @@ static struct option *preprocess_options(struct parse_opt_ctx_t *ctx,
702702
if (!nr_aliases)
703703
return NULL;
704704

705-
ALLOC_ARRAY(newopt, nr + 1);
706-
COPY_ARRAY(newopt, options, nr + 1);
705+
DUP_ARRAY(newopt, options, nr + 1);
707706

708707
/* each alias has two string pointers and NULL */
709708
CALLOC_ARRAY(ctx->alias_groups, 3 * (nr_aliases + 1));

pathspec.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
681681
int i, j;
682682

683683
*dst = *src;
684-
ALLOC_ARRAY(dst->items, dst->nr);
685-
COPY_ARRAY(dst->items, src->items, dst->nr);
684+
DUP_ARRAY(dst->items, src->items, dst->nr);
686685

687686
for (i = 0; i < dst->nr; i++) {
688687
struct pathspec_item *d = &dst->items[i];
@@ -691,8 +690,7 @@ void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
691690
d->match = xstrdup(s->match);
692691
d->original = xstrdup(s->original);
693692

694-
ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
695-
COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
693+
DUP_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
696694
for (j = 0; j < d->attr_match_nr; j++) {
697695
const char *value = s->attr_match[j].value;
698696
d->attr_match[j].value = xstrdup_or_null(value);

0 commit comments

Comments
 (0)