Skip to content

Stop on truncated zlib data and short base64 layer data - #189

Merged
fallahn merged 1 commit into
fallahn:masterfrom
vtxe02023274:fix/zlib-truncated-data
Nov 23, 2026
Merged

fallahn merged 1 commit into
fallahn:masterfrom
vtxe02023274:fix/zlib-truncated-data

Conversation

@vtxe02023274

Copy link
Copy Markdown
Contributor

Two problems with malformed base64 layer data:

  • Truncated zlib data never stops. When the compressed data ends early, inflate() returns Z_BUF_ERROR. decompress() treats it like Z_OK and 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.
  • Short layer data is read past the end. parseBase64() reads width * height * 4 bytes no matter how many were decoded or inflated. If the data is shorter (uncompressed or zlib), this reads past the end of byteData. It now reads only the bytes that are there and logs a warning. The same change removes the expectedSize - 3u underflow when the tile count is 0.

Repro: a 4x2 map loaded with loadFromString, layer data built from tile IDs 1..8.

layer data before after
full zlib 8 tiles 8 tiles
zlib cut in half memory grows until allocation fails error logged, layer skipped
zlib of 2 tiles out-of-bounds read (_GLIBCXX_ASSERTIONS aborts in operator[]) warning, 2 tiles
uncompressed, 2 tiles out-of-bounds read warning, 2 tiles

Tested on Linux with GCC 11, default build (miniz), with ASan, UBSan and _GLIBCXX_ASSERTIONS. Every .tmx in 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.

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.
@fallahn
fallahn merged commit 881c626 into fallahn:master Nov 23, 2026
9 checks passed
@fallahn

fallahn commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Wow that was a pretty bad bug, thanks for finding and fixing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants