From 531ef160e2daed6d1d703647bbf7be73e435da47 Mon Sep 17 00:00:00 2001 From: Sergio Ricardo Zerbetto Masson Date: Thu, 9 Jul 2026 13:36:57 -0300 Subject: [PATCH] Make RapidJSON Stack::Bottom pointer cast explicit via void* Stack::Bottom() returns the internal raw byte buffer (char* stack_) reinterpret_cast directly to T*. When T is a wider element type (e.g. a wide-character type) this is a narrow-to-wide pointer conversion that some compilers and static analysis tools diagnose as a potentially misaligned/narrowing cast. Apply a small CMake patch during the RapidJSON ExternalProject_Add download step (matching the existing fix-null-allocator-deref.cmake mechanism) that routes the cast through void*: reinterpret_cast(stack_) -> reinterpret_cast(static_cast(stack_)) This makes the raw-storage intent explicit. It is address-identical and preserves the exact behavior of the generic memory stack, covering both the const and non-const Bottom() overloads, and is idempotent. Validated by applying the patch to RapidJSON at the pinned commit (232389d4) and compiling a translation unit that includes rapidjson and exercises Stack::Bottom/ (const and non-const overloads) plus Document parse + Writer, with -Wall -Wextra -Werror. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../RapidJSON/CMakeRapidJSONDownload.txt.in | 1 + .../patches/fix-string-type-conversion.cmake | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 External/RapidJSON/patches/fix-string-type-conversion.cmake diff --git a/External/RapidJSON/CMakeRapidJSONDownload.txt.in b/External/RapidJSON/CMakeRapidJSONDownload.txt.in index 9e3be43..f399d09 100644 --- a/External/RapidJSON/CMakeRapidJSONDownload.txt.in +++ b/External/RapidJSON/CMakeRapidJSONDownload.txt.in @@ -9,6 +9,7 @@ ExternalProject_Add(RapidJSON SOURCE_DIR "${CMAKE_BINARY_DIR}/RapidJSON-src" BINARY_DIR "${CMAKE_BINARY_DIR}/RapidJSON-build" PATCH_COMMAND "${CMAKE_COMMAND}" -DSOURCE_DIR=${CMAKE_BINARY_DIR}/RapidJSON-src -P "${CMAKE_CURRENT_SOURCE_DIR}/patches/fix-null-allocator-deref.cmake" + COMMAND "${CMAKE_COMMAND}" -DSOURCE_DIR=${CMAKE_BINARY_DIR}/RapidJSON-src -P "${CMAKE_CURRENT_SOURCE_DIR}/patches/fix-string-type-conversion.cmake" CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" diff --git a/External/RapidJSON/patches/fix-string-type-conversion.cmake b/External/RapidJSON/patches/fix-string-type-conversion.cmake new file mode 100644 index 0000000..8b02554 --- /dev/null +++ b/External/RapidJSON/patches/fix-string-type-conversion.cmake @@ -0,0 +1,47 @@ +# fix-string-type-conversion.cmake +# +# Makes the pointer cast in RapidJSON's generic memory Stack explicit. +# Stack::Bottom() returns the internal raw byte buffer (char* stack_) +# reinterpret_cast directly to T*. When T is a wider element type (e.g. a +# wide-character type) this is a narrow-to-wide pointer conversion that some +# compilers and static analysis tools diagnose as a potentially +# misaligned/narrowing cast. +# +# Routing the cast through void* makes the "treat this buffer as raw storage" +# intent explicit. It is address-identical and preserves the exact behavior of +# the generic memory stack, while avoiding the direct narrow-to-wide character +# pointer conversion diagnostic. +# +# Usage: cmake -DSOURCE_DIR= -P fix-string-type-conversion.cmake + +if(NOT DEFINED SOURCE_DIR) + message(FATAL_ERROR "SOURCE_DIR must be defined") +endif() + +set(STACK_FILE "${SOURCE_DIR}/include/rapidjson/internal/stack.h") + +if(NOT EXISTS "${STACK_FILE}") + message(FATAL_ERROR "stack.h not found at ${STACK_FILE}") +endif() + +file(READ "${STACK_FILE}" content) + +string(FIND "${content}" "reinterpret_cast(static_cast(stack_))" already_pos) +if(NOT already_pos EQUAL -1) + message(STATUS "Patch already applied in stack.h") + return() +endif() + +string(FIND "${content}" "reinterpret_cast(stack_)" match_pos) +if(match_pos EQUAL -1) + message(STATUS "Pattern not found in stack.h; nothing to patch") + return() +endif() + +string(REPLACE + "reinterpret_cast(stack_)" + "reinterpret_cast(static_cast(stack_))" + content "${content}") + +file(WRITE "${STACK_FILE}" "${content}") +message(STATUS "Patched stack.h: routed Stack::Bottom char* -> T* cast through void*")