This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
Better handle errors from overlong arrays or overlarge pointers #2417
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR puts better error-handling around uses of numbers that are possibly too large in the decoder. Two new
DecoderError
types,OverlongArraysAndStringsNotImplementedError
andOverlargePointersNotImplementedError
, have been added. The latter is used when a pointer is too large to safely fit in anumber
, the former when a length is. These both have "NotImplemented" in the name to make it clear that, yeah, I guess such a thing could exist, but we don't support it.The old
PointerTooLargeError
is gone, along with the check for it, as it's been superseeded byOverlargePointersNotImplementedError
, and the check for it was only ever there to prevent problems withtoNumber()
. However, the oldOverlongArrayOrStringError
, and the check for it, is still there, though it's been renamedOverlongArrayOrStringStrictModeError
. It's still there because it has a purpose beyond preventing problems withtoNumber()
; it's there to make sure the decoder doesn't accidentally DOS itself should it, when decoding a possibly-ambiguous event, mistake a really large number (but not so large as to not safely fit in anumber
) for a length. So that's still around.Also, checks for number safety have been added in
read/storage.ts
andread/bytes.ts
, meaning two new types of read errors have been added,ReadErrorStorage
andReadErrorBytes
. Unfortunately if you get one of these the message may not be as informative as I'd like, but, oh well. (This is especially true ofReadErrorBytes
, which, due to the way things are currently organized, can't even tell you whether the read error ocurred in memory or in calldata.) In any case it's not like I expect this sort of thing to come up much at all; there's a reason I didn't bother with these errors before. Also, reads indecode/storage.ts
have now been wrapped in try/catches as a result.This PR also gets rid of a ton of circular dependencies that existed in
@truffle/codec
, because at least one of them was causing problems. So, a number of types and functions have been moved around, but ultimately it all worked out. Well, almost -- there's one circular dependency still remaining, and it's one introduced in this PR. I've leaving it in because it's not causing any problems at the moment and it looks like getting rid of it may be difficult. I'm planning to address that in a separate PR.(Edit: OK, actually I added another commit to this PR, addressing it. Read on for the original description of the circularity, and see the following comment for how I resolved it.)
That circular dependency is this:
format/errors.ts
depends ontypes/storage.ts
, because the newReadErrorStorage
includes aRange
as part of the error.types/storage.ts
depends onformat/values.ts
, because aSlot
may contain anElementaryValue
as a mapping key.format/values.ts
depends onformat/errors.ts
, because aResult
may be anErrorResult
.The best solution to this is presumably to split
Values
intoValues
andResults
, but that's going to be a pain to do, so like I said, I'm saving it for a different PR. Once that's done, though, then you'll haveResult > ErrorResult > DecoderError > ReadErrorStorage > Range > Slot > ElementaryValue
with no circularity.