diff --git a/beacon_chain/eth2_json_rpc_serialization.nim b/beacon_chain/eth2_json_rpc_serialization.nim index 5b7b267ad..eab18ed9f 100644 --- a/beacon_chain/eth2_json_rpc_serialization.nim +++ b/beacon_chain/eth2_json_rpc_serialization.nim @@ -40,10 +40,16 @@ proc fromJson*(n: JsonNode, argName: string, result: var Version) = proc `%`*(value: Version): JsonNode = result = newJString($value) -template genFromJsonForIntType(t: untyped) = - proc fromJson*(n: JsonNode, argName: string, result: var t) = +template genFromJsonForIntType(T: untyped) = + proc fromJson*(n: JsonNode, argName: string, result: var T) = n.kind.expect(JInt, argName) - result = n.getInt().t + 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 = T(asInt) genFromJsonForIntType(Epoch) genFromJsonForIntType(Slot) diff --git a/beacon_chain/validator_api.nim b/beacon_chain/validator_api.nim index 22a84cbf1..6d73f0945 100644 --- a/beacon_chain/validator_api.nim +++ b/beacon_chain/validator_api.nim @@ -43,7 +43,7 @@ func checkEpochToSlotOverflow(epoch: Epoch) = const maxEpoch = compute_epoch_at_slot(not 0'u64) if epoch >= maxEpoch: raise newException( - CatchableError, "Requesting epoch for which slot would overflow") + ValueError, "Requesting epoch for which slot would overflow") proc doChecksAndGetCurrentHead(node: BeaconNode, slot: Slot): BlockRef = result = node.chainDag.head diff --git a/docs/the_auditors_handbook/src/02.2.2_casting_and_low_level_memory_representation.md b/docs/the_auditors_handbook/src/02.2.2_casting_and_low_level_memory_representation.md index bcc2bf3e5..bbe9eb1e8 100644 --- a/docs/the_auditors_handbook/src/02.2.2_casting_and_low_level_memory_representation.md +++ b/docs/the_auditors_handbook/src/02.2.2_casting_and_low_level_memory_representation.md @@ -2,7 +2,9 @@ ## Conversions -Casting to or from a signed integer will lead to a range check +Casting to a signed integer will lead to a range check. +Conversion to an unsigned integer even from a negative signed integer will NOT lead to a range check (https://github.com/nim-lang/RFCs/issues/175) +https://nim-lang.org/docs/manual.html#statements-and-expressions-type-conversions ## Casting integers diff --git a/vendor/nim-json-rpc b/vendor/nim-json-rpc index dff46c991..99455437b 160000 --- a/vendor/nim-json-rpc +++ b/vendor/nim-json-rpc @@ -1 +1 @@ -Subproject commit dff46c991d8ff533991ce9ae7fbeda07f18e6956 +Subproject commit 99455437ba3d83d5af1c38007fedeeff295e959e