From 095245f6a1b96ad1a94039341cedc42b2f4b26d5 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Wed, 7 May 2025 14:47:51 +0200 Subject: [PATCH] Remove copyCBORStringToArray and copyCBORByteToArray in favor of explicit instructions --- src/cbor/IoTCloudMessageDecoder.cpp | 97 ++++++++++++++++++----------- 1 file changed, 61 insertions(+), 36 deletions(-) diff --git a/src/cbor/IoTCloudMessageDecoder.cpp b/src/cbor/IoTCloudMessageDecoder.cpp index e25962219..3c01d52af 100644 --- a/src/cbor/IoTCloudMessageDecoder.cpp +++ b/src/cbor/IoTCloudMessageDecoder.cpp @@ -17,28 +17,6 @@ #include "IoTCloudMessageDecoder.h" #include -static inline bool copyCBORStringToArray(CborValue * param, char * dest, size_t dest_size) { - if (cbor_value_is_text_string(param)) { - // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string - if(_cbor_value_copy_string(param, dest, &dest_size, NULL) == CborNoError) { - return true; - } - } - - return false; -} - -static inline size_t copyCBORByteToArray(CborValue * param, uint8_t * dest, size_t dest_size) { - if (cbor_value_is_byte_string(param)) { - // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string - if(_cbor_value_copy_string(param, dest, &dest_size, NULL) == CborNoError) { - return dest_size; - } - } - - return 0; -} - /****************************************************************************** MESSAGE DECODE FUNCTIONS ******************************************************************************/ @@ -47,7 +25,14 @@ MessageDecoder::Status ThingUpdateCommandDecoder::decode(CborValue* iter, Messag ThingUpdateCmd * thingCommand = (ThingUpdateCmd *) msg; // Message is composed of a single parameter, a string (thing_id) - if (!copyCBORStringToArray(iter, thingCommand->params.thing_id, sizeof(thingCommand->params.thing_id))) { + if (!cbor_value_is_text_string(iter)) { + return MessageDecoder::Status::Error; + } + + size_t dest_size = sizeof(thingCommand->params.thing_id); + + if(_cbor_value_copy_string( + iter, thingCommand->params.thing_id, &dest_size, NULL) != CborNoError) { return MessageDecoder::Status::Error; } @@ -58,7 +43,14 @@ MessageDecoder::Status ThingDetachCommandDecoder::decode(CborValue* iter, Messag ThingDetachCmd * thingCommand = (ThingDetachCmd *) msg; // Message is composed of a single parameter, a string (thing_id) - if (!copyCBORStringToArray(iter, thingCommand->params.thing_id, sizeof(thingCommand->params.thing_id))) { + if (!cbor_value_is_text_string(iter)) { + return MessageDecoder::Status::Error; + } + + size_t dest_size = sizeof(thingCommand->params.thing_id); + + if(_cbor_value_copy_string( + iter, thingCommand->params.thing_id, &dest_size, NULL) != CborNoError) { return MessageDecoder::Status::Error; } @@ -125,33 +117,66 @@ MessageDecoder::Status LastValuesUpdateCommandDecoder::decode(CborValue* iter, M } MessageDecoder::Status OtaUpdateCommandDecoder::decode(CborValue* iter, Message *msg) { - CborError error = CborNoError; OtaUpdateCmdDown * ota = (OtaUpdateCmdDown *) msg; // Message is composed 4 parameters: id, url, initialSha, finalSha - if (!copyCBORByteToArray(iter, ota->params.id, sizeof(ota->params.id))) { + + // decoding parameter id + size_t dest_size = sizeof(ota->params.id); + + if (!cbor_value_is_byte_string(iter)) { return MessageDecoder::Status::Error; } - error = cbor_value_advance(iter); + // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string + if(_cbor_value_copy_string(iter, ota->params.id, &dest_size, NULL) != CborNoError) { + return MessageDecoder::Status::Error; + } - if ((error != CborNoError) || !copyCBORStringToArray(iter, ota->params.url, sizeof(ota->params.url))) { + if(cbor_value_advance(iter) != CborNoError) { return MessageDecoder::Status::Error; } - error = cbor_value_advance(iter); + // decoding parameter url + dest_size = sizeof(ota->params.url); - if ((error != CborNoError) || - copyCBORByteToArray(iter, ota->params.initialSha256, - sizeof(ota->params.initialSha256)) != sizeof(ota->params.initialSha256)) { + if (!cbor_value_is_text_string(iter)) { return MessageDecoder::Status::Error; } - error = cbor_value_advance(iter); + if(_cbor_value_copy_string(iter, ota->params.url, &dest_size, NULL) != CborNoError) { + return MessageDecoder::Status::Error; + } + + if(cbor_value_advance(iter) != CborNoError) { + return MessageDecoder::Status::Error; + } + + // decoding parameter initialSha256 + dest_size = sizeof(ota->params.initialSha256); + + if (!cbor_value_is_byte_string(iter)) { + return MessageDecoder::Status::Error; + } + + // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string + if(_cbor_value_copy_string(iter, ota->params.initialSha256, &dest_size, NULL) != CborNoError) { + return MessageDecoder::Status::Error; + } + + if(cbor_value_advance(iter) != CborNoError) { + return MessageDecoder::Status::Error; + } + + // decoding parameter finalSha256 + dest_size = sizeof(ota->params.finalSha256); + + if (!cbor_value_is_byte_string(iter)) { + return MessageDecoder::Status::Error; + } - if ((error != CborNoError) || - copyCBORByteToArray(iter, ota->params.finalSha256, - sizeof(ota->params.finalSha256)) != sizeof(ota->params.finalSha256)) { + // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string + if(_cbor_value_copy_string(iter, ota->params.finalSha256, &dest_size, NULL) != CborNoError) { return MessageDecoder::Status::Error; }