From 38c5a6c7e0709426c0075964cd3edc1c17ed7994 Mon Sep 17 00:00:00 2001 From: Caleb Case Date: Thu, 16 Dec 2021 12:43:29 -0500 Subject: [PATCH] Make value retrieval not panic --- lib/block.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/block.go b/lib/block.go index 64eef57..c8d9ed0 100644 --- a/lib/block.go +++ b/lib/block.go @@ -43,6 +43,13 @@ func (b *block) Xor(other *block) { func (b *block) Value() []byte { size := binary.BigEndian.Uint64(b.Data[:8]) + // This truncates the value. The user of value should be comparing it + // to the value hash (done elsewhere) and that will (usually) catch + // this error. + if 8+size > uint64(len(b.Data)) { + size = uint64(len(b.Data)) - 8 + } + return b.Data[8 : 8+size] }