Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions arraycontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ func (ac *arrayContainer) addOffset(x uint16) (container, container) {
return low, high
}

// validate checks cardinality and sort order of the array container
// validate checks cardinality and that content is strictly increasing
func (ac *arrayContainer) validate() error {
cardinality := ac.getCardinality()

Expand All @@ -1353,7 +1353,7 @@ func (ac *arrayContainer) validate() error {
previous := ac.content[0]
for i := 1; i < len(ac.content); i++ {
next := ac.content[i]
if previous > next {
if previous >= next {
return ErrArrayIncorrectSort
}
previous = next
Expand Down
13 changes: 11 additions & 2 deletions arraycontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,11 +628,20 @@ func TestArrayContainerValidation(t *testing.T) {
array.content[500] = uint16(upperBound + upperBound)

err = array.validate()
assert.Error(t, err)
assert.ErrorIs(t, err, ErrArrayIncorrectSort)

// Adjacent duplicates must be rejected (strictly increasing), matching
// CRoaring, Java, and Rust validators (previous >= next).
array = newArrayContainer()
for i := 0; i < 10; i++ {
array.iadd(uint16(i))
}
array.content[5] = array.content[4]
err = array.validate()
assert.ErrorIs(t, err, ErrArrayIncorrectSort)

// Technically a run, but make sure the incorrect sort detection handles equal elements
// Repeated iadd of the same value is a no-op; container stays valid.
array = newArrayContainer()
for i := 0; i < upperBound; i++ {
array.iadd(uint16(1))
}
Expand Down
18 changes: 18 additions & 0 deletions roaring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3427,6 +3427,24 @@ func TestBitMapValidationFromDeserialization(t *testing.T) {
},
err: ErrArrayIncorrectSort,
},
{
name: "Array Adjacent Duplicates",
loader: func(bm *Bitmap) {
arrayEntries := make([]uint32, 0, 10)
for i := 0; i < 10; i++ {
arrayEntries = append(arrayEntries, uint32(i))
}
bm.AddMany(arrayEntries)
},
corruptor: func(s []byte) {
// Portable single-container array layout: values start at offset 16
// as little-endian uint16s: content[i] at 16+2*i.
// Set content[5] equal to content[4] (adjacent duplicate).
s[26] = 4
s[27] = 0
},
err: ErrArrayIncorrectSort,
},
}

for _, tt := range deserializationTests {
Expand Down
Loading