Skip to content

Conversation

@g-k-s-03
Copy link

@g-k-s-03 g-k-s-03 commented Dec 7, 2025

close #289
Description:
This PR addresses a critical null pointer dereference in the Win32Window::WndProc function that could crash the application in rare circumstances.

Issue:
The WndProc function calls GetThisFromHandle(window) and immediately dereferences the returned pointer:
else if (Win32Window* that = GetThisFromHandle(window)) {
return that->MessageHandler(window, message, wparam, lparam);
}

If a Windows message arrives before WM_NCCREATE, GWLP_USERDATA is not yet initialized, causing GetThisFromHandle(window) to return nullptr.

Dereferencing this pointer results in undefined behavior or a crash.

Solution:
Added a null check before calling MessageHandler:

Win32Window* that = GetThisFromHandle(window);
if (that) {
return that->MessageHandler(window, message, wparam, lparam);
}

Messages received before WM_NCCREATE now safely fall through to DefWindowProc, preventing crashes.

Optional debug logging was added for early messages (guarded by #ifdef DEBUG_EARLY_MESSAGES) to assist with debugging if needed.

Impact:
Fixes crashes caused by early messages on some systems.
No behavior change for normal message handling after WM_NCCREATE.

Tested:
Compiled and ran the application on Windows.
Verified no crash occurs with messages arriving before and after WM_NCCREATE.

Summary by Sourcery

Prevent Win32 window message handler from dereferencing a null window instance before initialization and add generated Gradle configuration cache problem report artifact.

Bug Fixes:

  • Guard Win32Window::WndProc against null results from GetThisFromHandle to avoid crashes from early window messages before WM_NCCREATE.

Enhancements:

  • Add optional debug logging for early Win32 window messages when enabled via DEBUG_EARLY_MESSAGES.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 7, 2025

Reviewer's Guide

Fixes a critical null pointer dereference in Win32Window::WndProc by adding a safe GetThisFromHandle call with a null check and optional debug logging, and also introduces a generated Gradle configuration cache HTML report file under android/build/reports/problems/.

Sequence diagram for updated Win32Window::WndProc message handling

sequenceDiagram
  participant OS as OS_Windows
  participant WndProc as Win32Window_WndProc
  participant Win32Window as Win32Window_instance
  participant DefProc as DefWindowProc

  OS->>WndProc: WndProc(window, message, wparam, lparam)
  alt message == WM_NCCREATE
    WndProc->>WndProc: get CREATESTRUCT from lparam
    WndProc->>Win32Window: construct instance (lpCreateParams)
    WndProc->>WndProc: EnableFullDpiSupportIfAvailable(window)
    WndProc->>Win32Window: that.window_handle_ = window
  end

  WndProc->>WndProc: that = GetThisFromHandle(window)
  alt that != nullptr
    WndProc->>Win32Window: MessageHandler(window, message, wparam, lparam)
    Win32Window-->>WndProc: LRESULT
    WndProc-->>OS: LRESULT
  else that == nullptr (early message before WM_NCCREATE)
    opt DEBUG_EARLY_MESSAGES defined
      WndProc->>WndProc: format debug_msg with message and window
      WndProc->>WndProc: OutputDebugString(debug_msg)
    end
    WndProc->>DefProc: DefWindowProc(window, message, wparam, lparam)
    DefProc-->>WndProc: LRESULT
    WndProc-->>OS: LRESULT
  end
Loading

Class diagram for Win32Window and WndProc changes

classDiagram
  class Win32Window {
    +HWND window_handle_
    +static LRESULT CALLBACK WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
    +LRESULT MessageHandler(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
  }

  class Helpers {
    +static Win32Window* GetThisFromHandle(HWND window)
    +static void EnableFullDpiSupportIfAvailable(HWND window)
  }

  Win32Window ..> Helpers : uses
  Win32Window ..> Win32Window : GetThisFromHandle returns this
  Win32Window : WndProc
  Win32Window : MessageHandler
Loading

File-Level Changes

Change Details Files
Prevent null pointer dereference when handling early Win32 window messages in Win32Window::WndProc.
  • Refactor WndProc to call GetThisFromHandle(window) once and store the result in a local variable.
  • Add a null check before invoking MessageHandler to ensure 'that' is non-null.
  • Route messages received before WM_NCCREATE to DefWindowProc when no Win32Window instance is available.
  • Introduce optional debug logging (guarded by DEBUG_EARLY_MESSAGES) to trace early messages that arrive before WM_NCCREATE.
  • Keep behavior unchanged for normal message handling after WM_NCCREATE and ensure window_handle_ is set before use.
windows/runner/win32_window.cpp
Add a generated Gradle configuration cache problems HTML report to the Android build outputs.
  • Add android/build/reports/problems/problems-report.html containing a static Gradle configuration cache problems report page.
  • Include embedded styles, scripts, and report data for local inspection of Gradle configuration cache warnings (e.g., deprecated JVM versions).
android/build/reports/problems/problems-report.html

Assessment against linked issues

Issue Objective Addressed Explanation
#289 Modify Win32Window::WndProc so that calls to GetThisFromHandle(window) are null-checked before dereferencing, preventing a potential null pointer dereference when GWLP_USERDATA is not yet initialized.
#289 Ensure that messages received before WM_NCCREATE are handled safely (e.g., fall through to DefWindowProc without dereferencing a null Win32Window pointer), preserving normal behavior once the window is initialized.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • The Gradle problems-report.html file under android/build/reports appears to be a generated artifact; consider removing it from version control and adding it to .gitignore to avoid bloating the repo with build outputs.
  • In Win32Window::WndProc, the new debug logging block uses OutputDebugString directly; if the project has an existing logging or tracing utility, it would be better to route this through that for consistency and configurability.
  • There is a minor style issue in win32_window.cpp around the new null-check block (extra blank line and trailing whitespace after the closing brace); aligning this with the surrounding formatting would keep the file consistent.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The Gradle problems-report.html file under android/build/reports appears to be a generated artifact; consider removing it from version control and adding it to .gitignore to avoid bloating the repo with build outputs.
- In Win32Window::WndProc, the new debug logging block uses OutputDebugString directly; if the project has an existing logging or tracing utility, it would be better to route this through that for consistency and configurability.
- There is a minor style issue in win32_window.cpp around the new null-check block (extra blank line and trailing whitespace after the closing brace); aligning this with the surrounding formatting would keep the file consistent.

## Individual Comments

### Comment 1
<location> `windows/runner/win32_window.cpp:171-173` </location>
<code_context>
-  } else if (Win32Window* that = GetThisFromHandle(window)) {
+  } 
+  
+  Win32Window* that = GetThisFromHandle(window);
+  if (that) {
     return that->MessageHandler(window, message, wparam, lparam);
+  } else {
+    // Optional: debug message for messages arriving before WM_NCCREATE
</code_context>

<issue_to_address>
**issue (bug_risk):** WM_NCCREATE will now also go through MessageHandler, which changes previous behavior.

Previously, `WM_NCCREATE` exited before reaching `MessageHandler`, but now it also flows through `GetThisFromHandle(window)` and into `MessageHandler` (if the handle is registered). If this behavior change isn’t desired, consider keeping the `else` or explicitly bypassing `WM_NCCREATE` to match the old behavior.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

Potential null pointer dereference in Win32Window::WndProc

1 participant