Treat some with_capacity calls as a hint. #394
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.
In many places, the existing code uses
with_capacity
to preallocate a container in preparation as an optimization for parsing the following fields. In cases where the capacity is read directly from a field in the file and passed towith_capacity
, it's trivial for an invalid file to trigger a controlled OOM crash by specifying a sufficiently large size.This is intended to fix the crash mentioned in BMO 1813063 comment 14, triggered by a fuzzer generated file containing an stsd box reporting 738197505 entries in the sample description table (but only containing 1).
I've replaced each case where we read a u32 or larger field directly from the file and use it as a size hint for
with_capacity
to calls to the trivial wrappersvec_with_capacity_hint
andhashmap_with_capacity_hint
that limit the maximum preallocation size to the arbitrary value of 1MBCAPACITY_HINT_LIMIT
.