libresource: handle message parsing errors consistently.
authorIsmo Puustinen <ismo.puustinen@intel.com>
Mon, 22 Sep 2014 08:08:53 +0000 (11:08 +0300)
committerIsmo Puustinen <ismo.puustinen@intel.com>
Mon, 22 Sep 2014 08:09:44 +0000 (11:09 +0300)
src/plugins/resource-native/libmurphy-resource/message.c
src/plugins/resource-native/libmurphy-resource/message.h
src/plugins/resource-native/libmurphy-resource/resource.c

index a26b08b..8ae161a 100644 (file)
@@ -133,15 +133,15 @@ bool fetch_seqno(mrp_msg_t *msg, void **pcursor, uint32_t *pseqno)
         tag != RESPROTO_SEQUENCE_NO || type != MRP_MSG_FIELD_UINT32)
     {
         *pseqno = 0;
-        return -1;
+        return false;
     }
 
     *pseqno = value.u32;
-    return 0;
+    return true;
 }
 
 
-int fetch_request(mrp_msg_t *msg, void **pcursor, uint16_t *preqtype)
+bool fetch_request(mrp_msg_t *msg, void **pcursor, uint16_t *preqtype)
 {
     uint16_t tag;
     uint16_t type;
@@ -152,11 +152,11 @@ int fetch_request(mrp_msg_t *msg, void **pcursor, uint16_t *preqtype)
         tag != RESPROTO_REQUEST_TYPE || type != MRP_MSG_FIELD_UINT16)
     {
         *preqtype = 0;
-        return -1;
+        return false;
     }
 
     *preqtype = value.u16;
-    return 0;
+    return true;
 }
 
 
@@ -180,8 +180,9 @@ bool fetch_status(mrp_msg_t *msg, void **pcursor, int *pstatus)
 
 
 
-int fetch_attribute_array(mrp_msg_t *msg, void **pcursor,
-                                 size_t dim, mrp_res_attribute_t *arr)
+bool fetch_attribute_array(mrp_msg_t *msg, void **pcursor,
+                                 size_t dim, mrp_res_attribute_t *arr,
+                                 int *n_arr)
 {
     mrp_res_attribute_t *attr;
     uint16_t tag;
@@ -189,6 +190,7 @@ int fetch_attribute_array(mrp_msg_t *msg, void **pcursor,
     mrp_msg_value_t value;
     size_t size;
     size_t i;
+    *n_arr = 0;
 
     i = 0;
 
@@ -199,7 +201,7 @@ int fetch_attribute_array(mrp_msg_t *msg, void **pcursor,
         if (tag  != RESPROTO_ATTRIBUTE_NAME ||
             type != MRP_MSG_FIELD_STRING ||
             i >= dim - 1) {
-            return -1;
+            return false;
         }
 
         attr = arr + i++;
@@ -207,7 +209,7 @@ int fetch_attribute_array(mrp_msg_t *msg, void **pcursor,
 
         if (!mrp_msg_iterate(msg, pcursor, &tag, &type, &value, &size) ||
             tag != RESPROTO_ATTRIBUTE_VALUE) {
-            return -1;
+            return false;
         }
 
         switch (type) {
@@ -228,13 +230,15 @@ int fetch_attribute_array(mrp_msg_t *msg, void **pcursor,
             attr->floating = value.dbl;
             break;
         default:
-            return -1;
+            return false;
         }
     }
 
     memset(arr + i, 0, sizeof(mrp_res_attribute_t));
 
-    return i;
+    *n_arr = i;
+
+    return TRUE;
 }
 
 
@@ -297,10 +301,10 @@ mrp_res_resource_set_t *resource_query_response(mrp_res_context_t *cx,
         dim = 0;
 
         while (fetch_resource_name(msg, pcursor, &rdef[dim].name)) {
-            int n_attrs = fetch_attribute_array(msg, pcursor, ATTRIBUTE_MAX+1,
-                    attrs);
+            int n_attrs = 0;
 
-            if (n_attrs < 0)
+            if (!fetch_attribute_array(msg, pcursor, ATTRIBUTE_MAX+1,
+                    attrs, &n_attrs))
                 goto failed;
 
             if (!(rdef[dim].attrs = mrp_attribute_array_dup(n_attrs, attrs))) {
index 008e6b8..5b37212 100644 (file)
@@ -50,12 +50,13 @@ bool fetch_mrp_str_array(mrp_msg_t *msg, void **pcursor,
 
 bool fetch_seqno(mrp_msg_t *msg, void **pcursor, uint32_t *pseqno);
 
-int fetch_request(mrp_msg_t *msg, void **pcursor, uint16_t *preqtype);
+bool fetch_request(mrp_msg_t *msg, void **pcursor, uint16_t *preqtype);
 
 bool fetch_status(mrp_msg_t *msg, void **pcursor, int *pstatus);
 
-int fetch_attribute_array(mrp_msg_t *msg, void **pcursor,
-                                 size_t dim, mrp_res_attribute_t *arr);
+bool fetch_attribute_array(mrp_msg_t *msg, void **pcursor,
+                                 size_t dim, mrp_res_attribute_t *arr,
+                                 int *n_arr);
 
 bool fetch_resource_name(mrp_msg_t *msg, void **pcursor,
                                 const char **pname);
index 0655333..57a18b0 100644 (file)
@@ -143,9 +143,8 @@ static void resource_event(mrp_msg_t *msg,
 
         mrp_res_info("data for '%s': %d", res->name, resid);
 
-        n_attrs = fetch_attribute_array(msg, pcursor, ATTRIBUTE_MAX + 1, attrs);
-
-        if (n_attrs < 0) {
+        if (!fetch_attribute_array(msg, pcursor, ATTRIBUTE_MAX + 1, attrs,
+                &n_attrs)) {
             mrp_res_error("failed to read attributes from message");
             goto ignore;
         }
@@ -253,8 +252,8 @@ static void recvfrom_msg(mrp_transport_t *transp, mrp_msg_t *msg,
     MRP_UNUSED(addr);
     MRP_UNUSED(addrlen);
 
-    if (fetch_seqno(msg, &cursor, &seqno) < 0 ||
-                fetch_request(msg, &cursor, &req) < 0)
+    if (!fetch_seqno(msg, &cursor, &seqno) ||
+                !fetch_request(msg, &cursor, &req))
         goto error;
 
     mrp_res_info("received message %d for %p", req, cx);