Skip to content

Commit ec48f06

Browse files
authored
Remove strnlen usage (#64)
* Remove unnecessary strnlen usage
1 parent 206ef74 commit ec48f06

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

modules/bg96/cellular_bg96_api.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999

100100
#define DATA_PREFIX_STRING "+QIRD:"
101101
#define DATA_PREFIX_STRING_LENGTH ( 6U )
102+
#define DATA_PREFIX_STRING_CHANGELINE_LENGTH ( 2U ) /* The length of the change line "\r\n". */
102103

103104
#define MAX_QIRD_STRING_PREFIX_STRING ( 14U ) /* The max data prefix string is "+QIRD: 1460\r\n" */
104105

@@ -1693,7 +1694,7 @@ static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext,
16931694
uint32_t * pDataLength )
16941695
{
16951696
char * pDataStart = NULL;
1696-
uint32_t tempStrlen = 0;
1697+
uint32_t prefixLineLength = 0U;
16971698
int32_t tempValue = 0;
16981699
CellularATError_t atResult = CELLULAR_AT_SUCCESS;
16991700
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
@@ -1722,6 +1723,7 @@ static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext,
17221723
if( ( pDataStart[ i ] == '\r' ) || ( pDataStart[ i ] == '\n' ) )
17231724
{
17241725
pDataStart[ i ] = '\0';
1726+
prefixLineLength = i;
17251727
break;
17261728
}
17271729
}
@@ -1735,14 +1737,12 @@ static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext,
17351737

