Skip to content

Commit

Permalink
adds code and test to detect logical clock overflow
Browse files Browse the repository at this point in the history
we don't expect it to be possible to have CRDB
generate 4B conflicts
  • Loading branch information
vroldanbet committed Nov 6, 2024
1 parent cb99ecc commit ae27707
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/datastore/revisions/hlcrevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ func parseHLCRevisionString(revisionStr string) (datastore.Revision, error) {
}

paddedLogicalClockStr := pieces[1] + strings.Repeat("0", logicalClockLength-len(pieces[1]))
logicalclock, err := strconv.ParseUint(paddedLogicalClockStr, 10, 32)
logicalclock, err := strconv.ParseUint(paddedLogicalClockStr, 10, 64)
if err != nil {
return datastore.NoRevision, fmt.Errorf("invalid revision string: %q", revisionStr)
}

if logicalclock > math.MaxUint32 {
return datastore.NoRevision, spiceerrors.MustBugf("received logical lock that exceeds MaxUint32 (%d > %d): revision %q", logicalclock, math.MaxUint32, revisionStr)
}

uintLogicalClock, err := safecast.ToUint32(logicalclock)
if err != nil {
return datastore.NoRevision, spiceerrors.MustBugf("could not cast logicalclock to uint32: %v", err)
Expand Down
7 changes: 7 additions & 0 deletions internal/datastore/revisions/hlcrevision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ func TestHLCToFromDecimal(t *testing.T) {
}
}

func TestFailsIfLogicalClockExceedsMaxUin32(t *testing.T) {
expectedError := "received logical lock that exceeds MaxUint32 (9999999999 > 4294967295): revision \"0.9999999999\""
require.PanicsWithValue(t, expectedError, func() {
_, _ = HLCRevisionFromString("0.9999999999")
})
}

func BenchmarkHLCParsing(b *testing.B) {
tcs := []string{
"1",
Expand Down

0 comments on commit ae27707

Please sign in to comment.