diff --git a/erpc_c/infra/erpc_basic_codec.cpp b/erpc_c/infra/erpc_basic_codec.cpp index 88bd183f3..54c8df89e 100644 --- a/erpc_c/infra/erpc_basic_codec.cpp +++ b/erpc_c/infra/erpc_basic_codec.cpp @@ -37,14 +37,7 @@ void BasicCodec::writeData(const void *value, uint32_t length) { if (isStatusOk()) { - if (value != NULL) - { - m_status = m_cursor.write(value, length); - } - else - { - m_status = kErpcStatus_MemoryError; - } + m_status = m_cursor.write(value, length); } } @@ -202,14 +195,7 @@ void BasicCodec::readData(void *value, uint32_t length) { if (isStatusOk()) { - if (value != NULL) - { - m_status = m_cursor.read(value, length); - } - else - { - m_status = kErpcStatus_MemoryError; - } + m_status = m_cursor.read(value, length); } } diff --git a/erpc_c/infra/erpc_message_buffer.cpp b/erpc_c/infra/erpc_message_buffer.cpp index d5588ee28..aff8c0da8 100644 --- a/erpc_c/infra/erpc_message_buffer.cpp +++ b/erpc_c/infra/erpc_message_buffer.cpp @@ -102,7 +102,11 @@ erpc_status_t MessageBuffer::Cursor::read(void *data, uint32_t length) erpc_status_t err; - if (m_remaining < length) + if ((length > 0U) && (data == NULL)) + { + err = kErpcStatus_MemoryError; + } + else if (length > m_remaining) { err = kErpcStatus_BufferOverrun; } @@ -124,7 +128,11 @@ erpc_status_t MessageBuffer::Cursor::write(const void *data, uint32_t length) erpc_status_t err; - if (length > m_remaining) + if ((length > 0U) && (data == NULL)) + { + err = kErpcStatus_MemoryError; + } + else if (length > m_remaining) { err = kErpcStatus_BufferOverrun; } diff --git a/erpcgen/src/templates/c_client_source.template b/erpcgen/src/templates/c_client_source.template index 2303fea99..93feea88f 100644 --- a/erpcgen/src/templates/c_client_source.template +++ b/erpcgen/src/templates/c_client_source.template @@ -107,7 +107,12 @@ extern ClientManager *g_client; {% for param in fn.parametersToClient if (param.serializedDirection == "" || param.serializedDirection == InDirection || param.referencedName != "") %} {% if param.isNullable %} -{$clientIndent} if ({$param.nullableName} != NULL{% if ((source == "client") && (param.direction != ReturnDirection) && (empty(param.lengthName) == false)) %} && {$param.lengthName} != NULL{% endif %}) +{% if ((source == "client") && (param.direction != ReturnDirection) && (empty(param.lengthName) == false)) %} +{% set lengthNameCon = ") && (" & param.lengthName & " != NULL)" >%} +{% else %} +{% set lengthNameCon = "" >%} +{% endif %} +{$clientIndent} if ({% if lengthNameCon != "" %}({% endif %}{$param.nullableName} != NULL{$lengthNameCon}) {$clientIndent} { {$addIndent(clientIndent & " ", param.coderCall.decode(param.coderCall))} } diff --git a/erpcgen/src/templates/c_coders.template b/erpcgen/src/templates/c_coders.template index d85902978..11541badc 100644 --- a/erpcgen/src/templates/c_coders.template +++ b/erpcgen/src/templates/c_coders.template @@ -47,7 +47,7 @@ if ({$info.sizeTemp} <= {$info.maxSize}) {% if source == "server" || info.useMallocOnClientSide == true %} {$indent}{$info.name} = (uint8_t *) erpc_malloc({$info.maxSize} * sizeof(uint8_t)); {% if generateAllocErrorChecks == true %} -{$indent}if ({$info.name} == NULL) +{$indent}if (({$info.name} == NULL) && ({$info.sizeTemp} > 0)) {$indent}{ {$indent} codec->updateStatus(kErpcStatus_MemoryError); {$indent}} @@ -100,7 +100,7 @@ if ({$info.sizeTemp} <= {$info.maxSize}) {% if source == "server" || info.useMallocOnClientSide == true %} {$indent}{$info.name} = ({$info.mallocType}) erpc_malloc({$info.maxSize} * sizeof({$info.mallocSizeType})); {% if generateAllocErrorChecks == true %} -{$indent}if ({$info.name} == NULL) +{$indent}if (({$info.name} == NULL) && ({$info.sizeTemp} > 0)) {$indent}{ {$indent} codec->updateStatus(kErpcStatus_MemoryError); {$indent}} @@ -312,7 +312,7 @@ codec->writeCallback((arrayOfFunPtr)({$info.callbacks}), {$info.callbacksCount}, {% def encodeSharedType(info) %} {% if sharedMemBeginAddr != "" %} -if ({$info.name} >= ERPC_SHARED_MEMORY_BEGIN && {$info.name} <= ERPC_SHARED_MEMORY_END) +if (({$info.name} >= ERPC_SHARED_MEMORY_BEGIN) && ({$info.name} <= ERPC_SHARED_MEMORY_END)) { codec->writePtr(reinterpret_cast({%if source == "client" && info.InoutOutDirection %}*{% endif %}{$info.name})); } diff --git a/erpcgen/src/templates/c_common_functions.template b/erpcgen/src/templates/c_common_functions.template index 0b83203b6..5081eff88 100644 --- a/erpcgen/src/templates/c_common_functions.template +++ b/erpcgen/src/templates/c_common_functions.template @@ -459,7 +459,12 @@ struct {% enddef ------------------------------- unionMembersDeclaration %} {% def f_paramIsNullableEncode(param) %} -if ({$param.nullableName} == NULL{% if ((source == "client") && (param.direction != ReturnDirection) && (empty(param.lengthName) == false)) %} || {$param.lengthName} == NULL{% endif %}) +{% if ((source == "client") && (param.direction != ReturnDirection) && (empty(param.lengthName) == false)) %} +{% set lengthNameCon = ") || (" & param.lengthName & " == NULL)" >%} +{% else %} +{% set lengthNameCon = "" >%} +{% endif %} +if ({% if lengthNameCon != "" %}({% endif %}{$param.nullableName} == NULL{$lengthNameCon}) { codec->writeNullFlag(true); } diff --git a/erpcgen/src/templates/c_server_source.template b/erpcgen/src/templates/c_server_source.template index 3d5dd7f4d..c05514e71 100644 --- a/erpcgen/src/templates/c_server_source.template +++ b/erpcgen/src/templates/c_server_source.template @@ -114,7 +114,7 @@ ERPC_MANUALLY_CONSTRUCTED_STATIC({$iface.serviceClassName}, s_{$iface.serviceCla #endif {% if serverIDName == "serviceID" %} {% for callbackFunction in callbackType.functions %} -{$serverIndent} {% if loop.first == false %}else {% endif %}if ({$serverIDName} == {$callbackFunction.serviceId} && {$functionIDName} == {$callbackFunction.id}) +{$serverIndent} {% if loop.first == false %}else {% endif %}if (({$serverIDName} == {$callbackFunction.serviceId}) && ({$functionIDName} == {$callbackFunction.id})) {$serverIndent} { {$serverIndent} {$callbackFunction.serverPrototype} {$serverIndent} } diff --git a/erpcgen/test/test_nullable_c.yml b/erpcgen/test/test_nullable_c.yml index f73990a90..e737775dd 100644 --- a/erpcgen/test/test_nullable_c.yml +++ b/erpcgen/test/test_nullable_c.yml @@ -335,7 +335,7 @@ test_client.cpp: - void bar(const bool * l, uint32_t c) - if: dir == 'inout' and ann == '@nullable' then: - - if (l == NULL || c == NULL) + - if ((l == NULL) || (c == NULL)) else: - if (l == NULL) - codec->writeNullFlag @@ -398,7 +398,7 @@ test_client.cpp: - void bar(bool * l, uint32_t * c) - if: ann == '@nullable' then: - - if (l == NULL || c == NULL) + - if ((l == NULL) || (c == NULL)) else: - if (l == NULL) - codec->writeNullFlag @@ -412,7 +412,7 @@ test_client.cpp: - codec->write - if: ann == '@nullable' then: - - if (l != NULL && c != NULL) + - if ((l != NULL) && (c != NULL)) else: - if (l != NULL) - codec->startReadList diff --git a/test/test_lists/test_lists_client_impl.cpp b/test/test_lists/test_lists_client_impl.cpp index 5c8a2e376..0e59f62cd 100644 --- a/test/test_lists/test_lists_client_impl.cpp +++ b/test/test_lists/test_lists_client_impl.cpp @@ -44,6 +44,20 @@ TEST(test_list, SendReceivedInt32) erpc_free(received_list); } +TEST(test_list, sendReceiveZeroSize) +{ + list_int32_1_t *received_list, send_list; + send_list.elementsCount = 0; + send_list.elements = NULL; + + received_list = sendReceivedInt32(&send_list); + + EXPECT_TRUE(received_list->elementsCount == 0); + + erpc_free(received_list->elements); + erpc_free(received_list); +} + TEST(test_list, SendReceived2Int32) { list_int32_2_t *received_list, send_list;