17361738
if( pDataStart != NULL )
17371739
{
1738-
tempStrlen = strlen( "+QIRD:" );
1739-
atResult = Cellular_ATStrtoi( &pDataStart[ tempStrlen ], 10, &tempValue );
1740+
atResult = Cellular_ATStrtoi( &pDataStart[ DATA_PREFIX_STRING_LENGTH ], 10, &tempValue );
17401741

17411742
if( ( atResult == CELLULAR_AT_SUCCESS ) && ( tempValue >= 0 ) &&
17421743
( tempValue <= ( int32_t ) CELLULAR_MAX_RECV_DATA_LEN ) )
17431744
{
1744-
/* Save the start of data point in pTemp. */
1745-
if( ( uint32_t ) ( strnlen( pDataStart, MAX_QIRD_STRING_PREFIX_STRING ) + 2 ) > lineLength )
1745+
if( ( prefixLineLength + DATA_PREFIX_STRING_CHANGELINE_LENGTH ) > lineLength )
17461746
{
17471747
/* More data is required. */
17481748
*pDataLength = 0;
@@ -1751,9 +1751,9 @@ static CellularPktStatus_t socketRecvDataPrefix( void * pCallbackContext,
17511751
}
17521752
else
17531753
{
1754-
pDataStart = &pLine[ strnlen( pDataStart, MAX_QIRD_STRING_PREFIX_STRING ) ];
1754+
pDataStart = &pLine[ prefixLineLength ];
17551755
pDataStart[ 0 ] = '\0';
1756-
pDataStart = &pDataStart[ 2 ];
1756+
pDataStart = &pDataStart[ DATA_PREFIX_STRING_CHANGELINE_LENGTH ];
17571757
*pDataLength = ( uint32_t ) tempValue;
17581758
}
17591759

source/cellular_at_core.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,21 @@ static uint8_t _charToNibble( char c );
7474
static void validateString( const char * pString,
7575
CellularATStringValidationResult_t * pStringValidationResult )
7676
{
77-
size_t stringLength = 0U;
77+
const char * pNullCharacterLocation = NULL;
7878

79-
/* The strnlen() function returns strlen(s), if that is less than maxlen,
80-
* or maxlen if there is no null terminating ('\0') among the first
81-
* maxlen characters pointed to by s.
79+
/* Validate the string length. If the string length is longer than expected, return
80+
* error to stop further processing.
8281
*
83-
* stringLength == CELLULAR_AT_MAX_STRING_SIZE is valid because it means that
84-
* ( CELLULAR_AT_MAX_STRING_SIZE + 1 ) character is null terminating
85-
* character.*/
86-
stringLength = strnlen( pString, CELLULAR_AT_MAX_STRING_SIZE + 1U );
82+
* CELLULAR_AT_MAX_STRING_SIZE defines the valid string length excluding NULL terminating
83+
* character. The longest valid string has '\0' at ( CELLULAR_AT_MAX_STRING_SIZE + 1U )
84+
*/
85+
pNullCharacterLocation = memchr( pString, '\0', ( CELLULAR_AT_MAX_STRING_SIZE + 1U ) );
8786

88-
if( stringLength == 0U )
87+
if( pNullCharacterLocation == pString )
8988
{
9089
*pStringValidationResult = CELLULAR_AT_STRING_EMPTY;
9190
}
92-
else if( stringLength > CELLULAR_AT_MAX_STRING_SIZE )
91+
else if( pNullCharacterLocation == NULL )
9392
{
9493
*pStringValidationResult = CELLULAR_AT_STRING_TOO_LARGE;
9594
}
@@ -766,12 +765,23 @@ CellularATError_t Cellular_ATStrDup( char ** ppDst,
766765
char * p = NULL;
767766
CellularATError_t atStatus = CELLULAR_AT_SUCCESS;
768767
const char * pTempSrc = pSrc;
768+
CellularATStringValidationResult_t stringValidationResult = CELLULAR_AT_STRING_UNKNOWN;
769769

770-
if( ( ppDst == NULL ) || ( pTempSrc == NULL ) || ( strnlen( pTempSrc, CELLULAR_AT_MAX_STRING_SIZE ) >= CELLULAR_AT_MAX_STRING_SIZE ) )
770+
if( ( ppDst == NULL ) || ( pTempSrc == NULL ) )
771771
{
772772
atStatus = CELLULAR_AT_BAD_PARAMETER;
773773
}
774774

775+
if( atStatus == CELLULAR_AT_SUCCESS )
776+
{
777+
validateString( pTempSrc, &stringValidationResult );
778+
779+
if( stringValidationResult != CELLULAR_AT_STRING_VALID )
780+
{
781+
atStatus = CELLULAR_AT_BAD_PARAMETER;
782+
}
783+
}
784+
775785
if( atStatus == CELLULAR_AT_SUCCESS )
776786
{
777787
*ppDst = ( char * ) Platform_Malloc( sizeof( char ) * ( strlen( pTempSrc ) + 1U ) );

source/cellular_pktio.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,8 @@ static CellularPktStatus_t _handleMsgType( CellularContext_t * pContext,
728728

729729
static bool _findLineInStream( CellularContext_t * pContext,
730730
char * pLine,
731-
uint32_t bytesRead )
731+
uint32_t bytesRead,
732+
uint32_t * pLineLength )
732733
{
733734
bool keepProcess = true;
734735
char * pTempLine = pLine;
@@ -747,6 +748,7 @@ static bool _findLineInStream( CellularContext_t * pContext,
747748
if( i < bytesRead )
748749
{
749750
pTempLine[ i ] = '\0';
751+
*pLineLength = i;
750752
}
751753
else
752754
{
@@ -833,11 +835,6 @@ static bool _preprocessLine( CellularContext_t * pContext,
833835
/* This is the case AT command don't need data send or data receive prefix. */
834836
/* MISRA empty else. */
835837
}
836-
837-
if( keepProcess == true )
838-
{
839-
keepProcess = _findLineInStream( pContext, pTempLine, *pBytesRead );
840-
}
841838
}
842839

843840
return keepProcess;
@@ -877,17 +874,14 @@ static bool _handleDataResult( CellularContext_t * pContext,
877874
static bool _getNextLine( CellularContext_t * pContext,
878875
char ** ppLine,
879876
uint32_t * pBytesRead,
877+
uint32_t currentLineLength,
880878
CellularPktStatus_t pktStatus )
881879
{
882-
uint32_t stringLength = 0;
883880
bool keepProcess = true;
884881

885-
/* Find other responses or urcs which need to be processed in this read buffer. */
886-
stringLength = ( uint32_t ) strnlen( *ppLine, *pBytesRead );
887-
888-
/* Advanced 1 bytes to read next Line. */
889-
*ppLine = &( ( *ppLine )[ ( stringLength + 1U ) ] );
890-
*pBytesRead = *pBytesRead - ( stringLength + 1U );
882+
/* Advanced 1 byte to read next Line. */
883+
*ppLine = &( ( *ppLine )[ ( currentLineLength + 1U ) ] );
884+
*pBytesRead = *pBytesRead - ( currentLineLength + 1U );
891885
pContext->pPktioReadPtr = *ppLine;
892886
pContext->partialDataRcvdLen = *pBytesRead;
893887

@@ -915,6 +909,7 @@ static void _handleAllReceived( CellularContext_t * pContext,
915909
CellularPktStatus_t pktStatus = CELLULAR_PKT_STATUS_OK;
916910
char * pStartOfData = NULL, * pTempLine = pData;
917911
uint32_t bytesRead = bytesInBuffer;
912+
uint32_t currentLineLength = 0U;
918913
bool keepProcess = true;
919914

920915
while( keepProcess == true )
@@ -932,6 +927,11 @@ static void _handleAllReceived( CellularContext_t * pContext,
932927
/* Preprocess line. */
933928
keepProcess = _preprocessLine( pContext, pTempLine, &bytesRead, &pStartOfData );
934929

930+
if( keepProcess == true )
931+
{
932+
keepProcess = _findLineInStream( pContext, pTempLine, bytesRead, &currentLineLength );
933+
}
934+
935935
if( keepProcess == true )
936936
{
937937
/* A complete Line received. Get the message type. */
@@ -949,13 +949,13 @@ static void _handleAllReceived( CellularContext_t * pContext,
949949
}
950950
else
951951
{
952-
keepProcess = _getNextLine( pContext, &pTempLine, &bytesRead, pktStatus );
952+
keepProcess = _getNextLine( pContext, &pTempLine, &bytesRead, currentLineLength, pktStatus );
953953
}
954954
}
955955
else if( ( pktStatus == CELLULAR_PKT_STATUS_OK ) || ( pktStatus == CELLULAR_PKT_STATUS_PENDING_DATA ) )
956956
{
957957
/* Process AT reponse success. Get the next Line. */
958-
keepProcess = _getNextLine( pContext, &pTempLine, &bytesRead, pktStatus );
958+
keepProcess = _getNextLine( pContext, &pTempLine, &bytesRead, currentLineLength, pktStatus );
959959
}
960960
else
961961
{

0 commit comments

Comments
 (0)