Repository navigation
Stop on truncated zlib data and short base64 layer data - #189
Merged
Merged
Conversation
decompress() kept doubling its output buffer when inflate() returned Z_BUF_ERROR, which happens when the compressed data ends early, so a truncated zlib layer grew memory until allocation failed. parseBase64() read expectedSize bytes regardless of how many bytes were decoded or inflated, reading past the end of byteData when the layer data is shorter than width * height * 4. Read only what is there and log a warning.
Owner
|
Wow that was a pretty bad bug, thanks for finding and fixing! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Two problems with malformed base64 layer data:
inflate()returnsZ_BUF_ERROR.decompress()treats it likeZ_OKand doubles the output buffer, so the loop keeps allocating until it runs out of memory. It now fails and the layer is skipped, like other inflate errors.parseBase64()readswidth * height * 4bytes no matter how many were decoded or inflated. If the data is shorter (uncompressed or zlib), this reads past the end ofbyteData. It now reads only the bytes that are there and logs a warning. The same change removes theexpectedSize - 3uunderflow when the tile count is 0.Repro: a 4x2 map loaded with
loadFromString, layer data built from tile IDs 1..8._GLIBCXX_ASSERTIONSaborts inoperator[])Tested on Linux with GCC 11, default build (miniz), with ASan, UBSan and
_GLIBCXX_ASSERTIONS. Every.tmxin the repo (ParseTest and the examples, 12 maps) loads with the same tile IDs before and after, and there are no new warnings with-Wall -Wextra.