Skip to content
Open
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
2 changes: 1 addition & 1 deletion algo/reach.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (s *ReachabilityCache) OrReach(node uint64, direction graph.Direction, dupl
// from the result before the XOR operation.
func (s *ReachabilityCache) XorReach(node uint64, direction graph.Direction, duplex cardinality.Duplex[uint64]) {
// Reach bitmap will contain the member due to resolution of component reach
reachBitmap := s.ReachOfComponentContainingMember(node, direction).Clone()
reachBitmap := s.ReachOfComponentContainingMember(node, direction).Clone().(cardinality.Duplex[uint64])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Same unchecked .(cardinality.Duplex[uint64]) assertion on Clone() result

Same pattern as in graph/types.go:185ReachOfComponentContainingMember returns Duplex[uint64], Clone() now deliberately returns the narrower Provider[uint64], and the naked assertion to Duplex[uint64] will panic if an immutable clone implementation is introduced. The impact here is particularly acute because XorReach is called on the hot algorithmic path.

🛡️ Safer assertion form
-	reachBitmap := s.ReachOfComponentContainingMember(node, direction).Clone().(cardinality.Duplex[uint64])
+	cloned, ok := s.ReachOfComponentContainingMember(node, direction).Clone().(cardinality.Duplex[uint64])
+	if !ok {
+		panic(fmt.Sprintf("cardinality: Clone() returned non-Duplex type for XorReach"))
+	}
+	reachBitmap := cloned
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@algo/reach.go` at line 281, The current code does a naked assertion of
Clone() to cardinality.Duplex[uint64] (reachBitmap :=
s.ReachOfComponentContainingMember(node,
direction).Clone().(cardinality.Duplex[uint64])) which will panic if Clone()
returns the narrower cardinality.Provider[uint64]; change it to assert to
cardinality.Provider[uint64] first and handle both cases safely: perform a type
switch or ok-check on the Clone() result, use it directly if it's already a
Duplex, otherwise take the Provider and construct or copy into a new Duplex
before using it (so XorReach and other hot-path code always receive a safe,
mutable Duplex without risking a panic).

reachBitmap.Remove(node)

duplex.Xor(reachBitmap)
Expand Down
38 changes: 26 additions & 12 deletions cardinality/cardinality.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ type DuplexConstructor[T uint32 | uint64] func() Duplex[T]
// Provider describes the most basic functionality of a cardinality provider algorithm: adding elements to the provider
// and producing the cardinality of those elements.
type Provider[T uint32 | uint64] interface {
Add(value ...T)
Or(other Provider[T])
Clear()
Cardinality() uint64
}

Expand All @@ -26,13 +23,21 @@ func CloneProvider[T uint32 | uint64](provider Provider[T]) Provider[T] {
}
}

type ImmutableSimplex[T uint32 | uint64] interface {
Provider[T]

Clone() Simplex[T]
}

// Simplex is a one-way cardinality provider that does not allow a user to retrieve encoded values back out of the
// provider. This interface is suitable for algorithms such as HyperLogLog which utilizes a hash function to merge
// identifiers into the cardinality provider.
type Simplex[T uint32 | uint64] interface {
Provider[T]
ImmutableSimplex[T]

Clone() Simplex[T]
Add(value ...T)
Or(other Provider[T])
Clear()
}

// Iterator allows enumeration of a duplex cardinality provider without requiring the allocation of the provider's set.
Expand All @@ -41,18 +46,27 @@ type Iterator[T uint32 | uint64] interface {
Next() T
}

type ImmutableDuplex[T uint32 | uint64] interface {
Provider[T]

Slice() []T
Contains(value T) bool
Each(delegate func(value T) bool)

Clone() Duplex[T]
}

// Duplex is a two-way cardinality provider that allows a user to retrieve encoded values back out of the provider. This
// interface is suitable for algorithms that behave similar to bitvectors.
type Duplex[T uint32 | uint64] interface {
Provider[T]
ImmutableDuplex[T]

Add(value ...T)
CheckedAdd(value T) bool
Remove(value T)
Or(other Provider[T])
Xor(other Provider[T])
And(other Provider[T])
AndNot(other Provider[T])
Remove(value T)
Slice() []T
Contains(value T) bool
Each(delegate func(value T) bool)
CheckedAdd(value T) bool
Clone() Duplex[T]
Clear()
}
8 changes: 7 additions & 1 deletion cardinality/roaring32.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ func (s bitmap32) CheckedAdd(value uint32) bool {
}

func (s bitmap32) Add(values ...uint32) {
s.bitmap.AddMany(values)
switch len(values) {
case 0:
case 1:
s.bitmap.Add(values[0])
default:
s.bitmap.AddMany(values)
}
}

func (s bitmap32) Remove(value uint32) {
Expand Down
8 changes: 7 additions & 1 deletion cardinality/roaring64.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ func (s bitmap64) CheckedAdd(value uint64) bool {
}

func (s bitmap64) Add(values ...uint64) {
s.bitmap.AddMany(values)
switch len(values) {
case 0:
case 1:
s.bitmap.Add(values[0])
default:
s.bitmap.AddMany(values)
}
}

func (s bitmap64) Remove(value uint64) {
Expand Down
Loading