Skip to content

Fix nontrivial-memcall warnings in cute_graphics_sdlgpu.cpp - #573

Open
pusewicz wants to merge 2 commits into
RandyGaul:masterfrom
pusewicz:fix-shader-internal-memset
Open

Fix nontrivial-memcall warnings in cute_graphics_sdlgpu.cpp#573
pusewicz wants to merge 2 commits into
RandyGaul:masterfrom
pusewicz:fix-shader-internal-memset

Conversation

@pusewicz

@pusewicz pusewicz commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Two different situations here, so two different treatments:

cf_sdlgpu_make_shader_from_bytecode / cf_sdlgpu_make_compute_shader_from_bytecode used CF_NEW (default-initialization) followed by a memset to zero the plain C-array/scalar members that default-init leaves uninitialized — verified empirically that CF_NEW's new(args) (Type) form really does leave those as garbage. The memset also stomped the Cute::Array members, which were already properly constructed (harmlessly, since their zero-state matches empty, but still unspecified behavior on a live non-trivial object). Switched to CF_PLACEMENT_NEW with an explicit T() (value-initialization), which zero-initializes every member — plain arrays included — in one well-defined step, so the memset goes away entirely rather than being merely silenced.

Verified with a temporary 0xCD-poison + CF_ASSERT pass over every member (removed once confirmed clean) that nothing is left uninitialized after the change.

cf_sdlgpu_shader_swap_contents / cf_sdlgpu_compute_shader_swap_contents do a deliberate raw byte-swap of two live objects for hot-reload (both types are pointer-only handles with no self-references, per the existing comment) — that's intentional, not a bug. Left the swap as-is and just cast the destination pointers to void*, per the compiler's own suggested fix.

Verified: framework + test suite builds warning-clean, and ./tests passes the same 313/333 as master (remaining 20 are the pre-existing headless GPU rendering failures from #572, unrelated to this change).

Two different situations here, treated differently:

- cf_sdlgpu_make_shader_from_bytecode / cf_sdlgpu_make_compute_shader_from_bytecode
  used CF_NEW (default-initialization) followed by a memset to zero
  the plain C-array/scalar members that default-init leaves
  uninitialized. That memset also stomped the Cute::Array members,
  which were already properly constructed. Switched to
  CF_PLACEMENT_NEW with an explicit T() (value-initialization),
  which zero-initializes every member in one well-defined step and
  removes the memset entirely. Verified with a temporary 0xCD
  poison-and-assert pass that no member was left uninitialized
  (removed after confirming clean).

- cf_sdlgpu_shader_swap_contents / cf_sdlgpu_compute_shader_swap_contents
  do a deliberate raw byte-swap of two live objects for hot-reload
  (both types are pointer-only handles with no self-references, per
  the existing comment). This is intentional, not a bug, so just
  cast the destination pointers to void* to silence the warning, as
  the compiler itself suggests.
Copilot AI lite review requested due to automatic review settings August 7, 2026 21:31
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR targets -Wnontrivial-memcall warnings in the SDL_gpu backend by removing memset on already-constructed shader internal objects and adjusting shader hot-reload swapping code paths.

Changes:

  • Replaced CF_NEW + CF_MEMSET with value-initialized placement-new allocations for CF_ShaderInternal and CF_ComputeShaderInternal.
  • Updated the shader/compute-shader “swap contents” hot-reload routines to cast destinations to void* for CF_MEMCPY calls.
Suppressed comments (1)

src/cute_graphics_sdlgpu.cpp:2651

  • cf_sdlgpu_compute_shader_swap_contents has the same issue as the graphics shader swap: CF_ComputeShaderInternal contains non-trivial Cute::Array members, so the memcpy swap is UB even if cast to void*. Also, cf_sdlgpu_destroy_compute_shader currently CF_FREEs the object without running its destructor, which leaks the reflection/sampler Cute::Array allocations. Use a move-based swap and explicitly run the destructor before freeing (mirrors cf_sdlgpu_destroy_shader_internal).
	uint8_t tmp[sizeof(CF_ComputeShaderInternal)];
	CF_MEMCPY(tmp, pa, sizeof(tmp));
	CF_MEMCPY((void*)pa, pb, sizeof(tmp));
	CF_MEMCPY((void*)pb, tmp, sizeof(tmp));

Comment thread src/cute_graphics_sdlgpu.cpp Outdated
Comment on lines +1322 to +1325
uint8_t tmp[sizeof(CF_ShaderInternal)];
CF_MEMCPY(tmp, pa, sizeof(tmp));
CF_MEMCPY(pa, pb, sizeof(tmp));
CF_MEMCPY(pb, tmp, sizeof(tmp));
CF_MEMCPY((void*)pa, pb, sizeof(tmp));
CF_MEMCPY((void*)pb, tmp, sizeof(tmp));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 7822c21 — swapped to Cute::swap (already exists in the codebase, does a proper move-based 3-step swap).

The raw memcpy-based 3-step swap was UB for a non-trivially-copyable
type even with the destination pointers cast to void* to silence the
compiler -- the cast only hides the warning, it doesn't make copying
a live object's bytes past its own constructor/destructor
well-defined.

Cute::swap already exists (a proper move-based 3-step swap) and is
in scope via the file's `using namespace Cute`, so swap the raw
memcpy dance for a single call to it. Verified with a standalone
struct mirroring CF_ShaderInternal's member mix (scalar, plain
C-array, Cute::Array) that all three kinds swap correctly.
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.

2 participants