Sfoglia il codice sorgente

use function attributes to ensure good use of APIs

Julius Pfrommer 9 anni fa
parent
commit
dc7192ba84

+ 12 - 0
include/ua_config.h.in

@@ -84,5 +84,17 @@
 # define UA_RESTRICT restrict
 #endif
 
+/* Function attributes */
+#ifdef __GNUC__
+# define UA_FUNC_ATTR_MALLOC __attribute__((malloc))
+# define UA_FUNC_ATTR_PURE __attribute__ ((pure))
+# define UA_FUNC_ATTR_CONST __attribute__((const))
+# define UA_FUNC_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
+#else
+# define UA_FUNC_ATTR_MALLOC
+# define UA_FUNC_ATTR_PURE
+# define UA_FUNC_ATTR_CONST
+# define UA_FUNC_ATTR_WARN_UNUSED_RESULT
+#endif
 
 #endif /* UA_CONFIG_H_ */

+ 1 - 1
src/ua_types_encoding_binary.c

@@ -781,7 +781,7 @@ UA_StatusCode UA_Variant_encodeBinary(UA_Variant const *src, UA_ByteString *dst,
             /* Jump back and print the length of the extension object */
             UA_Int32 encodingLength = *offset - oldoffset;
             oldoffset -= 4;
-            UA_Int32_encodeBinary(&encodingLength, dst, &oldoffset);
+            retval |= UA_Int32_encodeBinary(&encodingLength, dst, &oldoffset);
         }
         ptr += memSize;
     }

+ 3 - 3
src/ua_types_encoding_binary.h

@@ -74,10 +74,10 @@ UA_TYPE_BINARY_ENCODING(UA_Variant)
 UA_TYPE_BINARY_ENCODING(UA_DiagnosticInfo)
 
 UA_StatusCode UA_encodeBinary(const void *src, const UA_DataType *dataType, UA_ByteString *dst,
-                              size_t *UA_RESTRICT offset);
+                              size_t *UA_RESTRICT offset) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
 UA_StatusCode UA_decodeBinary(const UA_ByteString *src, size_t * UA_RESTRICT offset, void *dst,
-                              const UA_DataType *dataType);
-size_t UA_calcSizeBinary(const void *p, const UA_DataType *dataType);
+                              const UA_DataType *dataType) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
+size_t UA_calcSizeBinary(const void *p, const UA_DataType *dataType) UA_FUNC_ATTR_PURE;
 
 /// @}
 

+ 2 - 1
tests/check_memory.c

@@ -99,7 +99,8 @@ START_TEST(decodeShallFailWithTruncatedBufferButSurvive) {
 	pos = 0;
 	msg1.length = pos / 2;
 	//fprintf(stderr,"testing %s with half buffer\n",UA_TYPES[_i].name);
-	UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
+	retval = UA_decodeBinary(&msg1, &pos, obj2, &UA_TYPES[_i]);
+	ck_assert_int_ne(retval, UA_STATUSCODE_GOOD);
 	//then
 	// finally
 	//fprintf(stderr,"delete %s with half buffer\n",UA_TYPES[_i].name);