From 1e99582227c86cbdc7c86caeb7ba04c56134d174 Mon Sep 17 00:00:00 2001 From: Jason Valenzuela Date: Thu, 18 Apr 2024 16:15:03 -0400 Subject: [PATCH] Change msg router request data size to size_t. Resolves signed/unsigned warnings arising from comparisons of the request_data_size member with unsigned quantities, e.g., Visual Studio C4018. DecodePaddedEPath() was modified to change the consumed bytes value into a size_t output parameter while still supporting error return values, which may be negative. The new size_t type is better suited for this type of value and this function's use cases. --- source/src/cip/cipcommon.c | 9 ++++++--- source/src/cip/cipcommon.h | 11 ++++++++--- source/src/cip/cipmessagerouter.c | 18 ++++++++++-------- source/src/cip/ciptypes.h | 2 +- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/source/src/cip/cipcommon.c b/source/src/cip/cipcommon.c index 3b8386ecc..8b5549237 100644 --- a/source/src/cip/cipcommon.c +++ b/source/src/cip/cipcommon.c @@ -1385,8 +1385,10 @@ void EncodeEPath(const void *const data, message->used_message_length - start_length); } -int DecodePaddedEPath(CipEpath *epath, - const EipUint8 **message) { +EipStatus DecodePaddedEPath(CipEpath *epath, + const EipUint8 **message, + size_t *const bytes_consumed) { + OPENER_ASSERT(bytes_consumed != NULL); unsigned int number_of_decoded_elements = 0; const EipUint8 *message_runner = *message; @@ -1461,7 +1463,8 @@ int DecodePaddedEPath(CipEpath *epath, } *message = message_runner; - return number_of_decoded_elements * 2 + 1; /* number_of_decoded_elements times 2 as every encoding uses 2 bytes */ + *bytes_consumed = number_of_decoded_elements * sizeof(CipWord) + 1; /* number_of_decoded_elements times 2 as every encoding uses 2 bytes */ + return kEipStatusOk; } EipStatus CipCreateService(CipInstance *RESTRICT const instance, diff --git a/source/src/cip/cipcommon.h b/source/src/cip/cipcommon.h index 14952c56b..db6919cc1 100644 --- a/source/src/cip/cipcommon.h +++ b/source/src/cip/cipcommon.h @@ -147,10 +147,15 @@ EipStatus SetAttributeList(CipInstance *instance, /** @brief Decodes padded EPath * @param epath EPath object to the receiving element * @param message pointer to the message to decode - * @return Number of decoded bytes + * @param[out] bytes_decoded Location to store the number of bytes consumed + * from the message buffer. + * @return @link EipStatus kEipStatusOk @endlink if successful, or + * @link EipStatus kEipStatusError @endlink if the path + * could not be parsed. */ -int DecodePaddedEPath(CipEpath *epath, - const EipUint8 **message); +EipStatus DecodePaddedEPath(CipEpath *epath, + const EipUint8 **message, + size_t *const bytes_decoded); /** @brief Generic implementation of the CIP Create service * diff --git a/source/src/cip/cipmessagerouter.c b/source/src/cip/cipmessagerouter.c index b14d7910b..756dab84d 100644 --- a/source/src/cip/cipmessagerouter.c +++ b/source/src/cip/cipmessagerouter.c @@ -249,19 +249,21 @@ CipError CreateMessageRouterRequestStructure(const EipUint8 *data, data++; data_length--; - int number_of_decoded_bytes = - DecodePaddedEPath(&(message_router_request->request_path), &data); - if(number_of_decoded_bytes < 0) { + size_t number_of_decoded_bytes; + const EipStatus path_result = + DecodePaddedEPath(&(message_router_request->request_path), + &data, + &number_of_decoded_bytes); + if(path_result != kEipStatusOk) { return kCipErrorPathSegmentError; } - message_router_request->data = data; - message_router_request->request_data_size = data_length - - number_of_decoded_bytes; - - if(message_router_request->request_data_size < 0) { + if(number_of_decoded_bytes > data_length) { return kCipErrorPathSizeInvalid; } else { + message_router_request->data = data; + message_router_request->request_data_size = data_length - + number_of_decoded_bytes; return kCipErrorSuccess; } } diff --git a/source/src/cip/ciptypes.h b/source/src/cip/ciptypes.h index 7529669b7..786ffb643 100644 --- a/source/src/cip/ciptypes.h +++ b/source/src/cip/ciptypes.h @@ -265,7 +265,7 @@ typedef struct { typedef struct { CipUsint service; CipEpath request_path; - EipInt16 request_data_size; + size_t request_data_size; const CipOctet *data; } CipMessageRouterRequest;