Skip to content

fix(locations): use decimal path keys so option locations do not collide - #1656

Open
0z5a wants to merge 1 commit into
googleapis:mainfrom
0z5a:fix/locations-decimal-path-keys
Open

0z5a wants to merge 1 commit into
googleapis:mainfrom
0z5a:fix/locations-decimal-path-keys

Conversation

@0z5a

@0z5a 0z5a commented Nov 16, 2026

Copy link
Copy Markdown

strPath builds the key for each source location with string(segment), which converts every int32 path element to a rune. Numbers that are not valid code points (above 0x10FFFF, or in 55296-57343) all become U+FFFD, so two paths that differ only in such a number share a key and the later location overwrites the earlier one.

google.api.http (72295728) and google.api.routing (72295729) are both above 0x10FFFF. On a method that sets google.api.routing after google.api.http, locations.MethodHTTPRule returns the span of the routing annotation, so every rule that reports on the HTTP rule points at the wrong lines, and a disable comment placed on the google.api.http option is ignored.

This shows up on real APIs. google/datastore/v1/datastore.proto declares http and then routing on all 8 RPCs; before this change the linter reports its 8 core::0127::resource-name-extraction findings on the routing blocks (for example lines 56-59 for Lookup), and with this change on the http blocks (52-55). google/bigtable/v2/bigtable.proto uses the same order.

The fix writes each segment as a decimal number. The new TestMethodHTTPRuleWithRouting fails without the fix (it gets the routing span {9, 4, 11, 6}) and passes with it; go test ./... passes.

Building the keys this way is also cheaper: over the 1001 source locations of bigtable.proto the key loop goes from about 0.31 ms to 0.16 ms (medians of 6 interleaved benchmark runs) and from 14661 to 2248 allocations. Linting bigtable.proto end to end takes about the same time before and after (median 119 ms vs 114 ms over 11 interleaved runs).

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

strPath built each key with string(segment), which converts the int32
path element to a rune. Every number that is not a valid code point,
including the google.api.http (72295728) and google.api.routing
(72295729) extension numbers, became U+FFFD, so the routing option's
location overwrote the http option's location. MethodHTTPRule then
returned the routing span, rules reported HTTP findings on the wrong
lines, and disable comments on the http option were ignored.

Write each segment as a decimal number.
@0z5a
0z5a requested a review from a team as a code owner November 16, 2026 12:06

@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 fixes a bug in strPath where path segments were incorrectly converted to runes, causing large numbers (like extension numbers) to map to U+FFFD. The fix uses strconv.Itoa and strings.Builder to format segments as decimal numbers, and a new test case is added to verify this behavior. The review feedback suggests a great optimization to avoid heap allocations in strPath by using a stack-allocated byte slice and strconv.AppendInt, which would also eliminate the need for the strings import.

Comment thread locations/locations.go
Comment on lines +26 to 28
"strconv"
"strings"
"sync"

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.

medium

Since we can implement strPath using a stack-allocated byte slice instead of strings.Builder, the strings package is no longer needed. We can remove it from the imports to avoid unused import compilation errors.

Suggested change
"strconv"
"strings"
"sync"
"strconv"
"sync"

Comment thread locations/locations.go
Comment on lines +92 to 101
func strPath(segments []int32) string {
var b strings.Builder
for i, segment := range segments {
if i > 0 {
p += ","
b.WriteByte(',')
}
p += string(segment)
b.WriteString(strconv.Itoa(int(segment)))
}
return
return b.String()
}

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.

medium

We can optimize strPath to avoid heap allocations for individual path segments. Currently, strconv.Itoa allocates a new string for any segment value that is not a small integer (e.g., extension numbers like 72295728).

By using a stack-allocated byte slice (var buf [64]byte) and strconv.AppendInt, we can format the integers directly into the buffer and perform exactly one allocation when converting the final byte slice to a string.

Suggested change
func strPath(segments []int32) string {
var b strings.Builder
for i, segment := range segments {
if i > 0 {
p += ","
b.WriteByte(',')
}
p += string(segment)
b.WriteString(strconv.Itoa(int(segment)))
}
return
return b.String()
}
func strPath(segments []int32) string {
if len(segments) == 0 {
return ""
}
var buf [64]byte
b := buf[:0]
for i, segment := range segments {
if i > 0 {
b = append(b, ',')
}
b = strconv.AppendInt(b, int64(segment), 10)
}
return string(b)
}

This branch has not been deployed

No deployments
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