Skip to content

Make RapidJSON Stack::Bottom pointer cast explicit via void*#173

Merged
SergioRZMasson merged 1 commit into
microsoft:Release/1.9.5from
SergioRZMasson:sergioze/fix-rapidjson-string-type-conversion
Jul 14, 2026
Merged

Make RapidJSON Stack::Bottom pointer cast explicit via void*#173
SergioRZMasson merged 1 commit into
microsoft:Release/1.9.5from
SergioRZMasson:sergioze/fix-rapidjson-string-type-conversion

Conversation

@SergioRZMasson

@SergioRZMasson SergioRZMasson commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Background

Stack::Bottom<T>() 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.

Change

Following the same mechanism as the existing fix-null-allocator-deref.cmake
patch (#167), a small CMake string(REPLACE) patch is applied during the
RapidJSON ExternalProject_Add download step, routing the cast through void*:

reinterpret_cast<T*>(stack_)reinterpret_cast<T*>(static_cast<void*>(stack_))

This makes the "treat this buffer as raw storage" intent explicit. It is
address-identical and preserves the exact behavior of the generic memory stack,
covers both the const and non-const Bottom() overloads, and is idempotent.

Changes

  • new External/RapidJSON/patches/fix-string-type-conversion.cmake
  • edit External/RapidJSON/CMakeRapidJSONDownload.txt.in — chain the new
    patch after the existing one in PATCH_COMMAND

Validation

Applied the patch to RapidJSON at the pinned commit (232389d4) and compiled a
translation unit that includes rapidjson and exercises
Stack::Bottom<char>/<wchar_t> (const + non-const) plus Document parse +
Writer, with clang++ -std=c++14 -Wall -Wextra -Werror. Re-running the patch
is a no-op.

@SergioRZMasson
SergioRZMasson requested a review from bghgary July 9, 2026 19:32
Stack::Bottom<T>() 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<T*>(stack_)
  -> reinterpret_cast<T*>(static_cast<void*>(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<char>/<wchar_t> (const and non-const overloads) plus Document
parse + Writer, with -Wall -Wextra -Werror.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@SergioRZMasson
SergioRZMasson force-pushed the sergioze/fix-rapidjson-string-type-conversion branch from e757686 to 531ef16 Compare July 14, 2026 19:55
@SergioRZMasson
SergioRZMasson merged commit c8ccbd8 into microsoft:Release/1.9.5 Jul 14, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant