From 21dc27b115f46bfd340a9731c249f5f7b122413b Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Tue, 24 Nov 2020 14:13:09 +0100 Subject: [PATCH] internal/ops: remove some bounds checks Currently BCE is unable to understand that the accesses in the code are safe. Added an explicit slice to make the length bounds obvious. Signed-off-by: Egon Elbre --- internal/ops/ops.go | 14 ++++++-------- internal/ops/reader.go | 2 ++ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/ops/ops.go b/internal/ops/ops.go index c56b9d6b..da389d9e 100644 --- a/internal/ops/ops.go +++ b/internal/ops/ops.go @@ -24,6 +24,7 @@ func (q Quad) Transform(t f32.Affine2D) Quad { } func EncodeQuad(d []byte, q Quad) { + d = d[:24] bo := binary.LittleEndian bo.PutUint32(d[0:], math.Float32bits(q.From.X)) bo.PutUint32(d[4:], math.Float32bits(q.From.Y)) @@ -34,6 +35,7 @@ func EncodeQuad(d []byte, q Quad) { } func DecodeQuad(d []byte) (q Quad) { + d = d[:24] bo := binary.LittleEndian q.From.X = math.Float32frombits(bo.Uint32(d[0:])) q.From.Y = math.Float32frombits(bo.Uint32(d[4:])) @@ -44,17 +46,13 @@ func DecodeQuad(d []byte) (q Quad) { return } -func DecodeTransform(d []byte) (t f32.Affine2D) { - if opconst.OpType(d[0]) != opconst.TypeTransform { +func DecodeTransform(data []byte) (t f32.Affine2D) { + if opconst.OpType(data[0]) != opconst.TypeTransform { panic("invalid op") } - if len(d) < 1+6*4 { - panic("too short buffer") - } - return decodeAffine2D(d[1:]) -} + data = data[1:] + data = data[:4*6] -func decodeAffine2D(data []byte) f32.Affine2D { bo := binary.LittleEndian a := math.Float32frombits(bo.Uint32(data)) b := math.Float32frombits(bo.Uint32(data[4*1:])) diff --git a/internal/ops/reader.go b/internal/ops/reader.go index 5d713db3..0bab3a06 100644 --- a/internal/ops/reader.go +++ b/internal/ops/reader.go @@ -143,6 +143,7 @@ func (op *opMacroDef) decode(data []byte) { panic("invalid op") } bo := binary.LittleEndian + data = data[:9] dataIdx := int(int32(bo.Uint32(data[1:]))) refsIdx := int(int32(bo.Uint32(data[5:]))) *op = opMacroDef{ @@ -157,6 +158,7 @@ func (m *macroOp) decode(data []byte, refs []interface{}) { if opconst.OpType(data[0]) != opconst.TypeCall { panic("invalid op") } + data = data[:9] bo := binary.LittleEndian dataIdx := int(int32(bo.Uint32(data[1:]))) refsIdx := int(int32(bo.Uint32(data[5:])))