Raise an exception on bad buffer allocations
[ric-plt/lib/rmr.git] / src / bindings / rmr-python / rmr / rmr.py
index 8c9ff5a..85c9ee8 100644 (file)
@@ -19,6 +19,7 @@ import json
 from ctypes import RTLD_GLOBAL, Structure, c_int, POINTER, c_char, c_char_p, c_void_p, memmove, cast
 from ctypes import CDLL
 from ctypes import create_string_buffer
+from rmr.exceptions import BadBufferAllocation
 
 # https://docs.python.org/3.7/library/ctypes.html
 # https://stackoverflow.com/questions/2327344/ctypes-loading-a-c-shared-library-that-has-dependencies/30845750#30845750
@@ -219,7 +220,12 @@ def rmr_alloc_msg(vctx, size):
     Refer to the rmr C documentation for rmr_alloc_msg
     extern rmr_mbuf_t* rmr_alloc_msg(void* vctx, int size)
     """
-    return _rmr_alloc_msg(vctx, size)
+    sbuf = _rmr_alloc_msg(vctx, size)
+    try:
+        sbuf.contents
+    except ValueError:
+        raise BadBufferAllocation
+    return sbuf
 
 
 _rmr_free_msg = rmr_c_lib.rmr_free_msg
@@ -437,11 +443,8 @@ def message_summary(ptr_mbuf):
     dict:
         dict message summary
     """
-    if ptr_mbuf.contents.len > RMR_MAX_RCV_BYTES:
-        return "Malformed message: message length is greater than the maximum possible"
-
     return {
-        "payload": get_payload(ptr_mbuf),
+        "payload": get_payload(ptr_mbuf) if ptr_mbuf.contents.state == RMR_OK else None,
         "payload length": ptr_mbuf.contents.len,
         "message type": ptr_mbuf.contents.mtype,
         "subscription id": ptr_mbuf.contents.sub_id,