Harden pet data loading against malformed pet.bmd entries#69
Conversation
The x64 crash fixes originally in sven-n#494 (camera waypoint count read and the pet speed memcpy size) already landed on main via the native-Linux port (sven-n#497), so only the defensive hardening remains here: - PetInfo::SetActions: reject count <= 0 or count > 100, and Destroy() before reallocating so a repeat call does not leak the previous buffers. - PetProcess::LoadData: skip entries whose _type/_count are out of range instead of allocating from corrupt values. Cherry-picked from sven-n#494 by NaraBalvan; the redundant crash-fix hunks were dropped because they conflict with the equivalent fixes already on main.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces defensive hardening measures to the pet data loading logic. By validating input ranges and ensuring proper memory cleanup during object updates, the changes prevent potential crashes and memory leaks caused by malformed pet.bmd files. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces safety improvements and bug fixes in w_PetProcess.cpp. It adds bounds checking with a maximum limit on pet actions, fixes a bug in SetActions where sizeof(int) was incorrectly used instead of sizeof(float) during memcpy for speeds, and ensures Destroy() is called before allocating new actions. Review feedback highlights potential undefined behavior if member pointers are uninitialized when Destroy() is called, suggests an exception-safe allocation pattern, and recommends defining MAX_PET_ACTIONS in a shared location to avoid duplication.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| Destroy(); | ||
|
|
||
| m_count = count; | ||
| m_actions = new int[count](); | ||
| memcpy(m_actions, actions, sizeof(int) * m_count); | ||
|
|
||
| m_speeds = new float[count]; | ||
| memcpy(m_speeds, speeds, sizeof(int) * m_count); | ||
| memcpy(m_speeds, speeds, sizeof(float) * m_count); |
There was a problem hiding this comment.
☑ Potential Undefined Behavior & Exception Safety Issues
-
Uninitialized Pointers on First Call:
CallingDestroy()at the beginning ofSetActionsassumes thatm_actionsandm_speedsare already initialized tonullptr(orNULL) in thePetInfoconstructor. If they are not initialized, callingDestroy()on a newly constructedPetInfoobject will attempt to delete garbage pointers, leading to undefined behavior or immediate crashes. Please ensure they are initialized tonullptrin the constructor. -
Exception Safety (Exception-Safe Allocation):
Ifnew float[count]throwsstd::bad_allocafternew int[count]()succeeds, the previous state is already destroyed byDestroy(), but the member variables are not fully updated, leaving the object in an inconsistent state.
We can make this exception-safe by allocating the new buffers first, copying the data, and only then calling Destroy() and updating the member variables.
int* new_actions = new int[count]();
float* new_speeds = new float[count];
memcpy(new_actions, actions, sizeof(int) * count);
memcpy(new_speeds, speeds, sizeof(float) * count);
Destroy();
m_count = count;
m_actions = new_actions;
m_speeds = new_speeds;| void PetInfo::SetActions(int count, int* actions, float* speeds) | ||
| { | ||
| if (NULL == actions || 0 >= count || NULL == speeds) | ||
| constexpr int MAX_PET_ACTIONS = 100; |
There was a problem hiding this comment.
♻ Duplicate Constant Definition
The constant MAX_PET_ACTIONS is defined locally in both PetInfo::SetActions and PetProcess::LoadData. This duplication can lead to maintenance issues if the limit needs to be changed in the future.
Consider defining MAX_PET_ACTIONS in a shared location, such as the header file w_PetProcess.h or at the top of w_PetProcess.cpp.
|
Reopened against upstream instead: sven-n#500 |
Cherry-picks the residual hardening from sven-n#494 (by @NaraBalvan) onto current main.
Why this is a subset of sven-n#494
sven-n#494 fixed three x64 login-scene crashes. Two of them (the camera waypoint count read and the pet speed
memcpysize) already landed on main via the native-Linux port (sven-n#497), so those hunks are redundant and conflict with main. This PR keeps only what main does not already have: the defensive hardening.Changes (
w_PetProcess.cpp)PetInfo::SetActions: rejectcount <= 0orcount > 100, and callDestroy()before reallocating so a repeat call does not leak the previousm_actions/m_speeds. Alsosizeof(int)->sizeof(float)on the speeds copy (no-op since both are 4 bytes, but correct).PetProcess::LoadData: skip entries whose_type/_countare out of range instead of allocating from corrupt values.Notes
cmake --build ... --target Main).