Skip to content

Commit 52d47ca

Browse files
Fix MISRA violations
1 parent 65eba45 commit 52d47ca

File tree

4 files changed

+124
-125
lines changed

4 files changed

+124
-125
lines changed

MISRA.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,18 @@ _Ref 10.8.1_
6464
_Ref 14.3.1_
6565

6666
- MISRA Rule 14.3 The third-party http-parser library sets a uint64_t type field to
67-
`ULLONG_MAX` or `( ( uint64_t ) -1 )`, during its internal parsing. Coverity MISRA does not detect
68-
that this variable changes. This field is checked by the HTTP Client library.
69-
If the Content-Length header was found, then pHttpParser->content_length
67+
`ULLONG_MAX` or `( ( uint64_t ) -1 )`, during its internal parsing. Coverity MISRA
68+
does not detect that this variable changes. This field is checked by the HTTP
69+
Client library. If the Content-Length header was found, then pHttpParser->content_length
7070
will not be equal to the maximum 64 bit integer.
7171

72+
#### Rule 11.4
73+
_Ref 11.4.1_
74+
75+
- MISRA Rule 11.4 does not allow casting pointers to different data types as this may lead
76+
to misaligned pointers. But in this case, the pointers are casted to different data
77+
type to get the length of the header. It is not casted back to a pointer.
78+
7279
#### Rule 11.8
7380
_Ref 11.8.1_
7481

source/core_http_client.c

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,6 @@
4343
*/
4444
static uint32_t getZeroTimestampMs( void );
4545

46-
47-
HTTPStatus_t HTTPClient_SendHttpData( const TransportInterface_t * pTransport,
48-
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
49-
const uint8_t * pData,
50-
size_t dataLen );
51-
52-
53-
HTTPStatus_t HTTPClient_SendHttpHeaders( const TransportInterface_t * pTransport,
54-
HTTPClient_GetCurrentTimeFunc_t getTimestampMs,
55-
HTTPRequestHeaders_t * pRequestHeaders,
56-
size_t reqBodyLen,
57-
uint32_t sendFlags );
58-
5946
/**
6047
* @brief Adds the Content-Length header field and value to the
6148
* @p pRequestHeaders.
@@ -162,11 +149,6 @@ static HTTPStatus_t getFinalResponseStatus( HTTPParsingState_t parsingState,
162149
size_t totalReceived,
163150
size_t responseBufferLen );
164151

165-
166-
HTTPStatus_t HTTPClient_ReceiveAndParseHttpResponse( const TransportInterface_t * pTransport,
167-
HTTPResponse_t * pResponse,
168-
const HTTPRequestHeaders_t * pRequestHeaders );
169-
170152
/**
171153
* @brief Send the HTTP request over the network.
172154
*
@@ -668,7 +650,7 @@ static int httpParserOnStatusCallback( llhttp_t * pHttpParser,
668650
assert( pResponse != NULL );
669651

670652
/* Set the location of what to parse next. */
671-
pParsingContext->pBufferCur = pLoc + length;
653+
pParsingContext->pBufferCur = &pLoc[ length ];;
672654

673655
/* Initialize the first header field and value to be passed to the user
674656
* callback. */
@@ -748,7 +730,7 @@ static int httpParserOnHeaderFieldCallback( llhttp_t * pHttpParser,
748730
}
749731

750732
/* Set the location of what to parse next. */
751-
pParsingContext->pBufferCur = pLoc + length;
733+
pParsingContext->pBufferCur = &pLoc[ length ];;
752734

753735
/* The httpParserOnHeaderFieldCallback() always follows the
754736
* httpParserOnHeaderValueCallback() if there is another header field. When
@@ -794,7 +776,7 @@ static int httpParserOnHeaderValueCallback( llhttp_t * pHttpParser,
794776
pParsingContext = ( HTTPParsingContext_t * ) ( pHttpParser->data );
795777

796778
/* Set the location of what to parse next. */
797-
pParsingContext->pBufferCur = pLoc + length;
779+
pParsingContext->pBufferCur = &pLoc[ length ];;
798780

