Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

limit cursor lookup to values_count in Target bound helpers#16420

Open
isl-Ramzi wants to merge 1 commit into
firebase:mainfirebase/firebase-ios-sdk:mainfrom
isl-Ramzi:target-cursor-boundsisl-Ramzi/firebase-ios-sdk:target-cursor-boundsCopy head branch name to clipboard
Open

limit cursor lookup to values_count in Target bound helpers#16420
isl-Ramzi wants to merge 1 commit into
firebase:mainfirebase/firebase-ios-sdk:mainfrom
isl-Ramzi:target-cursor-boundsisl-Ramzi/firebase-ios-sdk:target-cursor-boundsCopy head branch name to clipboard

Conversation

@isl-Ramzi

Copy link
Copy Markdown
Contributor

GetAscendingBound and GetDescendingBound walk order_bys_ but index the cursor's position()->values array with the same counter, and a cursor is allowed to carry fewer components than the query has order-bys, so query.orderBy("a").orderBy("b").startAt(["a1"]) served by an index on (a asc, b asc) reads one google_firestore_v1_Value past the end of that array. The garbage struct goes straight into LowerBoundCompare/UpperBoundCompare, which dereferences whichever union member its uninitialized which_value_type selects; in a debug build it trips the Invalid type value: 0 assertion in GetTypeOrder, and otherwise the value can end up in the returned bound. Everywhere else that reads a cursor iterates values_count instead (Bound::CompareToDocument even asserts values_count <= order_by.size()), so this clamps the two loops the same way and adds regression tests for both bound directions.

@gemini-code-assist

Copy link
Copy Markdown
Contributor
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.

@ncooke3

ncooke3 commented Jul 21, 2026

Copy link
Copy Markdown
Member

/gemini review

@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 updates Target::GetAscendingBound and Target::GetDescendingBound to safely handle query cursors that have fewer components than the target has order-bys, preventing out-of-bounds access. Corresponding unit tests have been added to verify this behavior. The review feedback identifies a potential lifetime issue where a reference is bound to a dereferenced temporary SharedMessage returned by value, and suggests storing the shared pointer wrapper by value instead to prevent undefined behavior.

Comment on lines +255 to +262
const auto& position = *bound.value().position();
// A cursor may have fewer components than the target has order-bys; only
// the leading order-bys have a corresponding cursor value.
size_t count = std::min<size_t>(order_bys_.size(), position.values_count);
for (size_t i = 0; i < count; ++i) {
const auto& order_by = order_bys_[i];
if (order_by.field() == segment.field_path()) {
auto cursor_value = bound.value().position()->values[i];
auto cursor_value = position.values[i];

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.

high

The method bound.value().position() returns a temporary SharedMessage object by value. Binding a reference to the dereferenced temporary (const auto& position = *bound.value().position();) is fragile because the temporary SharedMessage is destroyed at the end of the full expression. While the underlying data remains allocated because bound keeps it alive, this dangling reference pattern is unsafe and can lead to undefined behavior if the context changes. Storing the SharedMessage by value (which is cheap as it is a shared pointer wrapper) is safer and cleaner.

Suggested change
const auto& position = *bound.value().position();
// A cursor may have fewer components than the target has order-bys; only
// the leading order-bys have a corresponding cursor value.
size_t count = std::min<size_t>(order_bys_.size(), position.values_count);
for (size_t i = 0; i < count; ++i) {
const auto& order_by = order_bys_[i];
if (order_by.field() == segment.field_path()) {
auto cursor_value = bound.value().position()->values[i];
auto cursor_value = position.values[i];
auto position = bound.value().position();
// A cursor may have fewer components than the target has order-bys; only
// the leading order-bys have a corresponding cursor value.
size_t count = std::min<size_t>(order_bys_.size(), position->values_count);
for (size_t i = 0; i < count; ++i) {
const auto& order_by = order_bys_[i];
if (order_by.field() == segment.field_path()) {
auto cursor_value = position->values[i];

Comment on lines +324 to +331
const auto& position = *bound.value().position();
// A cursor may have fewer components than the target has order-bys; only
// the leading order-bys have a corresponding cursor value.
size_t count = std::min<size_t>(order_bys_.size(), position.values_count);
for (size_t i = 0; i < count; ++i) {
const auto& order_by = order_bys_[i];
if (order_by.field() == segment.field_path()) {
auto cursor_value = bound.value().position()->values[i];
auto cursor_value = position.values[i];

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.

high

The method bound.value().position() returns a temporary SharedMessage object by value. Binding a reference to the dereferenced temporary (const auto& position = *bound.value().position();) is fragile because the temporary SharedMessage is destroyed at the end of the full expression. While the underlying data remains allocated because bound keeps it alive, this dangling reference pattern is unsafe and can lead to undefined behavior if the context changes. Storing the SharedMessage by value (which is cheap as it is a shared pointer wrapper) is safer and cleaner.

Suggested change
const auto& position = *bound.value().position();
// A cursor may have fewer components than the target has order-bys; only
// the leading order-bys have a corresponding cursor value.
size_t count = std::min<size_t>(order_bys_.size(), position.values_count);
for (size_t i = 0; i < count; ++i) {
const auto& order_by = order_bys_[i];
if (order_by.field() == segment.field_path()) {
auto cursor_value = bound.value().position()->values[i];
auto cursor_value = position.values[i];
auto position = bound.value().position();
// A cursor may have fewer components than the target has order-bys; only
// the leading order-bys have a corresponding cursor value.
size_t count = std::min<size_t>(order_bys_.size(), position->values_count);
for (size_t i = 0; i < count; ++i) {
const auto& order_by = order_bys_[i];
if (order_by.field() == segment.field_path()) {
auto cursor_value = position->values[i];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.