From 9cc0bb57b7e16f7602551037b0ce5bd13f0b9002 Mon Sep 17 00:00:00 2001 From: David Zero Date: Tue, 12 Mar 2024 22:45:15 -0400 Subject: [PATCH] Add and apply new clang-tidy config Also adds a CI step for clang-tidy --- .clang-tidy | 10 ++++++++++ .github/workflows/build_ship.yml | 17 ++++++++++++++++- .gitignore | 1 + CMakeLists.txt | 3 +++ README.md | 9 +++++++++ src/decode.c | 4 ++-- src/log.c | 2 +- src/tileset.c | 6 ++++-- 8 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..9d7fc09 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,10 @@ +--- +#-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling: Poorly supported Microsoft shit, not implemented by GCC afaict +#-bugprone-easily-swappable-parameters: you're not my real dad, you can't tell me how to order my parameters +Checks: > + bugprone-*, + concurrency-*, + performance-*, + -bugprone-easily-swappable-parameters, + -bugprone-assignment-in-if-condition, + -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling diff --git a/.github/workflows/build_ship.yml b/.github/workflows/build_ship.yml index b891cd5..0d2a923 100644 --- a/.github/workflows/build_ship.yml +++ b/.github/workflows/build_ship.yml @@ -17,11 +17,26 @@ jobs: run: sudo apt-get update && sudo apt-get install -y clang-format - name: Run Formatting - run: clang-format -i include/* src/*.c src/*.h + run: clang-format -i include/* src/*.h src/*.c - name: Check Formatting run: git diff --exit-code + clang-tidy: + runs-on: ubuntu-22.04 + steps: + - name : Checkout Source + uses: actions/checkout@v4 + + - name: Install Dependencies + run: sudo apt-get update && sudo apt-get install -y clang-tidy libjansson-dev zlib1g-dev libzstd-dev + + - name: Configure CMake + run: cmake -DCMAKE_BUILD_TYPE=Debug -DLIBTMJ_ZSTD=ON -DLIBTMJ_ZLIB=ON . + + - name: Tidy + run: clang-tidy include/* src/*.h src/*.c + build-linux: name: build-${{ matrix.os }}-${{ matrix.cc }} runs-on: ${{ matrix.os }} diff --git a/.gitignore b/.gitignore index bd9a5c8..6692769 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.swp bin/ example/ +compile_commands.json todo.txt # Doxygen diff --git a/CMakeLists.txt b/CMakeLists.txt index b207ff0..7ed5d90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,9 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY lib) +# Export compile commands +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + # Set options option(LIBTMJ_TEST "Enable unit tests" OFF) option(LIBTMJ_DOCS "Enable compiling documentation" OFF) diff --git a/README.md b/README.md index b1d9569..ac7230c 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,15 @@ int main(){ } ``` +## Notes + +JSON only defines a single number type, meant to accomodate both floating point +values and integers of arbitrary length. This library truncates all input +integers to the size of `int` on your platform, and all input floating point +numbers to the size of `double` on your platform. My estimation is that this is +acceptable for a Tiled map. If you find a case in which this breaks your map, +please let me know. + ## License BSD 2-Clause License diff --git a/src/decode.c b/src/decode.c index bb9e5fd..977e3ad 100644 --- a/src/decode.c +++ b/src/decode.c @@ -87,7 +87,7 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco stream.avail_in = data_size; stream.avail_out = INFLATE_BLOCK_SIZE; - stream.next_in = data; + stream.next_in = data; // NOLINT(clang-diagnostic-incompatible-pointer-types-discards-qualifiers) stream.next_out = out; // 15 + 32 for zlib and gzip decoding with automatic header detection, according to the manual @@ -213,7 +213,7 @@ uint8_t* tmj_zlib_decompress(const uint8_t* data, size_t data_size, size_t* deco // clang-format off const char b64_encode_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; -const char b64_decode_table[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, +const unsigned char b64_decode_table[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, diff --git a/src/log.c b/src/log.c index 6bc6751..9cb7e43 100644 --- a/src/log.c +++ b/src/log.c @@ -35,7 +35,7 @@ void logmsg(tmj_log_priority priority, char* msg, ...) { va_start(args, msg); - vsnprintf(logmsg_buf, LOGMSG_BUFSIZE, msg, args); + vsnprintf(logmsg_buf, LOGMSG_BUFSIZE, msg, args); // NOLINT(clang-analyzer-valist.Uninitialized) va_end(args); diff --git a/src/tileset.c b/src/tileset.c index d2e1f0a..7928022 100644 --- a/src/tileset.c +++ b/src/tileset.c @@ -254,7 +254,7 @@ int unpack_tileset(json_t* tileset, Tileset* ret) { if (ret->tiles == NULL) { logmsg(TMJ_LOG_ERR, "Unable to unpack tileset[%s]->tiles, the system is out of memory", ret->name); - goto fail_tiles; + goto fail_terrains; } size_t idx = 0; @@ -499,7 +499,7 @@ int unpack_tileset(json_t* tileset, Tileset* ret) { goto fail_tiles; } - ret->tiles[idx].terrain[idx2] = json_integer_value(terrain_idx); + ret->tiles[idx].terrain[idx2] = (int)json_integer_value(terrain_idx); } } } @@ -510,10 +510,12 @@ int unpack_tileset(json_t* tileset, Tileset* ret) { fail_tiles: for (size_t i = 0; i < ret->tile_count; i++) { free(ret->tiles[i].animation); + if (ret->tiles[i].objectgroup != NULL) { free(ret->tiles[i].objectgroup->properties); free_objects(ret->tiles[i].objectgroup->objects, ret->tiles[i].objectgroup->object_count); } + free(ret->tiles[i].objectgroup); free(ret->tiles[i].properties); }