Skip to content

Add null checks and thread guards to GetBlockSize - #1131

Open
mhucka wants to merge 1 commit into
tensorflow:masterfrom
mhucka:fix-get-block-size-safety
Open

mhucka wants to merge 1 commit into
tensorflow:masterfrom
mhucka:fix-get-block-size-safety

Conversation

@mhucka

@mhucka mhucka commented Oct 28, 2026

Copy link
Copy Markdown
Member

This is a fix for b/510434542: null pointer dereference and division by zero in GetBlockSize in tensorflow_quantum/core/ops/tfq_simulate_utils.cc.

The change is to return 1 in tfq_simulate_utils.cc whenever certain conditions hold, and otherwise return a value that preserves std::max(output_size / num_threads, 1). This PR also adds a test file for test_simulate_utils.cc.

This PR was developed with the help of Google Gemini.

Return 1 early in tfq::GetBlockSize when output_size <= 0, context or
context->device() is null, or tensorflow_cpu_worker_threads() is null or
has non-positive num_threads, and clamp positive floor division to at
least 1.

Update the header contract, clean up IWYU includes, and add hermetic C++
unit tests covering null context/device, short-circuiting, null or
non-positive worker threads, floor-division clamping, and single-thread
execution.
@mhucka
mhucka marked this pull request as ready for review October 28, 2026 02:30

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

Code Review

This pull request improves the robustness of the GetBlockSize utility function in tfq_simulate_utils.cc by adding validation checks for null pointers, non-positive output sizes, and invalid thread counts. It also updates the function's documentation and introduces a comprehensive suite of unit tests in tfq_simulate_utils_test.cc to verify these edge cases. No review comments were provided, so there is no feedback to address.

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