Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check that string read is not more than max length #328

Merged
merged 7 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions erpcgen/src/templates/c_coders.template
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
{% def decodeBuiltinType(info) --------------- BuiltinType %}
{% if info.builtinType == "kStringType" %}
uint32_t {$info.stringLocalName}_len;
char * {$info.stringLocalName}_local;
codec->readString(&{$info.stringLocalName}_len, &{$info.stringLocalName}_local);
{
uint32_t {$info.stringLocalName}_len;
char * {$info.stringLocalName}_local;
codec->readString(&{$info.stringLocalName}_len, &{$info.stringLocalName}_local);
{% if info.stringAllocSize != info.stringLocalName & "_len" %}
if ({$info.stringLocalName}_len <= {$info.stringAllocSize})
{
{% set indent = " " >%}
{% else %}
{% set indent = "" >%}
{% endif -- if info.stringAllocSize != info.stringLocalName & "_len" %}
{% if ((source == "client" && info.withoutAlloc == false) or source == "server") %}
{$info.name} = ({$info.builtinTypeName}) erpc_malloc(({$info.stringAllocSize} + 1) * sizeof(char));
{$indent} {$info.name} = ({$info.builtinTypeName}) erpc_malloc(({$info.stringAllocSize} + 1) * sizeof(char));
{% if generateAllocErrorChecks == true %}
if (({$info.name} == NULL) || ({$info.stringLocalName}_local == NULL))
{
codec->updateStatus(kErpcStatus_MemoryError);
}
else
{
{$indent} if (({$info.name} == NULL) || ({$info.stringLocalName}_local == NULL))
{$indent} {
{$indent} codec->updateStatus(kErpcStatus_MemoryError);
{$indent} }
{$indent} else
{$indent} {
{% endif -- generateAllocErrorChecks == true %}
{% endif -- withoutAlloc %}
{% if (((source == "client" && info.withoutAlloc == false) or source == "server") && generateAllocErrorChecks == true) %} {% endif -- withoutAlloc %}memcpy({$info.name}, {$info.stringLocalName}_local, {$info.stringLocalName}_len);
{% if (((source == "client" && info.withoutAlloc == false) or source == "server") && generateAllocErrorChecks == true) %} {% endif -- withoutAlloc %}({$info.name})[{$info.stringLocalName}_len] = 0;
{$indent} {% if (((source == "client" && info.withoutAlloc == false) or source == "server") && generateAllocErrorChecks == true) %} {% endif -- withoutAlloc %}memcpy({$info.name}, {$info.stringLocalName}_local, {$info.stringLocalName}_len);
{$indent} {% if (((source == "client" && info.withoutAlloc == false) or source == "server") && generateAllocErrorChecks == true) %} {% endif -- withoutAlloc %}({$info.name})[{$info.stringLocalName}_len] = 0;
{% if (((source == "client" && info.withoutAlloc == false) or source == "server") && generateAllocErrorChecks == true) %}
}
{$indent}}
{% endif -- withoutAlloc && generateAllocErrorChecks %}
{% if info.stringAllocSize != info.stringLocalName & "_len" %}
}
else
{
codec->updateStatus(kErpcStatus_InvalidArgument);
}
{% endif -- if info.stringAllocSize != info.stringLocalName & "_len" %}
}
{% else %}
{% if source == "client" && info.pointerScalarTypes %}
codec->read({$info.name});
Expand Down Expand Up @@ -65,7 +81,7 @@ if (({$info.sizeTemp} <= {$info.maxSize}) && ({$info.dataTemp} != NULL))
}
else
{
codec->updateStatus(kErpcStatus_Fail);
codec->updateStatus(kErpcStatus_InvalidArgument);
}
{% endif %}
{% enddef ------------------------------- BinaryType %}
Expand Down Expand Up @@ -228,7 +244,15 @@ codec->readData({$info.name}, {$info.sizeTemp} * sizeof({$info.builtinTypeName})
{# Encode sending data #}
{% def encodeBuiltinType(info) ----------------- %}
{% if info.builtinType == "kStringType" %}
codec->writeString(strlen((const char*){$info.name}), (const char*){$info.name});
{
uint32_t {$info.stringLocalName}_len = strlen((const char*){$info.name});

{% if info.stringAllocSize != info.stringLocalName & "_len" %}
erpc_assert({$info.stringLocalName}_len <= {$info.stringAllocSize});
amgross marked this conversation as resolved.
Show resolved Hide resolved

amgross marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}
codec->writeString({$info.stringLocalName}_len, (const char*){$info.name});
}
{% else %}
{% if source == "client" && info.pointerScalarTypes %}
codec->write(*{$info.name});
Expand All @@ -239,8 +263,8 @@ codec->write({$info.name});
{% enddef --------------------------------------- BuiltinType %}

{% def encodeBinaryType(info) ----------------- %}
{% if source == "server" && info.size != info.maxSize %}
erpc_assert({$info.size} <= {$info.maxSize} * sizeof({$info.mallocSizeType}));
{% if info.sizeTemp != info.maxSize %}
erpc_assert({% if source == "client" && info.pointerScalarTypes %}*{% endif %}{$info.size} <= {$info.maxSize} * sizeof({$info.mallocSizeType}));
{% endif %}
codec->writeBinary({% if source == "client" && info.pointerScalarTypes %}*{% endif %}{$info.size}, {$info.name});
{% enddef --------------------------------------- BinaryType %}
Expand Down
20 changes: 14 additions & 6 deletions erpcgen/test/test_nullable_c.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ test_client.cpp:
- codec->writeNullFlag(true)
- if: dir in ('in', 'inout')
then:
- codec->writeString(strlen((const char*)a), (const char*)a);
- uint32_t a_len = strlen((const char*)a);
- codec->writeString(a_len, (const char*)a);
- if: dir == 'out'
then:
- not: codec->writeString(a_len, (const char*)a);
- not: codec->writeString(strlen((const char*)a), (const char*)a);
- if: dir in ('out', 'inout')
then:
Expand All @@ -41,7 +43,8 @@ test_server.cpp:
- if: dir in ('out', 'inout')
then:
- if (a != NULL)
- codec->writeString(strlen((const char*)a), (const char*)a);
- uint32_t a_len = strlen((const char*)a);
- codec->writeString(a_len, (const char*)a);
---
name: param ustring
desc:
Expand All @@ -60,9 +63,11 @@ test_client.cpp:
- codec->writeNullFlag(true)
- if: dir in ('in', 'inout')
then:
- codec->writeString(strlen((const char*)a), (const char*)a);
- uint32_t a_len = strlen((const char*)a);
- codec->writeString(a_len, (const char*)a);
- if: dir == 'out'
then:
- not: codec->writeString(a_len, (const char*)a);
- not: codec->writeString(strlen((const char*)a), (const char*)a);
- if: dir in ('out', 'inout')
then:
Expand All @@ -85,7 +90,8 @@ test_server.cpp:
- if: dir in ('out', 'inout')
then:
- if (a != NULL)
- codec->writeString(strlen((const char*)a), (const char*)a);
- uint32_t a_len = strlen((const char*)a);
- codec->writeString(a_len, (const char*)a);
---
name: in param struct
desc:
Expand Down Expand Up @@ -212,7 +218,8 @@ test_client.cpp:
- codec->writeNullFlag(true);
- else
- codec->writeNullFlag(false);
- codec->writeString(strlen((const char*)data->a), (const char*)data->a);
- uint32_t a_len = strlen((const char*)data->a);
- codec->writeString(a_len, (const char*)data->a);
- if: dir in ('out', 'inout')
then:
- void read_erpc_pair_struct
Expand Down Expand Up @@ -241,7 +248,8 @@ test_server.cpp:
- codec->writeNullFlag(true);
- else
- codec->writeNullFlag(false);
- codec->writeString(strlen((const char*)data->a), (const char*)data->a);
- uint32_t a_len = strlen((const char*)data->a);
- codec->writeString(a_len, (const char*)data->a);

---
name: return struct
Expand Down