Skip to content

Commit 4c120bb

Browse files
authored
improved codec function to use reference instead of address (#324)
* improved codec function to use reference instead of address -- safer as prevent passing nullptr Signed-off-by: Cervenka Dusan <cervenka@acrios.com> * Updated comments related to variables order change Signed-off-by: Cervenka Dusan <cervenka@acrios.com> Signed-off-by: Cervenka Dusan <cervenka@acrios.com>
1 parent e5fb9a6 commit 4c120bb

12 files changed

+199
-198
lines changed

erpc_c/infra/erpc_basic_codec.cpp

Lines changed: 61 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
#include "erpc_basic_codec.hpp"
12+
1213
#include "erpc_config_internal.h"
1314
#include ENDIANNESS_HEADER
1415
#include "erpc_manually_constructed.hpp"
@@ -36,7 +37,7 @@ void BasicCodec::startWriteMessage(message_type_t type, uint32_t service, uint32
3637
write(sequence);
3738
}
3839

39-
void BasicCodec::writeData(const void *value, uint32_t length)
40+
void BasicCodec::writeData(uint32_t length, const void *value)
4041
{
4142
if (isStatusOk())
4243
{
@@ -49,73 +50,73 @@ void BasicCodec::write(bool value)
4950
// Make sure the bool is a single byte.
5051
uint8_t v = (uint8_t)value;
5152

52-
writeData(&v, sizeof(v));
53+
writeData(sizeof(v), &v);
5354
}
5455

5556
void BasicCodec::write(int8_t value)
5657
{
57-
writeData(&value, sizeof(value));
58+
writeData(sizeof(value), &value);
5859
}
5960

6061
void BasicCodec::write(int16_t value)
6162
{
6263
ERPC_WRITE_AGNOSTIC_16(value);
6364

64-
writeData(&value, sizeof(value));
65+
writeData(sizeof(value), &value);
6566
}
6667

6768
void BasicCodec::write(int32_t value)
6869
{
6970
ERPC_WRITE_AGNOSTIC_32(value);
7071

71-
writeData(&value, sizeof(value));
72+
writeData(sizeof(value), &value);
7273
}
7374

7475
void BasicCodec::write(int64_t value)
7576
{
7677
ERPC_WRITE_AGNOSTIC_64(value);
7778

78-
writeData(&value, sizeof(value));
79+
writeData(sizeof(value), &value);
7980
}
8081

8182
void BasicCodec::write(uint8_t value)
8283
{
83-
writeData(&value, sizeof(value));
84+
writeData(sizeof(value), &value);
8485
}
8586

8687
void BasicCodec::write(uint16_t value)
8788
{
8889
ERPC_WRITE_AGNOSTIC_16(value);
8990

90-
writeData(&value, sizeof(value));
91+
writeData(sizeof(value), &value);
9192
}
9293

9394
void BasicCodec::write(uint32_t value)
9495
{
9596
ERPC_WRITE_AGNOSTIC_32(value);
9697

97-
writeData(&value, sizeof(value));
98+
writeData(sizeof(value), &value);
9899
}
99100

100101
void BasicCodec::write(uint64_t value)
101102
{
102103
ERPC_WRITE_AGNOSTIC_64(value);
103104

104-
writeData(&value, sizeof(value));
105+
writeData(sizeof(value), &value);
105106
}
106107

107108
void BasicCodec::write(float value)
108109
{
109110
ERPC_WRITE_AGNOSTIC_FLOAT(value);
110111

111-
writeData(&value, sizeof(value));
112+
writeData(sizeof(value), &value);
112113
}
113114

114115
void BasicCodec::write(double value)
115116
{
116117
ERPC_WRITE_AGNOSTIC_DOUBLE(value);
117118

118-
writeData(&value, sizeof(value));
119+
writeData(sizeof(value), &value);
119120
}
120121

121122
void BasicCodec::writePtr(uintptr_t value)
@@ -126,7 +127,7 @@ void BasicCodec::writePtr(uintptr_t value)
126127

127128
ERPC_WRITE_AGNOSTIC_PTR(value);
128129

129-
writeData(&value, ptrSize);
130+
writeData(ptrSize, &value);
130131
}
131132

132133
void BasicCodec::writeString(uint32_t length, const char *value)
@@ -140,7 +141,7 @@ void BasicCodec::writeBinary(uint32_t length, const uint8_t *value)
140141
// Write the blob length as a u32.
141142
write(length);
142143

143-
writeData(value, length);
144+
writeData(length, value);
144145
}
145146

