Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zstd: fix crash in decoding #565

Merged
merged 4 commits into from Apr 24, 2022

Conversation

WojciechMula
Copy link
Contributor

@WojciechMula WojciechMula commented Apr 22, 2022

Fixes #562 and #563.

@WojciechMula
Copy link
Contributor Author

The file causes also some problems in Go code. For invocation go test -tags noasm -run TestDecoderRegression we have:

--- FAIL: TestDecoderRegression (0.24s)
    --- FAIL: TestDecoderRegression/Reader-05d57667c86a14c61262c925d95d398c6e921012 (0.00s)
        decoder_test.go:671: Received: 56821 output (177670) bigger than max block size (45056)
        decoder_test.go:686: Fresh Reader received: 56821 output (165905) bigger than max block size (45056)
        decoder_test.go:689: output (177670) bigger than max block size (45056) != output (165905) bigger than max block size (45056)

@klauspost
Copy link
Owner

You can add this to fix the Go code:

diff --git a/zstd/seqdec.go b/zstd/seqdec.go
index 477b1e1..a635f84 100644
--- a/zstd/seqdec.go
+++ b/zstd/seqdec.go
@@ -299,7 +299,7 @@ func (s *sequenceDecs) decodeSync(hist []byte) error {
 		}
 		size := ll + ml + len(out)
 		if size-startSize > maxBlockSize {
-			return fmt.Errorf("output (%d) bigger than max block size (%d)", size, maxBlockSize)
+			return fmt.Errorf("output (%d) bigger than max block size (%d)", size-startSize, maxBlockSize)
 		}
 		if size > cap(out) {
 			// Not enough size, which can happen under high volume block streaming conditions
@@ -410,7 +410,7 @@ func (s *sequenceDecs) decodeSync(hist []byte) error {
 
 	// Check if space for literals
 	if len(s.literals)+len(s.out)-startSize > maxBlockSize {
-		return fmt.Errorf("output (%d) bigger than max block size (%d)", len(s.out), maxBlockSize)
+		return fmt.Errorf("output (%d) bigger than max block size (%d)", len(s.literals)+len(s.out)-startSize, maxBlockSize)
 	}
 
 	// Add final literals

@WojciechMula WojciechMula marked this pull request as ready for review April 22, 2022 20:35
@WojciechMula
Copy link
Contributor Author

You can add this to fix the Go code:

Thank you very much! It helped.

@klauspost klauspost merged commit 2f23638 into klauspost:master Apr 24, 2022
@WojciechMula WojciechMula deleted the fix-crash-issue-563 branch April 24, 2022 15:31
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.

crash sequenceDecs_decodeSync_bmi2
2 participants