Skip to content

Commit aaf5a2a

Browse files
committed
cachekey: remove ID length assumption
AppendStrID was copied out of a system that only used UUIDs for string IDs, so we could make the assumption that IDs would be shorter than 127-bytes. This is not true generally, but apparently while copying the function, we forgot to re-evaluate the assumptions in the code. (even when stated in the comments) Fortunately, `encoding/binary.AppendUvarint` will grow the buffer if necessary, so this is a performance bug, not a correctness bug.
1 parent 44942c5 commit aaf5a2a

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

cachekey/enc.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ limitations under the License.
2222
// The encoding scheme has three key design criteria: reasonable performance, simplicity and robustness
2323
//
2424
// - All fields are separated by null bytes
25-
// - Strings are prefixed with a varint length (as handled by encoding/binary)
25+
// - Strings are prefixed with a varint length to eliminate any ambiguity (as handled by encoding/binary)
2626
// - Integers are encoded as a varint.
2727
//
2828
// This design is inspired by
@@ -33,14 +33,17 @@ package cachekey
3333
import (
3434
"encoding/binary"
3535
"slices"
36+
37+
"google.golang.org/protobuf/encoding/protowire"
3638
)
3739

3840
// AppendStrID encodes a string ID into a form that can be concatenated into a V2 galaxycache key
3941
func AppendStrID[I ~string](buf []byte, id I) []byte {
40-
// The IDs are currently guaranteed to be less than 127 bytes long, so
41-
// we can assume that our varint length is exactly one byte.
42-
buf = slices.Grow(buf, 2+len(id))
43-
buf = binary.AppendUvarint(buf, uint64(len(id)))
42+
// The IDs aren't guaranteed to be less than 127 bytes long, so compute the actual requirement
43+
idLen := uint64(len(id))
44+
vIntLen := protowire.SizeVarint(idLen)
45+
buf = slices.Grow(buf, 1+len(id)+vIntLen)
46+
buf = binary.AppendUvarint(buf, idLen)
4447
buf = append(buf, id...)
4548
// add the separating null-byte
4649
buf = append(buf, byte('\000'))

0 commit comments

Comments
 (0)