From a9736ebb0beda4bf7e3792876c66991b09734b79 Mon Sep 17 00:00:00 2001 From: Julius Pfrommer Date: Sun, 3 Nov 2024 22:03:14 +0100 Subject: [PATCH] refactor(core): Encode string as empty (instead of NULL) when the data-pointer is > NULL Before we required the data pointer to be exactly the UA_EMPTY_ARRAY_SENTINEL. But there can be situations where the data pointers goes to "" (empty string with the \0 ending). Then the string should be empty -- and not null (length -1). --- src/ua_types_encoding_binary.c | 2 +- tests/check_types_builtin.c | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/ua_types_encoding_binary.c b/src/ua_types_encoding_binary.c index 3ed6a59e9dd..b134c4e8329 100644 --- a/src/ua_types_encoding_binary.c +++ b/src/ua_types_encoding_binary.c @@ -436,7 +436,7 @@ Array_encodeBinary(const void *src, size_t length, const UA_DataType *type, Ctx return UA_STATUSCODE_BADINTERNALERROR; if(length > 0) signed_length = (i32)length; - else if(src == UA_EMPTY_ARRAY_SENTINEL) + else if(src >= UA_EMPTY_ARRAY_SENTINEL) /* src != NULL */ signed_length = 0; /* Encode the array length */ diff --git a/tests/check_types_builtin.c b/tests/check_types_builtin.c index 7570de39df1..5b99f549b25 100644 --- a/tests/check_types_builtin.c +++ b/tests/check_types_builtin.c @@ -946,6 +946,53 @@ START_TEST(UA_String_encodeShallWorkOnExample) { } END_TEST +START_TEST(UA_String_encodeShallWorkOnEmpty) { + // given + UA_String src = UA_STRING_NULL; + UA_Byte data[] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, + 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, + 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 }; + UA_ByteString dst = { 24, data }; + UA_Byte *pos = dst.data; + const UA_Byte *end = &dst.data[dst.length]; + + // when + UA_StatusCode retval = UA_String_encodeBinary(&src, &pos, end); + + // then + ck_assert_int_eq(retval, UA_STATUSCODE_GOOD); + ck_assert_int_eq(data[0], 0xFF); + + /////// + + // given + src = UA_STRING(""); + pos = dst.data; + end = &dst.data[dst.length]; + + // when + retval = UA_String_encodeBinary(&src, &pos, end); + + // then + ck_assert_int_eq(retval, UA_STATUSCODE_GOOD); + ck_assert_int_eq(data[0], 0x00); + + /////// + + // given + src.data = (UA_Byte*)""; + pos = dst.data; + end = &dst.data[dst.length]; + + // when + retval = UA_String_encodeBinary(&src, &pos, end); + + // then + ck_assert_int_eq(retval, UA_STATUSCODE_GOOD); + ck_assert_int_eq(data[0], 0x00); +} +END_TEST + START_TEST(UA_ExpandedNodeId_encodeShallWorkOnExample) { // given UA_ExpandedNodeId src = UA_EXPANDEDNODEID_NUMERIC(0, 15); @@ -1757,6 +1804,7 @@ static Suite *testSuite_builtin(void) { tcase_add_test(tc_encode, UA_Float_encodeShallWorkOnExample); tcase_add_test(tc_encode, UA_Double_encodeShallWorkOnExample); tcase_add_test(tc_encode, UA_String_encodeShallWorkOnExample); + tcase_add_test(tc_encode, UA_String_encodeShallWorkOnEmpty); tcase_add_test(tc_encode, UA_ExpandedNodeId_encodeShallWorkOnExample); tcase_add_test(tc_encode, UA_DataValue_encodeShallWorkOnExampleWithoutVariant); tcase_add_test(tc_encode, UA_DataValue_encodeShallWorkOnExampleWithVariant);