Skip to content

Commit

Permalink
Type cast memcpy pointers to void*
Browse files Browse the repository at this point in the history
  • Loading branch information
DakshitBabbar committed Feb 6, 2025
1 parent aa9d36d commit 08030c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 5 additions & 1 deletion source/core_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3751,12 +3751,16 @@ void MQTT_SerializeMQTTVec( uint8_t * pAllocatedMem,
{
const TransportOutVector_t * pTransportVec = pVec->pVector;
const size_t vecLen = pVec->vectorLen;
void * copyDest = NULL;
const void * copySource = NULL;
size_t index = 0;
size_t i = 0;

for( i = 0; i < vecLen; i++ )
{
( void ) memcpy( &pAllocatedMem[ index ], ( const uint8_t * ) pTransportVec[ i ].iov_base, pTransportVec[ i ].iov_len );
copyDest = &pAllocatedMem[ index ];
copySource = pTransportVec[ i ].iov_base;
( void ) memcpy( copyDest, copySource, pTransportVec[ i ].iov_len );
index += pTransportVec[ i ].iov_len;
}
}
Expand Down
22 changes: 11 additions & 11 deletions source/core_mqtt_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,8 @@ static uint8_t * encodeString( uint8_t * pDestination,
uint16_t sourceLength )
{
uint8_t * pBuffer = NULL;

/* Typecast const char * typed source buffer to const uint8_t *.
* This is to use same type buffers in memcpy. */
const uint8_t * pSourceBuffer = ( const uint8_t * ) pSource;
void * copyDest = NULL;
const void * copySource = NULL;

assert( pDestination != NULL );

Expand All @@ -546,9 +544,11 @@ static uint8_t * encodeString( uint8_t * pDestination,
pBuffer++;

/* Copy the string into pBuffer. */
if( pSourceBuffer != NULL )
copyDest = pBuffer;
copySource = pSource;
if( copySource != NULL )
{
( void ) memcpy( pBuffer, pSourceBuffer, sourceLength );
( void ) memcpy( copyDest, copySource, sourceLength );
}

/* Return the pointer to the end of the encoded string. */
Expand Down Expand Up @@ -713,7 +713,8 @@ static void serializePublishCommon( const MQTTPublishInfo_t * pPublishInfo,
bool serializePayload )
{
uint8_t * pIndex = NULL;
const uint8_t * pPayloadBuffer = NULL;
void * copyDest = NULL;
const void * copySource = NULL;

/* The first byte of a PUBLISH packet contains the packet type and flags. */
uint8_t publishFlags = MQTT_PACKET_TYPE_PUBLISH;
Expand Down Expand Up @@ -787,11 +788,10 @@ static void serializePublishCommon( const MQTTPublishInfo_t * pPublishInfo,
LogDebug( ( "Copying PUBLISH payload of length =%lu to buffer",
( unsigned long ) pPublishInfo->payloadLength ) );

/* Typecast const void * typed payload buffer to const uint8_t *.
* This is to use same type buffers in memcpy. */
pPayloadBuffer = ( const uint8_t * ) pPublishInfo->pPayload;
copyDest = pIndex;
copySource = pPublishInfo->pPayload;

( void ) memcpy( pIndex, pPayloadBuffer, pPublishInfo->payloadLength );
( void ) memcpy( copyDest, copySource, pPublishInfo->payloadLength );
/* Move the index to after the payload. */
pIndex = &pIndex[ pPublishInfo->payloadLength ];
}
Expand Down

0 comments on commit 08030c2

Please sign in to comment.