Skip to content

Harden pet data loading against malformed pet.bmd entries#69

Closed
Mosch0512 wants to merge 1 commit into
mainfrom
harden-pet-data-load
Closed

Harden pet data loading against malformed pet.bmd entries#69
Mosch0512 wants to merge 1 commit into
mainfrom
harden-pet-data-load

Conversation

@Mosch0512

Copy link
Copy Markdown
Owner

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 memcpy size) 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: reject count <= 0 or count > 100, and call Destroy() before reallocating so a repeat call does not leak the previous m_actions/m_speeds. Also sizeof(int) -> sizeof(float) on the speeds copy (no-op since both are 4 bytes, but correct).
  • PetProcess::LoadData: skip entries whose _type/_count are out of range instead of allocating from corrupt values.

Notes

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.
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, 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

  • Defensive Pet Data Loading: Added bounds checking for pet actions and counts in PetProcess::LoadData to prevent processing of malformed data.
  • Memory Management Improvements: Updated PetInfo::SetActions to call Destroy() before reallocating memory, preventing potential memory leaks on repeated calls.
  • Code Correctness: Corrected memcpy size calculation in PetInfo::SetActions and standardized pointer types for pet speed data.
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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

Comment on lines +52 to +59
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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

☑ Potential Undefined Behavior & Exception Safety Issues

  1. Uninitialized Pointers on First Call:
    Calling Destroy() at the beginning of SetActions assumes that m_actions and m_speeds are already initialized to nullptr (or NULL) in the PetInfo constructor. If they are not initialized, calling Destroy() on a newly constructed PetInfo object will attempt to delete garbage pointers, leading to undefined behavior or immediate crashes. Please ensure they are initialized to nullptr in the constructor.

  2. Exception Safety (Exception-Safe Allocation):
    If new float[count] throws std::bad_alloc after new int[count]() succeeds, the previous state is already destroyed by Destroy(), 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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

♻ 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.

@Mosch0512

Copy link
Copy Markdown
Owner Author

Reopened against upstream instead: sven-n#500

@Mosch0512 Mosch0512 closed this Jun 16, 2026
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