From 7b46f9bb860853a2fb44518c260f745c5e012667 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Thu, 29 Aug 2019 13:24:03 +0300 Subject: [PATCH] util: add error check to Read/WriteVarUint Fixes possibility of bogus reads/writes. Suggested by @im-kulikov. --- pkg/util/binaryReader.go | 4 ++++ pkg/util/binaryWriter.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/pkg/util/binaryReader.go b/pkg/util/binaryReader.go index f13879549..fe92ae61a 100644 --- a/pkg/util/binaryReader.go +++ b/pkg/util/binaryReader.go @@ -33,6 +33,10 @@ func (r *BinReader) ReadBE(v interface{}) { // ReadVarUint reads a variable-length-encoded integer from the // underlying reader func (r *BinReader) ReadVarUint() uint64 { + if r.Err != nil { + return 0 + } + var b uint8 r.Err = binary.Read(r.R, binary.LittleEndian, &b) diff --git a/pkg/util/binaryWriter.go b/pkg/util/binaryWriter.go index 39acf9a41..8f2c5229d 100644 --- a/pkg/util/binaryWriter.go +++ b/pkg/util/binaryWriter.go @@ -32,6 +32,10 @@ func (w *BinWriter) WriteBE(v interface{}) { // WriteVarUint writes a uint64 into the underlying writer using variable-length encoding func (w *BinWriter) WriteVarUint(val uint64) { + if w.Err != nil { + return + } + if val < 0 { w.Err = errors.New("value out of range") return