From 290ba712e9c5386ed4a22b04abe0e2853c010651 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Tue, 28 Apr 2020 14:43:44 +0300 Subject: [PATCH] Fix MultiAddress.protoAddress() bug for fixed arrays. Add tests for it. --- libp2p/multiaddress.nim | 2 +- tests/testmultiaddress.nim | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libp2p/multiaddress.nim b/libp2p/multiaddress.nim index e87d59f2e..2056c4c3f 100644 --- a/libp2p/multiaddress.nim +++ b/libp2p/multiaddress.nim @@ -482,7 +482,7 @@ proc protoArgument*(ma: MultiAddress, value: var openarray[byte]): int = if proto.kind == Fixed: result = proto.size if len(value) >= result: - if vb.data.readArray(value) != proto.size: + if vb.data.readArray(value.toOpenArray(0, proto.size - 1)) != proto.size: raise newException(MultiAddressError, "Decoding protocol error") elif proto.kind in {Length, Path}: if vb.data.readSeq(buffer) == -1: diff --git a/tests/testmultiaddress.nim b/tests/testmultiaddress.nim index 0b39dfec3..df398bca5 100644 --- a/tests/testmultiaddress.nim +++ b/tests/testmultiaddress.nim @@ -391,3 +391,11 @@ suite "MultiAddress test suite": discard MultiAddress.init(multiCodec("udp"), -1) discard MultiAddress.init(multiCodec("dccp"), -1) discard MultiAddress.init(multiCodec("sctp"), -1) + + test "MultiAddress protoAddress(fixed) test": + var + address_v4: array[4, byte] + address_v6: array[16, byte] + check: + MultiAddress.init("/ip4/0.0.0.0").protoAddress() == address_v4 + MultiAddress.init("/ip6/::0").protoAddress() == address_v6