Implement memory limits for HDR decoding #2713
Open
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 implements memory limits for the Radiance .hdr image decoder. The current decoder both parses and retains (in HdrMetadata:::custom_attributes) essentially the full header of the file, which can be arbitrarily long, so memory limits need to be provided before header decoding.
The actual implementation and memory accounting is rather awkward, and I've tried to simplify it by overestimating peak memory usage for header decoding, and eliminating some allocations for the signature and dimension lines of the header.. In practice, the HDR images I've found have less than a kilobyte of extra information in their header, so I don't expect constant factor overestimates while header decoding to be an issue.
It would be possible to simplify the memory usage estimation somewhat by changing the HdrMetadata public API to retain
Vec<u8>s instead ofStrings, but I think that would require making the rest of the decoder logic more complicated. (And it would be yet another API break.)Edit: my initial implementation undercounted/miscalculated some allocations in HdrMetadata::update_header_info, so I've updated the PR to track memory more closely to the place where it is allocated. The changes may make the code a bit easier to follow, but also ~40 lines longer.