Skip to content

fix(dx8wrapper): Prevent undefined behavior when converting Vector4 colors - #3655

Open
CryoTheRenegade wants to merge 2 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:refactor/convert-color-vector4
Open

CryoTheRenegade wants to merge 2 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:refactor/convert-color-vector4

Conversation

@CryoTheRenegade

@CryoTheRenegade CryoTheRenegade commented Nov 5, 2026 •

Copy link
Copy Markdown

Follow-up to #3651, from xezon's "Are there more of these?" on #3580. DX8Wrapper::Convert_Color(const Vector4&) and Convert_Color_Clamp pass the color to the Vector3 overload with reinterpret_cast<const Vector3&>(color). A Vector4 is not a Vector3, so reading it through that reference is undefined behavior, the same problem as #3521.

Convert_Color(const Vector4&) now builds a Vector3 from the first three components, and Convert_Color_Clamp calls it after clamping:

 WWINLINE unsigned int DX8Wrapper::Convert_Color(const Vector4& color)
 {
-	return Convert_Color(reinterpret_cast<const Vector3&>(color),color[3]);
+	return Convert_Color(Vector3(color.X,color.Y,color.Z),color.W);
 }
 ...
-	return Convert_Color(reinterpret_cast<const Vector3&>(clamped_color),clamped_color[3]);
+	return Convert_Color(clamped_color);

Evidence

  • Disassembly: I compared the objects of five callers (dynamesh.cpp, pointgr.cpp, streakRender.cpp, W3DScorch.cpp and W3DTerrainParticles.cpp) against a build of main, for both games. In every changed function, the float arithmetic and the call targets are the same as on main.
    • VS 2026 (ClangCL) Release: DynamicMeshClass::Change_Vertex_Color, PointGroupClass::Render, PointGroupClass::RenderVolumeParticle and StreakRendererClass::RenderStreak changed only in register allocation and stack offsets.
    • VC6 Release: W3DScorch::updateScorches, the W3DTerrainParticles constructor and W3DTerrainParticles::render now copy the three floats into the Vector3 temporary (three fld/fstp pairs) before the inline assembly converts the color. The other three files are unchanged.

AI Disclosure

Claude Opus 5.5 was used to aid in implementation and to compare the disassembly.

CryoTheRenegade and others added 2 commits October 26, 2026 15:36
…ctor3 cast

DX8Wrapper::Convert_Color(const Vector4&) and Convert_Color_Clamp read
the RGB part of a Vector4 through a Vector3 reference.

On VC6, the inline assembly that converts the color now lives in
Convert_Color_RGB, which takes a pointer to the red, green and blue
floats. Both Convert_Color overloads pass &color.X, so the Vector4
overload doesn't copy the color into a Vector3 temporary. Other
compilers build a Vector3 from X, Y and Z.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… inline assembly helper

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Both DX8Wrapper Vector4 color-conversion helpers now use named X, Y, and Z components for color and W for alpha. The clamped helper still clamps a copy before conversion.

Changes

Vector4 color conversion

Layer / File(s) Summary
Explicit component conversion
Core/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h
Both helpers construct the color from X, Y, and Z and pass W as alpha. The clamped helper still clamps a copy before conversion.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Change: Bug fix

Possibly related PRs

Suggested reviewers: xezon

Merge Risk: Minimal · up to 273f9

No actionable merge-blocking risk is identified in this localized color-conversion change. It is mergeable subject to normal build checks.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: it fixes undefined behavior in DX8Wrapper color conversion from Vector4. The change replaces the reinterpret_cast of a Vector4 as a Vector3 with explicit component con…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description explains the change in detail: DX8Wrapper::Convert_Color(const Vector4&) and Convert_Color_Clamp previously read a Vector4 through a reinterpret_cast to Vector3&, which is undefined be…
Full details: Title check

Explanation

The title names the main change: it fixes undefined behavior in DX8Wrapper color conversion from Vector4. The change replaces the reinterpret_cast of a Vector4 as a Vector3 with explicit component construction, which matches the title. The title uses a conventional prefix and runs 74 characters, which is within a reasonable length.

Full details: Description check

Explanation

The description explains the change in detail: DX8Wrapper::Convert_Color(const Vector4&) and Convert_Color_Clamp previously read a Vector4 through a reinterpret_cast to Vector3&, which is undefined behavior. It describes the replacement code, which builds a Vector3 from X, Y and Z and passes W as alpha, and the clamp path now calling the Vector4 overload. It also lists disassembly evidence for five callers across VS 2026 and VC6 builds. This matches the raw summary and the PR objectives, so the description is clearly related to the changeset.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b02a056b-7b15-45f1-a14b-88ebd8afb09d
Commits

Reviewing files that changed from the base of the PR and between 116fbcf and 273f99f.

Files selected for processing: 1

  • Core/Libraries/Source/WWVegas/WW3D2/dx8wrapper.h

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-07T20:40:03.479015Z 273f99f Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

Removes unsafe casts from the Vector4 color conversions in DX8Wrapper.

  • Vector4 colors now convert through their named color components.

Reviews (1) · Last reviewed commit: "refactor(ww3d2): Copy the Vector4 color ..." · Reviewed by Greptile

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