From 7aff4a6c5841b2ec39ff4c96e8b654f78c7026d8 Mon Sep 17 00:00:00 2001 From: Eiichiro Watanabe Date: Fri, 3 Feb 2017 17:39:13 +0900 Subject: packet: Avoid panic() during decoding BMP messages --- packet/bmp/bmp.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'packet') diff --git a/packet/bmp/bmp.go b/packet/bmp/bmp.go index 084a0d16..19e2840e 100644 --- a/packet/bmp/bmp.go +++ b/packet/bmp/bmp.go @@ -540,9 +540,15 @@ const ( BMP_MSG_TERMINATION ) -func ParseBMPMessage(data []byte) (*BMPMessage, error) { - msg := &BMPMessage{} - err := msg.Header.DecodeFromBytes(data) +func ParseBMPMessage(data []byte) (msg *BMPMessage, err error) { + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("not all data bytes are available") + } + }() + + msg = &BMPMessage{} + err = msg.Header.DecodeFromBytes(data) if err != nil { return nil, err } -- cgit v1.2.3