Message decoding exception handler fix

- was unfinished and caused the object processor thread to crash
This commit is contained in:
Peter Šurda 2018-02-13 13:24:37 +01:00
parent 3ad94cb4aa
commit f9a648d720
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
2 changed files with 25 additions and 11 deletions

View File

@ -509,7 +509,10 @@ class objectProcessor(threading.Thread):
if toLabel == '': if toLabel == '':
toLabel = toAddress toLabel = toAddress
try:
decodedMessage = helper_msgcoding.MsgDecode(messageEncodingType, message) decodedMessage = helper_msgcoding.MsgDecode(messageEncodingType, message)
except helper_msgcoding.MsgDecodeException:
return
subject = decodedMessage.subject subject = decodedMessage.subject
body = decodedMessage.body body = decodedMessage.body
@ -761,7 +764,10 @@ class objectProcessor(threading.Thread):
sendersAddressVersion, sendersStream, calculatedRipe) sendersAddressVersion, sendersStream, calculatedRipe)
logger.debug('fromAddress: ' + fromAddress) logger.debug('fromAddress: ' + fromAddress)
try:
decodedMessage = helper_msgcoding.MsgDecode(messageEncodingType, message) decodedMessage = helper_msgcoding.MsgDecode(messageEncodingType, message)
except helper_msgcoding.MsgDecodeException:
return
subject = decodedMessage.subject subject = decodedMessage.subject
body = decodedMessage.body body = decodedMessage.body

View File

@ -21,7 +21,15 @@ BITMESSAGE_ENCODING_SIMPLE = 2
BITMESSAGE_ENCODING_EXTENDED = 3 BITMESSAGE_ENCODING_EXTENDED = 3
class DecompressionSizeException(Exception): class MsgEncodeException(Exception):
pass
class MsgDecodeException(Exception):
pass
class DecompressionSizeException(MsgDecodeException):
def __init__(self, size): def __init__(self, size):
self.size = size self.size = size
@ -38,7 +46,7 @@ class MsgEncode(object):
elif self.encoding == BITMESSAGE_ENCODING_TRIVIAL: elif self.encoding == BITMESSAGE_ENCODING_TRIVIAL:
self.encodeTrivial(message) self.encodeTrivial(message)
else: else:
raise ValueError("Unknown encoding %i" % (encoding)) raise MsgEncodeException("Unknown encoding %i" % (encoding))
def encodeExtended(self, message): def encodeExtended(self, message):
try: try:
@ -46,10 +54,10 @@ class MsgEncode(object):
self.data = zlib.compress(msgpack.dumps(msgObj.encode(message)), 9) self.data = zlib.compress(msgpack.dumps(msgObj.encode(message)), 9)
except zlib.error: except zlib.error:
logger.error("Error compressing message") logger.error("Error compressing message")
raise raise MsgEncodeException("Error compressing message")
except msgpack.exceptions.PackException: except msgpack.exceptions.PackException:
logger.error("Error msgpacking message") logger.error("Error msgpacking message")
raise raise MsgEncodeException("Error msgpacking message")
self.length = len(self.data) self.length = len(self.data)
def encodeSimple(self, message): def encodeSimple(self, message):
@ -85,7 +93,7 @@ class MsgDecode(object):
data = dc.unconsumed_tail data = dc.unconsumed_tail
except zlib.error: except zlib.error:
logger.error("Error decompressing message") logger.error("Error decompressing message")
raise raise MsgDecodeException("Error decompressing message")
else: else:
raise DecompressionSizeException(len(tmp)) raise DecompressionSizeException(len(tmp))
@ -94,21 +102,21 @@ class MsgDecode(object):
except (msgpack.exceptions.UnpackException, except (msgpack.exceptions.UnpackException,
msgpack.exceptions.ExtraData): msgpack.exceptions.ExtraData):
logger.error("Error msgunpacking message") logger.error("Error msgunpacking message")
raise raise MsgDecodeException("Error msgunpacking message")
try: try:
msgType = tmp[""] msgType = tmp[""]
except KeyError: except KeyError:
logger.error("Message type missing") logger.error("Message type missing")
raise raise MsgDecodeException("Message type missing")
msgObj = messagetypes.constructObject(tmp) msgObj = messagetypes.constructObject(tmp)
if msgObj is None: if msgObj is None:
raise ValueError("Malformed message") raise MsgDecodeException("Malformed message")
try: try:
msgObj.process() msgObj.process()
except: except:
raise ValueError("Malformed message") raise MsgDecodeException("Malformed message")
if msgType == "message": if msgType == "message":
self.subject = msgObj.subject self.subject = msgObj.subject
self.body = msgObj.body self.body = msgObj.body