godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
88.94k stars 20.17k forks source link

Garbage byte when decompressing files #95014

Open Ratio2 opened 1 month ago

Ratio2 commented 1 month ago

Tested versions

Reproducible in: 4.1.2, 4.1.4, 4.2.2, 4.3rc1, master

System information

Any

Issue description

If the size of the original data for compression is a multiple of 4096, then when reading the data, an extra random byte appears at the end. The decompression code does not take into account that the last block may be of size zero.

Steps to reproduce

    var file := FileAccess.open_compressed("test.bin", FileAccess.WRITE)
    var data := PackedByteArray()
    var size := 4 * 1024
    data.resize(size)
    file.store_buffer(data)
    file.close()

    file = FileAccess.open_compressed("test.bin", FileAccess.READ)
    data = file.get_buffer(size * 2)
    assert(data.size() == size)

Minimal reproduction project (MRP)

N/A

AThousandShips commented 1 month ago

Note that this does not happen if you try to read the actual number of bytes with data = file.get_buffer(size), only when you try to read more than the data in the file, but it is specific to compressed files

This is due to past changes to handle some variable storage, but seems to handle things incorrectly in some cases, will look at a fix

Though I think specifically the issue is due to an error arising when doing modulo on the result, will test

Edit: Seems to work, running tests and will open a PR if it works correctly