# zstd.Decompress: smarter rebase when discarding \(\#30891\) · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/fa988e88ed21485830a70276b5c7567efb122f80)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: fa988e88ed21485830a70276b5c7567efb122f80

Requested commit: fa988e88ed21485830a70276b5c7567efb122f80

Commit: fa988e88ed21485830a70276b5c7567efb122f80

Tree: ecd73646095246276a7ab7cbdfe6aa799c911195

Author: mercenary

Committer: Andrew Kelley

## Message

```
zstd.Decompress: smarter rebase when discarding (#30891)

The call to `rebase` in `discardIndirect` and `discardDirect` was inappropriate. As `rebase` expects the `capacity` parameter to exclude the sliding window, this call was asking for ANOTHER `d.window_len` bytes. This was impossible to fulfill with a buffer smaller than 2*`d.window_len`, and caused [#25764](https://github.com/ziglang/zig/issues/25764).

This PR adds a basic test to do a discard (which does trigger [#25764](https://github.com/ziglang/zig/issues/25764)), and rebases only as much as is required to make the discard succeed ([or no rebase at all](https://github.com/ziglang/zig/issues/25764#issuecomment-3484716253)). That means: ideally rebase to fit `limit`, or if the buffer is too small, as much as possible.

I must say, `discardDirect` does not make much sense to me, but I replaced it anyway. `rebaseForDiscard` works fine with `d.reader.buffer.len == 0`. Let me know if anything should be changed.

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30891
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
Co-authored-by: mercenary <mercenary@noreply.codeberg.org>
Co-committed-by: mercenary <mercenary@noreply.codeberg.org>

```

## Parents

- [aa38f07c5173f9722ebfb933058a2a032c2badf3](https://git.cafe/gitcafe/zig/commit/aa38f07c5173f9722ebfb933058a2a032c2badf3?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/fa988e88ed21485830a70276b5c7567efb122f80?format=markdown)
