Skip to content

Commit

Permalink
fix: netpollmux shard index overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Oct 18, 2021
1 parent c1c542f commit c394eba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/remote/trans/netpollmux/shared_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func newSharedMap(size int32) *sharedMap {

// getShard returns shard under given seq id
func (m *sharedMap) getShard(seqID int32) *shared {
return m.shared[seqID%m.size]
return m.shared[abs(seqID)%m.size]
}

// store stores msg under given seq id.
Expand Down Expand Up @@ -104,3 +104,10 @@ func (m *sharedMap) rangeMap(fn func(seqID int32, msg EventHandler)) {
shard.Unlock()
}
}

func abs(n int32) int32 {
if n < 0 {
return -n
}
return n
}
4 changes: 4 additions & 0 deletions pkg/remote/trans/netpollmux/shared_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func TestSharedMap(t *testing.T) {
msg, ok := m.load(key)
test.Assert(t, msg == nil)
test.Assert(t, ok == false)
// check key<0
msg, ok = m.load(-1)
test.Assert(t, msg == nil)
test.Assert(t, ok == false)
// check key>0
for i := int32(1); i <= mod; i++ {
key := i
Expand Down

0 comments on commit c394eba

Please sign in to comment.