Skip to content

Commit

Permalink
Fix NewForHLC for unpadded strings
Browse files Browse the repository at this point in the history
  • Loading branch information
josephschorr committed Jan 29, 2024
1 parent fd5a6cd commit 349eb20
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
11 changes: 6 additions & 5 deletions internal/datastore/revisions/hlcrevision.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ func parseHLCRevisionString(revisionStr string) (datastore.Revision, error) {
return datastore.NoRevision, fmt.Errorf("invalid revision string: %q", revisionStr)
}

logicalclock, err := strconv.ParseInt(pieces[1], 10, 32)
if err != nil {
return datastore.NoRevision, fmt.Errorf("invalid revision string: %q", revisionStr)
if len(pieces[1]) > logicalClockLength {
return datastore.NoRevision, spiceerrors.MustBugf("invalid revision string due to unexpected logical clock size (%d): %q", len(pieces[1]), revisionStr)
}

if len(pieces[1]) != logicalClockLength {
return datastore.NoRevision, spiceerrors.MustBugf("invalid revision string due to unexpected logical clock size (%d): %q", len(pieces[1]), revisionStr)
paddedLogicalClockStr := pieces[1] + strings.Repeat("0", logicalClockLength-len(pieces[1]))
logicalclock, err := strconv.ParseInt(paddedLogicalClockStr, 10, 32)
if err != nil {
return datastore.NoRevision, fmt.Errorf("invalid revision string: %q", revisionStr)
}

return HLCRevision{timestamp, uint32(logicalclock) + logicalClockOffset}, nil
Expand Down
17 changes: 17 additions & 0 deletions internal/datastore/revisions/hlcrevision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ func TestNewForHLC(t *testing.T) {
"-1",
"1.0000000023",
"1703283409994227985.0000000004",
"1703283409994227985.0000000040",
"1703283409994227985.0010000000",
}

for _, tc := range tcs {
Expand All @@ -41,6 +43,7 @@ func TestTimestampNanoSec(t *testing.T) {
"1.0000000023": 1,
"9223372036854775807.0000000002": 9223372036854775807,
"1703283409994227985.0000000004": 1703283409994227985,
"1703283409994227985.0000000040": 1703283409994227985,
}

for tc, nano := range tcs {
Expand All @@ -63,6 +66,11 @@ func TestInexactFloat64(t *testing.T) {
"1.0000000023": 1.0000000023,
"9223372036854775807.0000000002": 9223372036854775807.0000000002,
"1703283409994227985.0000000004": 1703283409994227985.0000000004,
"1703283409994227985.0000000040": 1703283409994227985.000000004,
"1703283409994227985.000000004": 1703283409994227985.000000004,
"1703283409994227985.0010": 1703283409994227985.001,
"1703283409994227985.0010000000": 1703283409994227985.001,
"1703283409994227985.001": 1703283409994227985.001,
}

for tc, floatValue := range tcs {
Expand Down Expand Up @@ -117,6 +125,15 @@ func TestHLCKeyEquals(t *testing.T) {
{
"1703283409994227985.0000000005", "1703283409994227985.0000000005", true,
},
{
"1703283409994227985.0000000050", "1703283409994227985.0000000050", true,
},
{
"1703283409994227985.0000000050", "1703283409994227985.0000000005", false,
},
{
"1703283409994227985.000000005", "1703283409994227985.0000000050", true,
},
}

for _, tc := range tcs {
Expand Down

0 comments on commit 349eb20

Please sign in to comment.