From 99455437ba3d83d5af1c38007fedeeff295e959e Mon Sep 17 00:00:00 2001 From: Mamy Ratsimbazafy Date: Wed, 7 Oct 2020 12:09:36 +0200 Subject: [PATCH] Fix silent uint64 negative conversion https://github.com/status-im/nim-beacon-chain/issues/1671 https://github.com/status-im/nim-beacon-chain/pull/1819 (#85) --- json_rpc/jsonmarshal.nim | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/json_rpc/jsonmarshal.nim b/json_rpc/jsonmarshal.nim index 7c4c00b..ede3f3c 100644 --- a/json_rpc/jsonmarshal.nim +++ b/json_rpc/jsonmarshal.nim @@ -86,7 +86,13 @@ proc fromJson*(n: JsonNode, argName: string, result: var int64) = proc fromJson*(n: JsonNode, argName: string, result: var uint64) = n.kind.expect(JInt, argName) - result = n.getInt().uint64 + let asInt = n.getInt() + # signed -> unsigned conversions are unchecked + # https://github.com/nim-lang/RFCs/issues/175 + if asInt < 0: + raise newException( + ValueError, "JSON-RPC input is an unexpected negative value") + result = uint64(asInt) proc fromJson*(n: JsonNode, argName: string, result: var ref int64) = n.kind.expect(JInt, argName)