Skip to content

Commit

Permalink
huff0: fix handling errors in Decompress1X asm (#604)
Browse files Browse the repository at this point in the history
Add a regression test.
  • Loading branch information
WojciechMula committed May 26, 2022
1 parent ab138a9 commit 416d716
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
2 changes: 2 additions & 0 deletions huff0/_generate/gen.go
Expand Up @@ -495,6 +495,8 @@ func (d decompress1x) generateProcedure(name string) {
MOVQ(I64(-1), tmp)
Store(tmp, ctx.Field("decoded"))
}

RET()
}

func (d decompress1x) decompress(id int, br *bitReader, peekBits, dt reg.GPVirtual, out reg.Register) {
Expand Down
2 changes: 2 additions & 0 deletions huff0/decompress_amd64.s
Expand Up @@ -765,6 +765,7 @@ error_max_decoded_size_exeeded:
MOVQ ctx+0(FP), AX
MOVQ $-1, CX
MOVQ CX, 40(AX)
RET

// func decompress1x_main_loop_bmi2(ctx *decompress1xContext)
// Requires: BMI2
Expand Down Expand Up @@ -861,3 +862,4 @@ error_max_decoded_size_exeeded:
MOVQ ctx+0(FP), AX
MOVQ $-1, CX
MOVQ CX, 40(AX)
RET
40 changes: 40 additions & 0 deletions huff0/decompress_test.go
Expand Up @@ -3,7 +3,10 @@ package huff0
import (
"bytes"
"fmt"
"io/ioutil"
"testing"

"github.com/klauspost/compress/zip"
)

func TestDecompress1X(t *testing.T) {
Expand Down Expand Up @@ -96,6 +99,43 @@ func TestDecompress1X(t *testing.T) {
}
}

func TestDecompress1XRegression(t *testing.T) {
data, err := ioutil.ReadFile("testdata/decompress1x_regression.zip")
if err != nil {
t.Fatal(err)
}
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatal(err)
}
for _, tt := range zr.File {
if tt.UncompressedSize64 == 0 {
continue
}
rc, err := tt.Open()
if err != nil {
t.Fatal(err)
}
data, err := ioutil.ReadAll(rc)
if err != nil {
t.Fatal(err)
}

t.Run(tt.Name, func(t *testing.T) {
s, rem, err := ReadTable(data, nil)
if err != nil {
t.Fatal(err)
}
_, err = s.Decompress1X(rem)
if err == nil {
t.Fatal("expected error to be returned")
}

t.Logf("returned error: %s", err)
})
}
}

func TestDecompress4X(t *testing.T) {
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
Expand Down
Binary file added huff0/testdata/decompress1x_regression.zip
Binary file not shown.

0 comments on commit 416d716

Please sign in to comment.