Skip to content

Commit

Permalink
Change msg router request data size to size_t.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jvalenzuela authored and MartinMelikMerkumians committed Oct 29, 2024
1 parent d5ee6b1 commit 1e99582
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
9 changes: 6 additions & 3 deletions source/src/cip/cipcommon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions source/src/cip/cipcommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
18 changes: 10 additions & 8 deletions source/src/cip/cipmessagerouter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/src/cip/ciptypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 1e99582

Please sign in to comment.