From 0e925e103529f1a76ba84f8f68e8a2cc4510393e Mon Sep 17 00:00:00 2001 From: mikkelfj Date: Tue, 24 Oct 2023 16:19:34 +0200 Subject: [PATCH] Add clang debug sanitizer flag and fix related warnings --- CHANGELOG.md | 2 +- CMakeLists.txt | 4 ++++ src/compiler/parser.c | 1 - src/runtime/builder.c | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec7bece8..c9acb87e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ - Fix regression where empty namespace in schema does not reset root scope correctly in parser (#265). - Fix lexer checks that breaks with UTF-8, notably UTF-8 schema comments (#267). -- Fix UB in memcpy(p, 0, 0) by initializing scope prefix (mostly to silence sanitizers). +- Add sanitizer flag for clang debug and related warnings (input from several PRs incl. #237) ## [0.6.1] diff --git a/CMakeLists.txt b/CMakeLists.txt index 492ec8bc..ace0368e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -188,6 +188,10 @@ if (CMAKE_C_COMPILER_ID MATCHES "Clang" AND NOT "${CMAKE_CXX_SIMULATE_ID}" STREQ if (FLATCC_IGNORE_CONST_COND) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-tautological-constant-out-of-range-compare") endif() + if (CMAKE_BUILD_TYPE MATCHES Debug) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=undefined") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") + endif() # Suppress warning relaxed in clang-6, see https://reviews.llvm.org/D28148 if (CMAKE_C_COMPILER_VERSION VERSION_LESS 6) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-missing-field-initializers") diff --git a/src/compiler/parser.c b/src/compiler/parser.c index 88c94613..006bb86f 100644 --- a/src/compiler/parser.c +++ b/src/compiler/parser.c @@ -1270,7 +1270,6 @@ static void push_token(fb_parser_t *P, long id, const char *first, const char *l size_t offset; fb_token_t *t; - P->te = P->ts + P->tcapacity; if (P->token == P->te) { offset = (size_t)(P->token - P->ts); P->tcapacity = P->tcapacity ? 2 * P->tcapacity : 1024; diff --git a/src/runtime/builder.c b/src/runtime/builder.c index b62c2b66..1e5f8164 100644 --- a/src/runtime/builder.c +++ b/src/runtime/builder.c @@ -177,7 +177,7 @@ int flatcc_builder_default_alloc(void *alloc_context, iovec_t *b, size_t request return 0; } -#define T_ptr(base, pos) ((void *)((uint8_t *)(base) + (uoffset_t)(pos))) +#define T_ptr(base, pos) ((void *)((size_t)(base) + (size_t)(pos))) #define ds_ptr(pos) (T_ptr(B->buffers[flatcc_builder_alloc_ds].iov_base, (pos))) #define vs_ptr(pos) (T_ptr(B->buffers[flatcc_builder_alloc_vs].iov_base, (pos))) #define pl_ptr(pos) (T_ptr(B->buffers[flatcc_builder_alloc_pl].iov_base, (pos)))