Commit 2226acb4 authored by Janoš Guljaš's avatar Janoš Guljaš Committed by GitHub

prevent panic in proximity function with shorter second argument (#517)

parent fb8473da
...@@ -19,8 +19,10 @@ package swarm ...@@ -19,8 +19,10 @@ package swarm
// (0 farthest, 255 closest, 256 self) // (0 farthest, 255 closest, 256 self)
func Proximity(one, other []byte) (ret uint8) { func Proximity(one, other []byte) (ret uint8) {
b := MaxPO/8 + 1 b := MaxPO/8 + 1
l := uint8(len(one)) if l := uint8(len(one)); b > l {
if b > l { b = l
}
if l := uint8(len(other)); b > l {
b = l b = l
} }
var m uint8 = 8 var m uint8 = 8
......
...@@ -168,10 +168,30 @@ func TestProximity(t *testing.T) { ...@@ -168,10 +168,30 @@ func TestProximity(t *testing.T) {
addr: []byte{0b00000000, 0b00000000, 0b00000000, 0b00000001}, addr: []byte{0b00000000, 0b00000000, 0b00000000, 0b00000001},
po: limitPO(31), po: limitPO(31),
}, },
{
addr: nil,
po: limitPO(31),
},
{
addr: []byte{0b00000001},
po: limitPO(7),
},
{
addr: []byte{0b00000000},
po: limitPO(31),
},
{
addr: []byte{0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000001},
po: limitPO(31),
},
} { } {
got := Proximity(base, tc.addr) got := Proximity(base, tc.addr)
if got != tc.po { if got != tc.po {
t.Errorf("got %v bin, want %v", got, tc.po) t.Errorf("got %v bin, want %v", got, tc.po)
} }
got = Proximity(tc.addr, base)
if got != tc.po {
t.Errorf("got %v bin, want %v (reverse arguments)", got, tc.po)
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment