Skip to content

Commit 349eb20

Browse files
committed
Fix NewForHLC for unpadded strings
1 parent fd5a6cd commit 349eb20

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

internal/datastore/revisions/hlcrevision.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ func parseHLCRevisionString(revisionStr string) (datastore.Revision, error) {
5050
return datastore.NoRevision, fmt.Errorf("invalid revision string: %q", revisionStr)
5151
}
5252

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

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

6263
return HLCRevision{timestamp, uint32(logicalclock) + logicalClockOffset}, nil

internal/datastore/revisions/hlcrevision_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func TestNewForHLC(t *testing.T) {
1717
"-1",
1818
"1.0000000023",
1919
"1703283409994227985.0000000004",
20+
"1703283409994227985.0000000040",
21+
"1703283409994227985.0010000000",
2022
}
2123

2224
for _, tc := range tcs {
@@ -41,6 +43,7 @@ func TestTimestampNanoSec(t *testing.T) {
4143
"1.0000000023": 1,
4244
"9223372036854775807.0000000002": 9223372036854775807,
4345
"1703283409994227985.0000000004": 1703283409994227985,
46+
"1703283409994227985.0000000040": 1703283409994227985,
4447
}
4548

4649
for tc, nano := range tcs {
@@ -63,6 +66,11 @@ func TestInexactFloat64(t *testing.T) {
6366
"1.0000000023": 1.0000000023,
6467
"9223372036854775807.0000000002": 9223372036854775807.0000000002,
6568
"1703283409994227985.0000000004": 1703283409994227985.0000000004,
69+
"1703283409994227985.0000000040": 1703283409994227985.000000004,
70+
"1703283409994227985.000000004": 1703283409994227985.000000004,
71+
"1703283409994227985.0010": 1703283409994227985.001,
72+
"1703283409994227985.0010000000": 1703283409994227985.001,
73+
"1703283409994227985.001": 1703283409994227985.001,
6674
}
6775

6876
for tc, floatValue := range tcs {
@@ -117,6 +125,15 @@ func TestHLCKeyEquals(t *testing.T) {
117125
{
118126
"1703283409994227985.0000000005", "1703283409994227985.0000000005", true,
119127
},
128+
{
129+
"1703283409994227985.0000000050", "1703283409994227985.0000000050", true,
130+
},
131+
{
132+
"1703283409994227985.0000000050", "1703283409994227985.0000000005", false,
133+
},
134+
{
135+
"1703283409994227985.000000005", "1703283409994227985.0000000050", true,
136+
},
120137
}
121138

122139
for _, tc := range tcs {

0 commit comments

Comments
 (0)