Skip to content

Commit d35993e

Browse files
committed
sstable: avoid overflow in GetScaledProperties
1 parent 1e81d2b commit d35993e

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

sstable/properties.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"strings"
1818
"unsafe"
1919

20+
"github.com/cockroachdb/crlib/crmath"
2021
"github.com/cockroachdb/pebble/internal/intern"
2122
"github.com/cockroachdb/pebble/internal/invariants"
2223
"github.com/cockroachdb/pebble/sstable/colblk"
@@ -143,7 +144,7 @@ func (c *CommonProperties) GetScaledProperties(backingSize, size uint64) CommonP
143144
backingSize = max(backingSize, size)
144145

145146
scale := func(a uint64) uint64 {
146-
return (a*size + backingSize - 1) / backingSize
147+
return crmath.ScaleUint64(a, size, backingSize)
147148
}
148149
// It's important that no non-zero fields (like NumDeletions, NumRangeKeySets)
149150
// become zero (or vice-versa).

sstable/properties_test.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,10 @@ func TestPropertiesSave(t *testing.T) {
138138
func TestScalePropertiesBadSizes(t *testing.T) {
139139
// Verify that GetScaledProperties works even if the given sizes are not
140140
// valid.
141-
p := Properties{
142-
CommonProperties: CommonProperties{
143-
NumDeletions: 0,
144-
NumEntries: 1,
145-
NumDataBlocks: 10,
146-
},
141+
p := CommonProperties{
142+
NumDeletions: 0,
143+
NumEntries: 1,
144+
NumDataBlocks: 10,
147145
}
148146
scaled := p.GetScaledProperties(100, 0)
149147
require.Equal(t, uint64(0), scaled.NumDeletions)
@@ -156,6 +154,23 @@ func TestScalePropertiesBadSizes(t *testing.T) {
156154
require.Equal(t, uint64(10), scaled.NumDataBlocks)
157155
}
158156

157+
func TestScalePropertiesOverflow(t *testing.T) {
158+
// Verify that GetScaledProperties works even if the given sizes are not
159+
// valid.
160+
p := CommonProperties{
161+
RawKeySize: 1 << 40,
162+
RawValueSize: 1 << 50,
163+
}
164+
scaled := p.GetScaledProperties(1<<60, 1<<60)
165+
require.Equal(t, p, scaled)
166+
167+
scaled = p.GetScaledProperties(1<<60, 1<<50)
168+
require.Equal(t, scaled, CommonProperties{
169+
RawKeySize: 1 << 30,
170+
RawValueSize: 1 << 40,
171+
})
172+
}
173+
159174
func BenchmarkPropertiesLoad(b *testing.B) {
160175
var w rowblk.Writer
161176
w.RestartInterval = propertiesBlockRestartInterval

0 commit comments

Comments
 (0)