ultimate-research / ssbh_lib

Reading and writing SSBH file formats in Rust
MIT License
8 stars 3 forks source link

allocation failures on malformed array lengths #73

Closed ScanMountGoat closed 3 years ago

ScanMountGoat commented 3 years ago

Attempting to parse an offset of 16 and length of u64::MAX will first attempt to allocate an enormous vector and then seek by 16 and start filling the vector. This will panic when the allocation fails.

In practice, it's more common that the file itself is small, but the array length is malformed. In this case, the vector shouldn't be allocated up front. If less than the array's length many elements can be read, the read method should return an error. This could be achieved by only reserving up to N elements of capacity.

ScanMountGoat commented 3 years ago

This applies to SsbhArray and SsbhByteBuffer. https://github.com/ultimate-research/ssbh_lib/blob/4a1cc5a14e7771b08b139fe0f6891e3cb49b38fd/ssbh_lib/src/lib.rs#L331-L344

ScanMountGoat commented 3 years ago

This isn't possible to fix completely, but the current measures will reject negative integers without noticeably impacting performance.