From b2d7bff70aab734dd1ba900fd45d811ab681e940 Mon Sep 17 00:00:00 2001 From: greatroar <61184462+greatroar@users.noreply.github.com> Date: Mon, 19 Dec 2022 14:49:13 +0100 Subject: [PATCH] zstd: Trigger BCE by switching on lengths (#716) This patches reduces the number of bounds checks in zstd, as reported by go build -gcflags="-d=ssa/check_bce/debug=1" |& wc -l from 723 to 693. --- zstd/decodeheader.go | 4 ++-- zstd/framedec.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/zstd/decodeheader.go b/zstd/decodeheader.go index 1f5d41bf41..f6a240970d 100644 --- a/zstd/decodeheader.go +++ b/zstd/decodeheader.go @@ -152,7 +152,7 @@ func (h *Header) Decode(in []byte) error { } b, in = in[:size], in[size:] h.HeaderSize += int(size) - switch size { + switch len(b) { case 1: h.DictionaryID = uint32(b[0]) case 2: @@ -182,7 +182,7 @@ func (h *Header) Decode(in []byte) error { } b, in = in[:fcsSize], in[fcsSize:] h.HeaderSize += int(fcsSize) - switch fcsSize { + switch len(b) { case 1: h.FrameContentSize = uint64(b[0]) case 2: diff --git a/zstd/framedec.go b/zstd/framedec.go index 6cb3b4e55f..65984bf07c 100644 --- a/zstd/framedec.go +++ b/zstd/framedec.go @@ -167,7 +167,7 @@ func (d *frameDec) reset(br byteBuffer) error { return err } var id uint32 - switch size { + switch len(b) { case 1: id = uint32(b[0]) case 2: @@ -204,7 +204,7 @@ func (d *frameDec) reset(br byteBuffer) error { println("Reading Frame content", err) return err } - switch fcsSize { + switch len(b) { case 1: d.FrameContentSize = uint64(b[0]) case 2: