diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c35fcf268..50e59f33d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -211,7 +211,7 @@ jobs: make -j CFLAGS="$CFLAGS" CC=$CC AR=$AR env: PLATFORM: ${{ matrix.platform }} - CFLAGS: -g -Werror -Wall -Wextra -Wshadow -Wpedantic -Werror=incompatible-pointer-types + CFLAGS: -g -Werror -Wall -Wextra -Wshadow -Wpedantic -Werror=incompatible-pointer-types -Werror=strict-aliasing -Wstrict-aliasing=2 - name: Build C library (CMake) if: "!matrix.cross && !matrix.vm" diff --git a/.github/workflows/wasm_exports.yml b/.github/workflows/wasm_exports.yml index 6c3d57e44..a4177c0dd 100644 --- a/.github/workflows/wasm_exports.yml +++ b/.github/workflows/wasm_exports.yml @@ -31,7 +31,7 @@ jobs: - name: Build C library (make) run: make -j CFLAGS="$CFLAGS" env: - CFLAGS: -g -Werror -Wall -Wextra -Wshadow -Wpedantic -Werror=incompatible-pointer-types + CFLAGS: -g -Werror -Wall -Wextra -Wshadow -Wpedantic -Werror=incompatible-pointer-types -Werror=strict-aliasing -Wstrict-aliasing=2 - name: Build Wasm Library working-directory: lib/binding_web diff --git a/CMakeLists.txt b/CMakeLists.txt index f11895c0a..9781db695 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,7 +33,8 @@ if(MSVC) else() target_compile_options(tree-sitter PRIVATE -Wall -Wextra -Wshadow -Wpedantic - -Werror=incompatible-pointer-types) + -Werror=incompatible-pointer-types + -Werror=strict-aliasing -Wstrict-aliasing=2) endif() if(TREE_SITTER_FEATURE_WASM) diff --git a/Makefile b/Makefile index 13f33e13a..9a19fbb14 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ OBJ := $(SRC:.c=.o) # define default flags, and override to append mandatory flags ARFLAGS := rcs -CFLAGS ?= -O3 -Wall -Wextra -Wshadow -Wpedantic -Werror=incompatible-pointer-types +CFLAGS ?= -O3 -Wall -Wextra -Wshadow -Wpedantic -Werror=incompatible-pointer-types -Werror=strict-aliasing -Wstrict-aliasing=2 override CFLAGS += -std=c11 -fPIC -fvisibility=hidden override CFLAGS += -D_POSIX_C_SOURCE=200112L -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_DARWIN_C_SOURCE override CFLAGS += -Ilib/src -Ilib/src/wasm -Ilib/include diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index 70ef5e39a..ed79ef72f 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -3,6 +3,11 @@ #include "./language.h" #include "./tree.h" +_Static_assert( + sizeof(TreeCursor) <= sizeof(TSTreeCursor), + "TreeCursor must fit within TSTreeCursor" +); + typedef struct { Subtree parent; const TSTree *tree; @@ -153,8 +158,10 @@ static inline bool ts_tree_cursor_child_iterator_previous( // TSTreeCursor - lifecycle TSTreeCursor ts_tree_cursor_new(TSNode node) { - TSTreeCursor self = {NULL, NULL, {0, 0, 0}}; - ts_tree_cursor_init((TreeCursor *)&self, node); + TreeCursor cursor = {0}; + ts_tree_cursor_init(&cursor, node); + TSTreeCursor self = {0}; + memcpy(&self, &cursor, sizeof(cursor)); return self; } @@ -697,12 +704,13 @@ const char *ts_tree_cursor_current_field_name(const TSTreeCursor *_self) { TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *_cursor) { const TreeCursor *cursor = (const TreeCursor *)_cursor; - TSTreeCursor res = {NULL, NULL, {0, 0}}; - TreeCursor *copy = (TreeCursor *)&res; - copy->tree = cursor->tree; - copy->root_alias_symbol = cursor->root_alias_symbol; - array_init(©->stack); - array_push_all(©->stack, &cursor->stack); + TreeCursor copy = {0}; + copy.tree = cursor->tree; + copy.root_alias_symbol = cursor->root_alias_symbol; + array_init(©.stack); + array_push_all(©.stack, &cursor->stack); + TSTreeCursor res = {0}; + memcpy(&res, ©, sizeof(copy)); return res; }