Skip to content

Commit 36b742d

Browse files
erpcgen: Fix templates for malloc errors.
If during the execution of the code the malloc fails to allocate memory appropriate checks are needed for safe execution. This patch introduces new checks to ensure safe execution. Signed-off-by: Adithya Baglody <[email protected]>
1 parent 5959ccd commit 36b742d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

erpcgen/src/templates/c_coders.template

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ else
7171
{% enddef ------------------------------- BinaryType %}
7272

7373
{% def decodeEnumType(info) ---------------- EnumType %}
74-
codec->read(&_tmp_local);
74+
if (!codec->getStatus()) {
75+
codec->read(&_tmp_local);
7576
{% if source == "client" && info.pointerScalarTypes %}
76-
*{$info.name} = static_cast<{$info.enumName}>(_tmp_local);
77+
*{$info.name} = static_cast<{$info.enumName}>(_tmp_local);
7778
{% else %}
78-
{$info.name} = static_cast<{$info.enumName}>(_tmp_local);
79+
{$info.name} = static_cast<{$info.enumName}>(_tmp_local);
7980
{% endif -- pointerScalarTypes %}
81+
}
8082
{% enddef ---------------------------------- EnumType %}
8183

8284
{% def decodeListType(info) ------------------- ListType %}
@@ -125,14 +127,24 @@ else
125127
{% else >%}
126128
for (uint32_t {$info.forLoopCount} = 0; {$info.forLoopCount} < {$info.sizeTemp}; ++{$info.forLoopCount})
127129
{
128-
{$addIndent(" ", info.protoNext.decode(info.protoNext))}
130+
{% if source == "server"%}
131+
if (codec->getStatus()) {
132+
break;
133+
}
134+
{% endif >%}
135+
if({$info.name})
136+
{
137+
{$addIndent(" ", info.protoNext.decode(info.protoNext))}
138+
}
129139
}
130140
{% endif >%}
131141
{% enddef ----------------------------------- ArrayType %}
132142

133143
{% def decodeStructType(info) --------------- StructType %}
134144
{% if info.inDataContainer %}
135-
read_{$info.typeName}_struct(codec, &({$info.name}));
145+
if (!codec->getStatus()) {
146+
read_{$info.typeName}_struct(codec, &({$info.name}));
147+
}
136148
{% else -- not inDataContainer %}
137149
read_{$info.typeName}_struct(codec, {$info.name});
138150
{% endif -- inDataContainer %}
@@ -269,6 +281,11 @@ codec->startWriteList({% if source == "client" && info.pointerScalarTypes %}*{%
269281
{% else >%}
270282
for (uint32_t {$info.forLoopCount} = 0; {$info.forLoopCount} < {% if source == "client" && info.pointerScalarTypes %}*{% endif %}{$info.size}; ++{$info.forLoopCount})
271283
{
284+
{% if source == "server" %}
285+
if (codec->getStatus()) {
286+
break;
287+
}
288+
{% endif >%}
272289
{$addIndent(" ", info.protoNext.encode(info.protoNext))}
273290
}
274291
{% endif >%}

erpcgen/src/templates/c_common_functions.template

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ static void read_{$struct.name}_struct_shared(erpc::{$codecClass} * codec, {$str
144144
static void read_{$struct.name}_struct(erpc::{$codecClass} * codec, {$struct.name} * data)
145145
{% endif %}
146146
{
147+
if(NULL == data) {
148+
return;
149+
}
147150
{% if struct.hasNullableMember %}
148151
bool isNull;
149152
{% endif -- hasNullableMember %}
@@ -185,6 +188,9 @@ static void write_{$struct.name}_struct_shared(erpc::{$codecClass} * codec, cons
185188
static void write_{$struct.name}_struct(erpc::{$codecClass} * codec, const {$struct.name} * data)
186189
{% endif %}
187190
{
191+
if(NULL == data) {
192+
return;
193+
}
188194
{% for mem in struct.members if (shared == "def" && not mem.serializedViaMember) || (mem.noSharedMem && shared == "noSharedMem") %}
189195
{% if mem.isNullable %}
190196
if ({$mem.coderCall.name}{$mem.structElements} == NULL)
@@ -271,6 +277,9 @@ static void read_{$union.name}_union_shared(erpc::{$codecClass} * codec, int32_t
271277
static void read_{$union.name}_union(erpc::{$codecClass} * codec, int32_t * discriminator, {$union.name} * data)
272278
{% endif %}
273279
{
280+
if(NULL == data) {
281+
return;
282+
}
274283
{% if union.needTempVariable %}
275284
int32_t _tmp_local;
276285

@@ -387,7 +396,10 @@ if ({$info.name})
387396
{% def freeArray(info) %}
388397
for (uint32_t {$info.forLoopCount} = 0; {$info.forLoopCount} < {$info.size}; ++{$info.forLoopCount})
389398
{
390-
{$addIndent(" ", info.protoNext.freeingCall(info.protoNext))}
399+
if ({$info.name})
400+
{
401+
{$addIndent(" ", info.protoNext.freeingCall(info.protoNext))}
402+
}
391403
}
392404
{% enddef ------------------------------- freeArray %}
393405

0 commit comments

Comments
 (0)