The Compress and UnCompress classes have undocumented restrictions on when the buffers passed to their `compress` and `uncompress` methods can be re-used. This needs to be fixed so that people are not accidentally creating or reading corrupted data.
Comment #1 by zan77137 — 2020-09-15T11:49:46Z
This issue seems to be implementation problems rather than undocumented restrictions: since the argument of compress/uncompress is requiring const(void)[], the function has to take into account the possibility that the buffer may be changed.
Specifically, the following code does not work:
-----------------------------------------------
auto compressed = appender!(ubyte[])();
scope compress = new Compress(HeaderFormat.gzip);
char[128] buffer;
foreach (e; ["abc", "abcdefghijk"]) {
auto line = sformat!"%s\n"(buffer, e);
// Invalid buffer is held by Compress
const compressedLine = compress.compress(line);
compressed.put(cast(const(ubyte)[]) compressedLine);
}
compressed.put(cast(const(ubyte)[]) compress.flush());
-----------------------------------------------
To solve this, there are two policies:
1. Change the type of argument to immutable(void)[] (this is a breaking change)
2. Copy the argument's buffer, and manage the life of the copied buffer in Compress/UnCompress.
Comment #2 by robert.schadek — 2024-12-01T16:33:29Z