146147
void BasicCodec::startWriteList(uint32_t length)
@@ -191,11 +192,11 @@ void BasicCodec::writeCallback(funPtr callback1, funPtr callback2)
191192
}
192193
}
193194

194-
void BasicCodec::startReadMessage(message_type_t *type, uint32_t *service, uint32_t *request, uint32_t *sequence)
195+
void BasicCodec::startReadMessage(message_type_t &type, uint32_t &service, uint32_t &request, uint32_t &sequence)
195196
{
196197
uint32_t header;
197198

198-
read(&header);
199+
read(header);
199200

200201
if (((header >> 24) & 0xffU) != kBasicCodecVersion)
201202
{
@@ -204,150 +205,150 @@ void BasicCodec::startReadMessage(message_type_t *type, uint32_t *service, uint3
204205

205206
if (isStatusOk())
206207
{
207-
*service = ((header >> 16) & 0xffU);
208-
*request = ((header >> 8) & 0xffU);
209-
*type = static_cast<message_type_t>(header & 0xffU);
208+
service = ((header >> 16) & 0xffU);
209+
request = ((header >> 8) & 0xffU);
210+
type = static_cast<message_type_t>(header & 0xffU);
210211

211212
read(sequence);
212213
}
213214
}
214215

215-
void BasicCodec::readData(void *value, uint32_t length)
216+
void BasicCodec::readData(uint32_t length, void *value)
216217
{
217218
if (isStatusOk())
218219
{
219220
m_status = m_cursor.read(value, length);
220221
}
221222
}
222223

223-
void BasicCodec::read(bool *value)
224+
void BasicCodec::read(bool &value)
224225
{
225226
uint8_t v = 0;
226227

227-
readData(&v, sizeof(v));
228+
readData(sizeof(v), &v);
228229
if (isStatusOk())
229230
{
230-
*value = (bool)v;
231+
value = (bool)v;
231232
}
232233
}
233234

234-
void BasicCodec::read(int8_t *value)
235+
void BasicCodec::read(int8_t &value)
235236
{
236-
readData(value, sizeof(*value));
237+
readData(sizeof(value), &value);
237238
}
238239

239-
void BasicCodec::read(int16_t *value)
240+
void BasicCodec::read(int16_t &value)
240241
{
241-
readData(value, sizeof(*value));
242+
readData(sizeof(value), &value);
242243
if (isStatusOk())
243244
{
244245
ERPC_READ_AGNOSTIC_16(*value);
245246
}
246247
}
247248

248-
void BasicCodec::read(int32_t *value)
249+
void BasicCodec::read(int32_t &value)
249250
{
250-
readData(value, sizeof(*value));
251+
readData(sizeof(value), &value);
251252
if (isStatusOk())
252253
{
253254
ERPC_READ_AGNOSTIC_32(*value);
254255
}
255256
}
256257

257-
void BasicCodec::read(int64_t *value)
258+
void BasicCodec::read(int64_t &value)
258259
{
259-
readData(value, sizeof(*value));
260+
readData(sizeof(value), &value);
260261
if (isStatusOk())
261262
{
262263
ERPC_READ_AGNOSTIC_64(*value);
263264
}
264265
}
265266

266-
void BasicCodec::read(uint8_t *value)
267+
void BasicCodec::read(uint8_t &value)
267268
{
268-
readData(value, sizeof(*value));
269+
readData(sizeof(value), &value);
269270
}
270271

271-
void BasicCodec::read(uint16_t *value)
272+
void BasicCodec::read(uint16_t &value)
272273
{
273-
readData(value, sizeof(*value));
274+
readData(sizeof(value), &value);
274275
if (isStatusOk())
275276
{
276277
ERPC_READ_AGNOSTIC_16(*value);
277278
}
278279
}
279280

280-
void BasicCodec::read(uint32_t *value)
281+
void BasicCodec::read(uint32_t &value)
281282
{
282-
readData(value, sizeof(*value));
283+
readData(sizeof(value), &value);
283284
if (isStatusOk())
284285
{
285286
ERPC_READ_AGNOSTIC_32(*value);
286287
}
287288
}
288289

289-
void BasicCodec::read(uint64_t *value)
290+
void BasicCodec::read(uint64_t &value)
290291
{
291-
readData(value, sizeof(*value));
292+
readData(sizeof(value), &value);
292293
if (isStatusOk())
293294
{
294295
ERPC_READ_AGNOSTIC_64(*value);
295296
}
296297
}
297298

298-
void BasicCodec::read(float *value)
299+
void BasicCodec::read(float &value)
299300
{
300-
readData(value, sizeof(*value));
301+
readData(sizeof(value), &value);
301302
if (isStatusOk())
302303
{
303304
ERPC_READ_AGNOSTIC_FLOAT(*value);
304305
}
305306
}
306307

307-
void BasicCodec::read(double *value)
308+
void BasicCodec::read(double &value)
308309
{
309-
readData(value, sizeof(*value));
310+
readData(sizeof(value), &value);
310311
if (isStatusOk())
311312
{
312313
ERPC_READ_AGNOSTIC_DOUBLE(*value);
313314
}
314315
}
315316

316-
void BasicCodec::readPtr(uintptr_t *value)
317+
void BasicCodec::readPtr(uintptr_t &value)
317318
{
318319
uint8_t ptrSize;
319320

320-
read(&ptrSize);
321+
read(ptrSize);
321322

322-
if (ptrSize > sizeof(*value))
323+
if (ptrSize > sizeof(value))
323324
{
324325
updateStatus(kErpcStatus_BadAddressScale);
325326
}
326327

327-
readData(value, ptrSize);
328+
readData(ptrSize, &value);
328329
if (isStatusOk())
329330
{
330331
ERPC_READ_AGNOSTIC_PTR(*value);
331332
}
332333
}
333334

334-
void BasicCodec::readString(uint32_t *length, char **value)
335+
void BasicCodec::readString(uint32_t &length, char **value)
335336
{
336337
readBinary(length, reinterpret_cast<uint8_t **>(value));
337338
}
338339

339-
void BasicCodec::readBinary(uint32_t *length, uint8_t **value)
340+
void BasicCodec::readBinary(uint32_t &length, uint8_t **value)
340341
{
341342
// Read length first as u32.
342343
read(length);
343344

344345
if (isStatusOk())
345346
{
346-
if (m_cursor.getRemainingUsed() < *length)
347+
if (m_cursor.getRemainingUsed() < length)
347348
{
348349
m_status = kErpcStatus_Fail;
349350
}
350-
else if (m_cursor.getRemaining() < *length)
351+
else if (m_cursor.getRemaining() < length)
351352
{
352353
m_status = kErpcStatus_BufferOverrun;
353354
}
@@ -357,41 +358,41 @@ void BasicCodec::readBinary(uint32_t *length, uint8_t **value)
357358
*value = m_cursor.get();
358359

359360
// Skip over data.
360-
m_cursor += (uint16_t)*length;
361+
m_cursor += (uint16_t)length;
361362
}
362363
}
363364
if (!isStatusOk())
364365
{
365-
*length = 0;
366+
length = 0;
366367
*value = NULL;
367368
}
368369
}
369370

370-
void BasicCodec::startReadList(uint32_t *length)
371+
void BasicCodec::startReadList(uint32_t &length)
371372
{
372373
// Read list length as u32.
373374
read(length);
374375

375376
if (!isStatusOk())
376377
{
377-
*length = 0;
378+
length = 0;
378379
}
379380
}
380381

381-
void BasicCodec::startReadUnion(int32_t *discriminator)
382+
void BasicCodec::startReadUnion(int32_t &discriminator)
382383
{
383384
// Read union discriminator as u32.
384385
read(discriminator);
385386
}
386387

387-
void BasicCodec::readNullFlag(bool *isNull)
388+
void BasicCodec::readNullFlag(bool &isNull)
388389
{
389390
uint8_t flag;
390391

391-
read(&flag);
392+
read(flag);
392393
if (isStatusOk())
393394
{
394-
*isNull = (flag == (uint8_t)kIsNull);
395+
isNull = (flag == (uint8_t)kIsNull);
395396
}
396397
}
397398

@@ -402,7 +403,7 @@ void BasicCodec::readCallback(arrayOfFunPtr callbacks, uint8_t callbacksCount, f
402403
erpc_assert(callbacksCount > 1U);
403404

404405
// callbacks = callbacks table
405-
read(&_tmp_local);
406+
read(_tmp_local);
406407
if (isStatusOk())
407408
{
408409
if (_tmp_local < callbacksCount)

0 commit comments

Comments
 (0)