Skip to content

Commit

Permalink
Fix the check if there is really max size
Browse files Browse the repository at this point in the history
  • Loading branch information
amgross committed Dec 1, 2022
1 parent f1c4a23 commit 05da618
Showing 1 changed file with 36 additions and 32 deletions.
68 changes: 36 additions & 32 deletions erpcgen/src/templates/c_coders.template
Original file line number Diff line number Diff line change
@@ -1,38 +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);
{% if info.stringAllocSize != info.stringLocalName + "_len" %}
if ({$info.stringLocalName}_len <= {$info.stringAllocSize})
{
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" %}
{% endif -- if info.stringAllocSize != info.stringLocalName & "_len" %}
{% if ((source == "client" && info.withoutAlloc == false) or source == "server") %}
{$indent}{$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 %}
{$indent}if (({$info.name} == NULL) || ({$info.stringLocalName}_local == NULL))
{$indent}{
{$indent} codec->updateStatus(kErpcStatus_MemoryError);
{$indent}}
{$indent}else
{$indent}{
{$indent} if (({$info.name} == NULL) || ({$info.stringLocalName}_local == NULL))
{$indent} {
{$indent} codec->updateStatus(kErpcStatus_MemoryError);
{$indent} }
{$indent} else
{$indent} {
{% endif -- generateAllocErrorChecks == true %}
{% endif -- withoutAlloc %}
{$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;
{$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}}
{$indent}}
{% endif -- withoutAlloc && generateAllocErrorChecks %}
{% if info.stringAllocSize != info.stringLocalName + "_len" %}
}
else
{
codec->updateStatus(kErpcStatus_InvalidArgument);
{% if info.stringAllocSize != info.stringLocalName & "_len" %}
}
else
{
codec->updateStatus(kErpcStatus_InvalidArgument);
}
{% endif -- if info.stringAllocSize != info.stringLocalName & "_len" %}
}
{% endif -- if info.stringAllocSize != info.stringLocalName + "_len" %}
{% else %}
{% if source == "client" && info.pointerScalarTypes %}
codec->read({$info.name});
Expand Down Expand Up @@ -242,21 +244,23 @@ codec->readData({$info.name}, {$info.sizeTemp} * sizeof({$info.builtinTypeName})
{# Encode sending data #}
{% def encodeBuiltinType(info) ----------------- %}
{% if info.builtinType == "kStringType" %}
uint32_t {$info.stringLocalName}_len = strlen((const char*){$info.name});
{% if info.stringAllocSize != info.stringLocalName + "_len" %}
{
uint32_t {$info.stringLocalName}_len = strlen((const char*){$info.name});
{% if info.stringAllocSize != info.stringLocalName & "_len" %}

{% if source == "server" %}
erpc_assert({$info.stringLocalName}_len <= {$info.stringAllocSize});
erpc_assert({$info.stringLocalName}_len <= {$info.stringAllocSize});

{% else %}
if ({$info.stringAllocSize} < {$info.stringLocalName}_len)
{
codec->updateStatus(kErpcStatus_InvalidArgument);
}
if ({$info.stringAllocSize} < {$info.stringLocalName}_len)
{
codec->updateStatus(kErpcStatus_InvalidArgument);
}

{% endif %}
{% endif %}
codec->writeString({$info.stringLocalName}_len, (const char*){$info.name});
codec->writeString({$info.stringLocalName}_len, (const char*){$info.name});
}
{% else %}
{% if source == "client" && info.pointerScalarTypes %}
codec->write(*{$info.name});
Expand All @@ -267,12 +271,12 @@ codec->write({$info.name});
{% enddef --------------------------------------- BuiltinType %}

{% def encodeBinaryType(info) ----------------- %}
{% if info.size != info.maxSize %}
{% if info.sizeTemp != info.maxSize %}
{% if source == "server" %}
erpc_assert({$info.size} <= {$info.maxSize} * sizeof({$info.mallocSizeType}));

{% else %}
if ({% if info.pointerScalarTypes %}*{% endif %}{$info.size} > {$info.maxSize})
if ({% if source == "client" && info.pointerScalarTypes %}*{% endif %}{$info.size} > {$info.maxSize})
{
codec->updateStatus(kErpcStatus_InvalidArgument);
}
Expand Down

0 comments on commit 05da618

Please sign in to comment.