Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

Commit 4d2f775

Browse files
committed
Merge branch 'tc/memzero-array' into next
MEMZERO_ARRAY() helper is introduced to avoid clearing only the first N bytes of an N-element array whose elements are larger than a byte. * tc/memzero-array: contrib/coccinelle: pass include paths to spatch(1) git-compat-util: introduce MEMZERO_ARRAY() macro
2 parents d8ae4b5 + 467860b commit 4d2f775

File tree

10 files changed

+36
-10
lines changed

10 files changed

+36
-10
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ SANITIZE_LEAK =
982982
SANITIZE_ADDRESS =
983983

984984
# For the 'coccicheck' target
985-
SPATCH_INCLUDE_FLAGS = --all-includes
985+
SPATCH_INCLUDE_FLAGS = --all-includes $(addprefix -I ,compat ewah refs sha256 trace2 win32 xdiff)
986986
SPATCH_FLAGS =
987987
SPATCH_TEST_FLAGS =
988988

builtin/last-modified.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ static void process_parent(struct last_modified *lm,
327327
if (!(parent->object.flags & PARENT1))
328328
active_paths_free(lm, parent);
329329

330-
memset(lm->scratch->words, 0x0, lm->scratch->word_alloc * sizeof(eword_t));
330+
MEMZERO_ARRAY(lm->scratch->words, lm->scratch->word_alloc);
331331
diff_queue_clear(&diff_queued_diff);
332332
}
333333

compat/simple-ipc/ipc-win32.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ static LPSECURITY_ATTRIBUTES get_sa(struct my_sa_data *d)
686686
goto fail;
687687
}
688688

689-
memset(ea, 0, NR_EA * sizeof(EXPLICIT_ACCESS));
689+
MEMZERO_ARRAY(ea, NR_EA);
690690

691691
ea[0].grfAccessPermissions = GENERIC_READ | GENERIC_WRITE;
692692
ea[0].grfAccessMode = SET_ACCESS;

contrib/coccinelle/array.cocci

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,23 @@ expression dst, src, n;
101101
-ALLOC_ARRAY(dst, n);
102102
-COPY_ARRAY(dst, src, n);
103103
+DUP_ARRAY(dst, src, n);
104+
105+
@@
106+
type T;
107+
T *ptr;
108+
expression n;
109+
@@
110+
- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
111+
- \| sizeof(*ptr)
112+
- \) )
113+
+ MEMZERO_ARRAY(ptr, n)
114+
115+
@@
116+
type T;
117+
T[] ptr;
118+
expression n;
119+
@@
120+
- memset(ptr, \( 0x0 \| 0 \), n * \( sizeof(T)
121+
- \| sizeof(*ptr)
122+
- \) )
123+
+ MEMZERO_ARRAY(ptr, n)

contrib/coccinelle/meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ foreach header : headers_to_check
5050
coccinelle_headers += meson.project_source_root() / header
5151
endforeach
5252

53+
coccinelle_includes = []
54+
foreach path : ['compat', 'ewah', 'refs', 'sha256', 'trace2', 'win32', 'xdiff']
55+
coccinelle_includes += ['-I', meson.project_source_root() / path]
56+
endforeach
57+
5358
patches = [ ]
5459
foreach source : coccinelle_sources
5560
patches += custom_target(
@@ -58,6 +63,7 @@ foreach source : coccinelle_sources
5863
'--all-includes',
5964
'--sp-file', concatenated_rules,
6065
'--patch', meson.project_source_root(),
66+
coccinelle_includes,
6167
'@INPUT@',
6268
],
6369
input: meson.project_source_root() / source,

diff-delta.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
171171
mem = hash + hsize;
172172
entry = mem;
173173

174-
memset(hash, 0, hsize * sizeof(*hash));
174+
MEMZERO_ARRAY(hash, hsize);
175175

176176
/* allocate an array to count hash entries */
177177
hash_count = calloc(hsize, sizeof(*hash_count));

ewah/bitmap.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ static void bitmap_grow(struct bitmap *self, size_t word_alloc)
4646
{
4747
size_t old_size = self->word_alloc;
4848
ALLOC_GROW(self->words, word_alloc, self->word_alloc);
49-
memset(self->words + old_size, 0x0,
50-
(self->word_alloc - old_size) * sizeof(eword_t));
49+
MEMZERO_ARRAY(self->words + old_size, (self->word_alloc - old_size));
5150
}
5251

5352
void bitmap_set(struct bitmap *self, size_t pos)
@@ -192,8 +191,8 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
192191
if (self->word_alloc < other_final) {
193192
self->word_alloc = other_final;
194193
REALLOC_ARRAY(self->words, self->word_alloc);
195-
memset(self->words + original_size, 0x0,
196-
(self->word_alloc - original_size) * sizeof(eword_t));
194+
MEMZERO_ARRAY(self->words + original_size,
195+
(self->word_alloc - original_size));
197196
}
198197

199198
ewah_iterator_init(&it, other);

git-compat-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ static inline uint64_t u64_add(uint64_t a, uint64_t b)
726726
#define ALLOC_ARRAY(x, alloc) (x) = xmalloc(st_mult(sizeof(*(x)), (alloc)))
727727
#define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
728728
#define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
729+
#define MEMZERO_ARRAY(x, alloc) memset((x), 0x0, st_mult(sizeof(*(x)), (alloc)))
729730

730731
#define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
731732
BARF_UNLESS_COPYABLE((dst), (src)))

hashmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void hashmap_partial_clear_(struct hashmap *map, ssize_t entry_offset)
194194
return;
195195
if (entry_offset >= 0) /* called by hashmap_clear_entries */
196196
free_individual_entries(map, entry_offset);
197-
memset(map->table, 0, map->tablesize * sizeof(struct hashmap_entry *));
197+
MEMZERO_ARRAY(map->table, map->tablesize);
198198
map->shrink_at = 0;
199199
map->private_size = 0;
200200
}

pack-revindex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
7575
for (bits = 0; max >> bits; bits += DIGIT_SIZE) {
7676
unsigned i;
7777

78-
memset(pos, 0, BUCKETS * sizeof(*pos));
78+
MEMZERO_ARRAY(pos, BUCKETS);
7979

8080
/*
8181
* We want pos[i] to store the index of the last element that

0 commit comments

Comments
 (0)