Skip to content

Commit

Permalink
refactor(core): Encode string as empty (instead of NULL) when the dat…
Browse files Browse the repository at this point in the history
…a-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).
  • Loading branch information
jpfr committed Nov 3, 2024
1 parent 68bf6a3 commit a9736eb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ua_types_encoding_binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
48 changes: 48 additions & 0 deletions tests/check_types_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a9736eb

Please sign in to comment.