799781
/* If httpParserOnHeaderValueCallback() is invoked in succession, then the
800782
* last time llhttp_execute() was called only part of the header field
@@ -916,7 +898,7 @@ static int httpParserOnHeadersCompleteCallback( llhttp_t * pHttpParser )
916898
* parsing here. */
917899
if( ( pResponse->respOptionFlags & HTTP_RESPONSE_DO_NOT_PARSE_BODY_FLAG ) != 0U )
918900
{
919-
shouldContinueParse = LLHTTP_PAUSE_PARSING;
901+
shouldContinueParse = ( int ) LLHTTP_PAUSE_PARSING;
920902
}
921903

922904
return shouldContinueParse;
@@ -963,7 +945,7 @@ static int httpParserOnBodyCallback( llhttp_t * pHttpParser,
963945
/* MISRA Ref 11.8.1 [Removal of const from pointer] */
964946
/* More details at: https://github.com/FreeRTOS/coreHTTP/blob/main/MISRA.md#rule-118 */
965947
/* coverity[misra_c_2012_rule_11_8_violation] */
966-
pNextWriteLoc = ( char * ) ( pResponse->pBody + pResponse->bodyLen );
948+
pNextWriteLoc = ( char * ) ( &pResponse->pBody[ pResponse->bodyLen ] );
967949

968950
/* If the response is of type Transfer-Encoding: chunked, then actual body
969951
* will follow the the chunked header. This body data is in a later location
@@ -985,7 +967,7 @@ static int httpParserOnBodyCallback( llhttp_t * pHttpParser,
985967
pResponse->bodyLen += length;
986968

987969
/* Set the next location of parsing. */
988-
pParsingContext->pBufferCur = pLoc + length;
970+
pParsingContext->pBufferCur = &pLoc[ length ];;
989971

990972
LogDebug( ( "Response parsing: Found the response body: "
991973
"BodyLength=%lu",
@@ -1243,7 +1225,7 @@ static HTTPStatus_t parseHttpResponse( HTTPParsingContext_t * pParsingContext,
12431225
else
12441226
{
12451227
/* The next location to parse is after what has already been parsed. */
1246-
pParsingContext->pBufferCur = parsingStartLoc + parseLen;
1228+
pParsingContext->pBufferCur = &parsingStartLoc[ parseLen ];
12471229
}
12481230

12491231
returnStatus = processLlhttpError( &( pParsingContext->llhttpParser ) );
@@ -1378,18 +1360,18 @@ static HTTPStatus_t addHeader( HTTPRequestHeaders_t * pRequestHeaders,
13781360
assert( fieldLen != 0U );
13791361
assert( valueLen != 0U );
13801362

1381-
pBufferCur = ( char * ) ( pRequestHeaders->pBuffer + pRequestHeaders->headersLen );
1363+
pBufferCur = ( char * ) ( &pRequestHeaders->pBuffer[ pRequestHeaders->headersLen ] );
13821364
backtrackHeaderLen = pRequestHeaders->headersLen;
13831365

13841366
/* Backtrack before trailing "\r\n" (HTTP header end) if it's already written.
13851367
* Note that this method also writes trailing "\r\n" before returning.
13861368
* The first condition prevents reading before start of the header. */
13871369
if( ( HTTP_HEADER_END_INDICATOR_LEN <= pRequestHeaders->headersLen ) &&
1388-
( strncmp( ( char * ) pBufferCur - HTTP_HEADER_END_INDICATOR_LEN,
1370+
( strncmp( ( char * ) &pBufferCur[ 0U - HTTP_HEADER_END_INDICATOR_LEN ],
13891371
HTTP_HEADER_END_INDICATOR, HTTP_HEADER_END_INDICATOR_LEN ) == 0 ) )
13901372
{
13911373
backtrackHeaderLen -= HTTP_HEADER_LINE_SEPARATOR_LEN;
1392-
pBufferCur -= HTTP_HEADER_LINE_SEPARATOR_LEN;
1374+
pBufferCur = &pBufferCur[ 0U - HTTP_HEADER_LINE_SEPARATOR_LEN ];
13931375
}
13941376

13951377
/* Check if there is enough space in buffer for additional header. */
@@ -1411,14 +1393,14 @@ static HTTPStatus_t addHeader( HTTPRequestHeaders_t * pRequestHeaders,
14111393

14121394
if( returnStatus == HTTPSuccess )
14131395
{
1414-
pBufferCur += fieldLen;
1396+
pBufferCur = &pBufferCur[ fieldLen ];
14151397

14161398
/* Copy the field separator, ": ", into the buffer. */
14171399
( void ) memcpy( pBufferCur,
14181400
httpFieldSeparator,
14191401
HTTP_HEADER_FIELD_SEPARATOR_LEN );
14201402

1421-
pBufferCur += HTTP_HEADER_FIELD_SEPARATOR_LEN;
1403+
pBufferCur = &pBufferCur[ HTTP_HEADER_FIELD_SEPARATOR_LEN ];
14221404

14231405
/* Copy the header value into the buffer. */
14241406
if( httpHeaderStrncpy( pBufferCur, pValue, valueLen, HTTP_HEADER_STRNCPY_IS_VALUE ) == NULL )
@@ -1429,7 +1411,7 @@ static HTTPStatus_t addHeader( HTTPRequestHeaders_t * pRequestHeaders,
14291411

14301412
if( returnStatus == HTTPSuccess )
14311413
{
1432-
pBufferCur += valueLen;
1414+
pBufferCur = &pBufferCur[ valueLen ];
14331415

14341416
/* Copy the header end indicator, "\r\n\r\n" into the buffer. */
14351417
( void ) memcpy( pBufferCur,
@@ -1485,7 +1467,7 @@ static HTTPStatus_t addRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,
14851467

14861468
/* Write the range start value in the buffer. */
14871469
rangeValueLength += convertInt32ToAscii( rangeStartOrlastNbytes,
1488-
rangeValueBuffer + rangeValueLength,
1470+
&rangeValueBuffer[ rangeValueLength ],
14891471
sizeof( rangeValueBuffer ) - rangeValueLength );
14901472

14911473
/* Add remaining value data depending on the range specification type. */
@@ -1494,19 +1476,19 @@ static HTTPStatus_t addRangeHeader( HTTPRequestHeaders_t * pRequestHeaders,
14941476
if( rangeEnd != HTTP_RANGE_REQUEST_END_OF_FILE )
14951477
{
14961478
/* Write the "-" character to the buffer.*/
1497-
*( rangeValueBuffer + rangeValueLength ) = DASH_CHARACTER;
1479+
rangeValueBuffer[ rangeValueLength ] = DASH_CHARACTER;
14981480
rangeValueLength += DASH_CHARACTER_LEN;
14991481

15001482
/* Write the rangeEnd value of the request range to the buffer. */
15011483
rangeValueLength += convertInt32ToAscii( rangeEnd,
1502-
rangeValueBuffer + rangeValueLength,
1484+
&rangeValueBuffer[ rangeValueLength ],
15031485
sizeof( rangeValueBuffer ) - rangeValueLength );
15041486
}
15051487
/* Case when request is for bytes in the range [rangeStart, EoF). */
15061488
else if( rangeStartOrlastNbytes >= 0 )
15071489
{
15081490
/* Write the "-" character to the buffer.*/
1509-
*( rangeValueBuffer + rangeValueLength ) = DASH_CHARACTER;
1491+
rangeValueBuffer[ rangeValueLength ] = DASH_CHARACTER;
15101492
rangeValueLength += DASH_CHARACTER_LEN;
15111493
}
15121494
else
@@ -1565,32 +1547,32 @@ static HTTPStatus_t writeRequestLine( HTTPRequestHeaders_t * pRequestHeaders,
15651547
{
15661548
/* Write "<METHOD> <PATH> HTTP/1.1\r\n" to start the HTTP header. */
15671549
( void ) memcpy( pBufferCur, pMethod, methodLen );
1568-
pBufferCur += methodLen;
1550+
pBufferCur = &pBufferCur[ methodLen ];
15691551

15701552
*pBufferCur = SPACE_CHARACTER;
1571-
pBufferCur += SPACE_CHARACTER_LEN;
1553+
pBufferCur = &pBufferCur[ SPACE_CHARACTER_LEN ];
15721554

15731555
/* Use "/" as default value if <PATH> is NULL. */
15741556
if( ( pPath == NULL ) || ( pathLen == 0U ) )
15751557
{
15761558
( void ) memcpy( pBufferCur,
15771559
pHttpEmptyPath,
15781560
HTTP_EMPTY_PATH_LEN );
1579-
pBufferCur += HTTP_EMPTY_PATH_LEN;
1561+
pBufferCur = &pBufferCur[ HTTP_EMPTY_PATH_LEN ];
15801562
}
15811563
else
15821564
{
15831565
( void ) memcpy( pBufferCur, pPath, pathLen );
1584-
pBufferCur += pathLen;
1566+
pBufferCur = &pBufferCur[ pathLen ];
15851567
}
15861568

15871569
*pBufferCur = SPACE_CHARACTER;
1588-
pBufferCur += SPACE_CHARACTER_LEN;
1570+
pBufferCur = &pBufferCur[ SPACE_CHARACTER_LEN ];
15891571

15901572
( void ) memcpy( pBufferCur,
15911573
pHttpProtocolVersion,
15921574
HTTP_PROTOCOL_VERSION_LEN );
1593-
pBufferCur += HTTP_PROTOCOL_VERSION_LEN;
1575+
pBufferCur = &pBufferCur[ HTTP_PROTOCOL_VERSION_LEN ];
15941576
( void ) memcpy( pBufferCur,
15951577
pHeaderLineSeparator,
15961578
HTTP_HEADER_LINE_SEPARATOR_LEN );
@@ -1880,7 +1862,7 @@ HTTPStatus_t HTTPClient_SendHttpData( const TransportInterface_t * pTransport,
18801862
lastSendTimeMs = getTimestampMs();
18811863

18821864
bytesRemaining -= ( size_t ) bytesSent;
1883-
pIndex += bytesSent;
1865+
pIndex = &pIndex[ bytesSent ];
18841866
LogDebug( ( "Sent data over the transport: "
18851867
"BytesSent=%ld, BytesRemaining=%lu, TotalBytesSent=%lu",
18861868
( long int ) bytesSent,
@@ -2081,7 +2063,7 @@ HTTPStatus_t HTTPClient_ReceiveAndParseHttpResponse( const TransportInterface_t
20812063
{
20822064
/* Receive the HTTP response data into the pResponse->pBuffer. */
20832065
currentReceived = pTransport->recv( pTransport->pNetworkContext,
2084-
pResponse->pBuffer + totalReceived,
2066+
&pResponse->pBuffer[ totalReceived ],
20852067
pResponse->bufferLen - totalReceived );
20862068

20872069
/* Transport receive errors are negative. */
@@ -2157,6 +2139,10 @@ HTTPStatus_t HTTPClient_ReceiveAndParseHttpResponse( const TransportInterface_t
21572139
/* There may be dangling data if we parse with do not parse body flag.
21582140
* We expose this data through body to let the applications access it. */
21592141
pResponse->pBody = ( const uint8_t * ) parsingContext.pBufferCur;
2142+
2143+
/* MISRA Ref 11.4.1 [Casting pointer to int] */
2144+
/* More details at: https://github.com/FreeRTOS/coreHTTP/blob/main/MISRA.md#rule-114 */
2145+
/* coverity[misra_c_2012_rule_11_4_violation] */
21602146
pResponse->bodyLen = totalReceived - ( size_t ) ( ( ( uintptr_t ) pResponse->pBody ) - ( ( uintptr_t ) pResponse->pBuffer ) );
21612147
}
21622148

0 commit comments

Comments
 (0)