Repository navigation
Conversation
This fixes b/510433099, "[TFQ] Memory Corruption via Uninitialized or
Unchecked Qubit IDs in Parsing":
> The ParseAppendGate and ParseProtoControls functions access
> op.qubits(i) or argument maps without verifying existence. They also
> use absl::SimpleAtoi and ignore its failure, using uninitialized stack
> variables as qubit indices. This leads to heap out-of-bounds
> reads/writes in the simulator.
This is an instance of CWE-125.
The solution implemented here is in the form of a `ParseQubitIds`
helper, then use that in all 15 gate/channel builders plus control-qubit
checks.
With respect to performance, `ParseQubitIds` is inline in the anonymous
namespace of `circuit_parser_qsim.cc` and is always called with a
compile-time-sized 1- or 2-element `std::initializer_list<unsigned
int*>:({&q0} or {&q0, &q1})`. Under -O2/-c opt, the compiler inlines the
call, folds `qubits.size()` to 1 or 2, unrolls the loop, and eliminates
the pointer indirection. There is no function-call overhead.
mhucka
marked this pull request as ready for review
October 13, 2026 23:20
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces robust validation for qubit and control qubit IDs in the qsim circuit parser. It adds a new helper function ParseQubitIds to verify that qubit IDs are valid integers within the allowed range and are distinct. It also updates control qubit parsing to ensure control qubits are valid and distinct from both each other and target qubits. Comprehensive unit tests are added to verify these validation rules across various gates and channels. I have no feedback to provide as there are no review comments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This fixes b/510433099, "[TFQ] Memory Corruption via Uninitialized or Unchecked Qubit IDs in Parsing":
This is an instance of CWE-125.
The solution implemented here is in the form of a
ParseQubitIdshelper, then use that in all 15 gate/channel builders plus control-qubit checks.With respect to performance,
ParseQubitIdsis inline in the anonymous namespace ofcircuit_parser_qsim.ccand is always called with a compile-time-sized 1- or 2-elementstd::initializer_list<unsigned int*>:({&q0} or {&q0, &q1}). Under -O2/-c opt, the compiler inlines the call, foldsqubits.size()to 1 or 2, unrolls the loop, and eliminates the pointer indirection. There is no function-call overhead.