Skip to content

Commit 9f5b776

Browse files
authored
Warnings (#66)
* first cut at reducing warnings binaryfusefilter.h only issues addressed 1. changed some return value and parameter types of (static) functions -- PLEASE CHECK THIS IN REVIEW 2. sprinkled 'U' into bitwise operations to silence warnings 3. casting to avoid "standard integer promotion rules" which resulted in signedness warnings 4. explicitly reducing results to the target type rather than letting it happen implicitly tests still passing * first cut at reducing warnings binaryfusefilter.h only issues addressed 1. changed some return value and parameter types of (static) functions -- PLEASE CHECK THIS IN REVIEW 2. sprinkled 'U' into bitwise operations to silence warnings 3. casting to avoid "standard integer promotion rules" which resulted in signedness warnings 4. explicitly reducing results to the target type rather than letting it happen implicitly 5. when and `if` statements ends in break or return, then a following `else if` can be just a new `if` tests still passing * starting work on xofilter.h * binclude/binaryfusefilter.h apparently clean for first time * formatting * first cut on xofilter.h mostly casting size_t down to uint32_t - maybe some internal struct types should have been size_t? also some integer promotion casts * round2 on xorfilter.h mostly casting blocklengt to uint32_t to fit into keyindex.index should keyindex.index be a size_t? * bench.c and unit.c very repetitive casting of mainly sizes and doubles. * all silent now on a clean compile with -Wconversion and -Wsign-conversion so putting these in the Makefile, so during "private" development with the Makefile new warnings will be noticed straight away but not in CMakeLists.txt, because as this is a header-only INTERFACE library, it would force these warning levels on the users. * another sweep from including c++ project turned up these additional 'U' tweaks * mistaken cast which broke test * factoring out the report functionality all sections were indentical except for the call to *contain() and *size_in_bytes some void* and function pointer juggling allowed to make this generic report code reduced by 2/3rds * iron out slight inconsistencies between tests * abstracting away the rest of the test logic for all but the special "failure rate test" the large function dispatch table is a litle annoying, but can be removed as well...TBC tests all pass * fixing a memory leak caught by sanitizer just a missing free() * _duplicates test cases can be convered by the same code remove initialization. it's not needed and compiler now happy * removing the need for large array of boiler plate function wrappers instead of having a wrapper function per action per filter type, we can cast the functions as a generic function pointer on the into the generic test runner, and then cast them as a compatible function pointer type inside the test runner. The generic `filter*` parameter cannot be `void*` and must be a dummy struct because: § 6.2.5..28: "All pointers to structure types shall have the same representation and alignment requirements as each other" (the same is not true for `void*` which may have different representation. This simple change results in a large code reduction and removes the unsightly and hard to remove array of boiler plate function wrappers. * upgrade CI use ubuntu 24.04 run a matrix with both gcc and clang compile tests/unit target with full warnings use -Werror so any new warnings are kept out compile the test with sanitzers to catch runtime UBSAN/ASAN issues execute ctest run with env options to ensure UBSAN/ASAN warning result in CI failure * change options only * correct compile and link options for test this CI build should now fail with UBSAN warning on clang (but would pass on gcc, and would pass on ubuntu 22.04 clang) * syntax * remove whitespce after \ * use sanitizers only on *nix not on mingw as not supported * fix the UBSAN warning this was the motivaing origin of all the CI changes resolve the UBSAN warning due to recent LLVM changes in clang-17 UBSAN, which ap0plies much more strict interpretation of casting of function pointers where one of the parameters is a void* and therefore not "an exact match" for the function ultimately called. this is not a problem in practice at runtime as these pointers are binary compatible, but best to avoid the interpretation of the standard by clang UBSAN - as major projects have also had to do The warning is only generated at runtime with sanatizers from recent clang-17+ compiled in. Hence the fairly wide reaching changes to CI. Solution to the UBSAN warning is macro generated "thunks", ie simple function wrappers, which allow the compiler to be happy about the cast. Much more terse than spelling out the wrapper functions, but with all the benefits of conformance and clangd/IDE help with completion and type checks. * really skip saninitizers unless not mingw * fixing warnings on MINGW32 immediately caught by the new -Werror policy, MINGW32 CI build was failing because: - size_t is 32bit on MINGW32 - filter->blockLength is uint64_t so any calculation involving filter->blockLength and being stored in a size_t was causing an implicit cast which this commit makes explicit review whether to just make filter->blockLength a size_t so it auto adjusts to the correct size.
1 parent 872a8bb commit 9f5b776

File tree

5 files changed

+157
-112
lines changed

5 files changed

+157
-112
lines changed

.github/workflows/ubuntu.yml

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
1-
name: Ubuntu 22.04 CI (GCC 11)
1+
name: Ubuntu 24.04 (gcc-13, clang-18)
22

33
on: [push, pull_request]
44

55
jobs:
66
ubuntu-build:
7-
runs-on: ubuntu-22.04
7+
runs-on: ubuntu-24.04
8+
strategy:
9+
matrix:
10+
compiler: [gcc, clang]
811
steps:
9-
- uses: actions/checkout@v3
10-
- name: Use cmake
12+
- name: checkout code
13+
uses: actions/checkout@v4
14+
- name: build with cmake
1115
run: |
16+
if [ "${{ matrix.compiler }}" == "gcc" ]; then
17+
export CC=gcc
18+
export CXX=g++
19+
elif [ "${{ matrix.compiler }}" == "clang" ]; then
20+
export CC=clang
21+
export CXX=clang++
22+
fi
1223
mkdir build &&
1324
cd build &&
1425
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=destination &&
1526
cmake --build . &&
16-
ctest --output-on-failure &&
27+
# force failure if sanitizers report any warnings
28+
env \
29+
ASAN_OPTIONS='halt_on_error=1:abort_on_error=1:print_summary=1' \
30+
UBSAN_OPTIONS='halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1' \
31+
ctest --output-on-failure &&
1732
cmake --install . &&
1833
cd ../tests/installation_tests/find &&
1934
mkdir build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../../../build/destination .. && cmake --build .

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
all: unit bench
22

33
unit : tests/unit.c include/xorfilter.h include/binaryfusefilter.h
4-
cc -std=c99 -O3 -o unit tests/unit.c -lm -Iinclude -Wall -Wextra -Wshadow -Wcast-qual -Wconversion -Wsign-conversion
5-
4+
${CC} -std=c99 -g -O2 -fsanitize=address,leak,undefined -o unit tests/unit.c -lm -Iinclude -Wall -Wextra -Wshadow -Wcast-qual -Wconversion -Wsign-conversion -Werror
65

76
ab : tests/a.c tests/b.c
8-
cc -std=c99 -o c tests/a.c tests/b.c -lm -Iinclude -Wall -Wextra -Wshadow -Wcast-qual -Wconversion -Wsign-conversion
7+
${CC} -std=c99 -o c tests/a.c tests/b.c -lm -Iinclude -Wall -Wextra -Wshadow -Wcast-qual -Wconversion -Wsign-conversion
98

109
bench : benchmarks/bench.c include/xorfilter.h include/binaryfusefilter.h
11-
cc -std=c99 -O3 -o bench benchmarks/bench.c -lm -Iinclude -Wall -Wextra -Wshadow -Wcast-qual -Wconversion -Wsign-conversion
10+
${CC} -std=c99 -O3 -o bench benchmarks/bench.c -lm -Iinclude -Wall -Wextra -Wshadow -Wcast-qual -Wconversion -Wsign-conversion
1211

1312
test: unit ab
13+
ASAN_OPTIONS='halt_on_error=1:abort_on_error=1:print_summary=1' \
14+
UBSAN_OPTIONS='halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1' \
1415
./unit
1516

1617
clean:

include/xorfilter.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ static inline bool xor16_allocate(uint32_t size, xor16_t *filter) {
157157

158158
// report memory usage
159159
static inline size_t xor8_size_in_bytes(const xor8_t *filter) {
160-
return 3 * filter->blockLength * sizeof(uint8_t) + sizeof(xor8_t);
160+
return 3 * (size_t)(filter->blockLength) * sizeof(uint8_t) + sizeof(xor8_t);
161161
}
162162

163163
// report memory usage
164164
static inline size_t xor16_size_in_bytes(const xor16_t *filter) {
165-
return 3 * filter->blockLength * sizeof(uint16_t) + sizeof(xor16_t);
165+
return 3 * (size_t)(filter->blockLength) * sizeof(uint16_t) + sizeof(xor16_t);
166166
}
167167

168168
// release memory
@@ -449,9 +449,9 @@ static inline bool xor8_buffered_populate(uint64_t *keys, uint32_t size, xor8_t
449449
if(size == 0) { return false; }
450450
uint64_t rng_counter = 1;
451451
filter->seed = xor_rng_splitmix64(&rng_counter);
452-
size_t arrayLength = filter->blockLength * 3; // size of the backing array
452+
size_t arrayLength = (size_t)(filter->blockLength) * 3; // size of the backing array
453453
xor_setbuffer_t buffer0, buffer1, buffer2;
454-
size_t blockLength = filter->blockLength;
454+
size_t blockLength = (size_t)(filter->blockLength);
455455
bool ok0 = xor_init_buffer(&buffer0, blockLength);
456456
bool ok1 = xor_init_buffer(&buffer1, blockLength);
457457
bool ok2 = xor_init_buffer(&buffer2, blockLength);
@@ -660,8 +660,8 @@ static inline bool xor8_populate(uint64_t *keys, uint32_t size, xor8_t *filter)
660660
if(size == 0) { return false; }
661661
uint64_t rng_counter = 1;
662662
filter->seed = xor_rng_splitmix64(&rng_counter);
663-
size_t arrayLength = filter->blockLength * 3; // size of the backing array
664-
size_t blockLength = filter->blockLength;
663+
size_t arrayLength = (size_t)(filter->blockLength) * 3; // size of the backing array
664+
size_t blockLength = (size_t)(filter->blockLength);
665665

666666
xor_xorset_t *sets =
667667
(xor_xorset_t *)malloc(arrayLength * sizeof(xor_xorset_t));
@@ -867,9 +867,9 @@ static inline bool xor16_buffered_populate(uint64_t *keys, uint32_t size, xor16_
867867
if(size == 0) { return false; }
868868
uint64_t rng_counter = 1;
869869
filter->seed = xor_rng_splitmix64(&rng_counter);
870-
size_t arrayLength = filter->blockLength * 3; // size of the backing array
870+
size_t arrayLength = (size_t)(filter->blockLength) * 3; // size of the backing array
871871
xor_setbuffer_t buffer0, buffer1, buffer2;
872-
size_t blockLength = filter->blockLength;
872+
size_t blockLength = (size_t)(filter->blockLength);
873873
bool ok0 = xor_init_buffer(&buffer0, blockLength);
874874
bool ok1 = xor_init_buffer(&buffer1, blockLength);
875875
bool ok2 = xor_init_buffer(&buffer2, blockLength);
@@ -1081,8 +1081,8 @@ static inline bool xor16_populate(uint64_t *keys, uint32_t size, xor16_t *filter
10811081
if(size == 0) { return false; }
10821082
uint64_t rng_counter = 1;
10831083
filter->seed = xor_rng_splitmix64(&rng_counter);
1084-
size_t arrayLength = filter->blockLength * 3; // size of the backing array
1085-
size_t blockLength = filter->blockLength;
1084+
size_t arrayLength = (size_t)(filter->blockLength) * 3; // size of the backing array
1085+
size_t blockLength = (size_t)(filter->blockLength);
10861086

10871087
xor_xorset_t *sets =
10881088
(xor_xorset_t *)malloc(arrayLength * sizeof(xor_xorset_t));
@@ -1282,12 +1282,12 @@ static inline bool xor16_populate(uint64_t *keys, uint32_t size, xor16_t *filter
12821282

12831283
static inline size_t xor16_serialization_bytes(xor16_t *filter) {
12841284
return sizeof(filter->seed) + sizeof(filter->blockLength) +
1285-
sizeof(uint16_t) * 3 * filter->blockLength;
1285+
sizeof(uint16_t) * 3 * (size_t)(filter->blockLength);
12861286
}
12871287

12881288
static inline size_t xor8_serialization_bytes(const xor8_t *filter) {
12891289
return sizeof(filter->seed) + sizeof(filter->blockLength) +
1290-
sizeof(uint8_t) * 3 * filter->blockLength;
1290+
sizeof(uint8_t) * 3 * (size_t)(filter->blockLength);
12911291
}
12921292

12931293
// serialize a filter to a buffer, the buffer should have a capacity of at least
@@ -1298,7 +1298,7 @@ static inline void xor16_serialize(const xor16_t *filter, char *buffer) {
12981298
buffer += sizeof(filter->seed);
12991299
memcpy(buffer, &filter->blockLength, sizeof(filter->blockLength));
13001300
buffer += sizeof(filter->blockLength);
1301-
memcpy(buffer, filter->fingerprints, filter->blockLength * 3 * sizeof(uint16_t));
1301+
memcpy(buffer, filter->fingerprints, (size_t)(filter->blockLength) * 3 * sizeof(uint16_t));
13021302
}
13031303

13041304
// serialize a filter to a buffer, the buffer should have a capacity of at least
@@ -1309,7 +1309,7 @@ static inline void xor8_serialize(const xor8_t *filter, char *buffer) {
13091309
buffer += sizeof(filter->seed);
13101310
memcpy(buffer, &filter->blockLength, sizeof(filter->blockLength));
13111311
buffer += sizeof(filter->blockLength);
1312-
memcpy(buffer, filter->fingerprints, filter->blockLength * 3 * sizeof(uint8_t));
1312+
memcpy(buffer, filter->fingerprints, (size_t)(filter->blockLength) * 3 * sizeof(uint8_t));
13131313
}
13141314

13151315
// deserialize a filter from a buffer, returns true on success, false on failure.
@@ -1322,11 +1322,11 @@ static inline bool xor16_deserialize(xor16_t * filter, const char *buffer) {
13221322
buffer += sizeof(filter->seed);
13231323
memcpy(&filter->blockLength, buffer, sizeof(filter->blockLength));
13241324
buffer += sizeof(filter->blockLength);
1325-
filter->fingerprints = (uint16_t*)malloc(filter->blockLength * 3 * sizeof(uint16_t));
1325+
filter->fingerprints = (uint16_t*)malloc((size_t)(filter->blockLength) * 3 * sizeof(uint16_t));
13261326
if(filter->fingerprints == NULL) {
13271327
return false;
13281328
}
1329-
memcpy(filter->fingerprints, buffer, filter->blockLength * 3 * sizeof(uint16_t));
1329+
memcpy(filter->fingerprints, buffer, (size_t)(filter->blockLength) * 3 * sizeof(uint16_t));
13301330
return true;
13311331
}
13321332

@@ -1341,11 +1341,11 @@ static inline bool xor8_deserialize(xor8_t * filter, const char *buffer) {
13411341
buffer += sizeof(filter->seed);
13421342
memcpy(&filter->blockLength, buffer, sizeof(filter->blockLength));
13431343
buffer += sizeof(filter->blockLength);
1344-
filter->fingerprints = (uint8_t*)malloc(filter->blockLength * 3 * sizeof(uint8_t));
1344+
filter->fingerprints = (uint8_t*)malloc((size_t)(filter->blockLength) * 3 * sizeof(uint8_t));
13451345
if(filter->fingerprints == NULL) {
13461346
return false;
13471347
}
1348-
memcpy(filter->fingerprints, buffer, filter->blockLength * 3 * sizeof(uint8_t));
1348+
memcpy(filter->fingerprints, buffer, (size_t)(filter->blockLength) * 3 * sizeof(uint8_t));
13491349
return true;
13501350
}
13511351

tests/CMakeLists.txt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
add_executable(unit unit.c)
22
add_test(unit unit)
3-
target_link_libraries(unit PUBLIC xor_singleheader)
3+
target_link_libraries(unit PRIVATE xor_singleheader)
4+
5+
6+
# full warnings with sanitizers for tests. Include debug symbols and
7+
# only -O2 to maintain some debugability. -Werror to
8+
# prevent new warning creeping in Matches Makefile
9+
if (MSVC)
10+
# limited support for MSVC, this is not tested
11+
list(APPEND TEST_COMPILE_OPTIONS /W4 /fsanitize=address)
12+
else() # *nix
13+
list(APPEND TEST_COMPILE_OPTIONS -g -O2
14+
-Wall -Wextra -Wshadow -Wcast-qual -Wconversion -Wsign-conversion -Werror)
15+
16+
if (NOT MINGW) # sanitizers are not supported under mingw
17+
list(APPEND TEST_COMPILE_OPTIONS -fsanitize=address,undefined,leak)
18+
# sanitsizers need to be specified at link time as well
19+
target_link_options(unit PRIVATE -fsanitize=address,leak,undefined)
20+
endif()
21+
endif()
22+
23+
target_compile_options(unit PRIVATE ${TEST_COMPILE_OPTIONS})
24+

0 commit comments

Comments
 (0)