Skip to content

Commit 870b352

Browse files
authored
[CoreHttp] Add strncasecmp implementation (#106)
* cbmc changes * Function changes * memory statistics changes * make file changes * Addressing Review comments * Addressing PRBR comments
1 parent 0e4ad23 commit 870b352

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

docs/doxygen/include/size_table.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
</tr>
1010
<tr>
1111
<td>core_http_client.c</td>
12-
<td><center>3.1K</center></td>
13-
<td><center>2.5K</center></td>
12+
<td><center>3.2K</center></td>
13+
<td><center>2.6K</center></td>
1414
</tr>
1515
<tr>
1616
<td>http_parser.c (http-parser)</td>
@@ -19,7 +19,7 @@
1919
</tr>
2020
<tr>
2121
<td><b>Total estimates</b></td>
22-
<td><b><center>18.8K</center></b></td>
23-
<td><b><center>15.5K</center></b></td>
22+
<td><b><center>18.9K</center></b></td>
23+
<td><b><center>15.6K</center></b></td>
2424
</tr>
2525
</table>

lexicon.txt

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ sendflags
233233
sendhttpbody
234234
sendhttpheaders
235235
sendpartialcall
236+
sensitivity
236237
sizeof
237238
snprintf
238239
statuscode

source/core_http_client.c

+37-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <assert.h>
2929
#include <string.h>
30+
#include <ctype.h>
3031

3132
#include "core_http_client.h"
3233
#include "core_http_client_private.h"
@@ -544,6 +545,22 @@ static void processCompleteHeader( HTTPParsingContext_t * pParsingContext );
544545
*/
545546
static HTTPStatus_t processHttpParserError( const http_parser * pHttpParser );
546547

548+
/**
549+
* @brief Compares at most the first n bytes of str1 and str2 without case sensitivity
550+
* and n must be less than the actual size of either string.
551+
*
552+
* @param[in] str1 First string to be compared.
553+
* @param[in] str2 Second string to be compared.
554+
* @param[in] n The maximum number of characters to be compared.
555+
*
556+
* @return One of the following:
557+
* 0 if str1 is equal to str2
558+
* 1 if str1 is not equal to str2.
559+
*/
560+
static int8_t caseInsensitiveStringCmp( const char * str1,
561+
const char * str2,
562+
size_t n );
563+
547564
/*-----------------------------------------------------------*/
548565

549566
static uint32_t getZeroTimestampMs( void )
@@ -553,6 +570,25 @@ static uint32_t getZeroTimestampMs( void )
553570

554571
/*-----------------------------------------------------------*/
555572

573+
static int8_t caseInsensitiveStringCmp( const char * str1,
574+
const char * str2,
575+
size_t n )
576+
{
577+
size_t i = 0U;
578+
579+
for( i = 0U; i < n; i++ )
580+
{
581+
if( toupper( str1[ i ] ) != toupper( str2[ i ] ) )
582+
{
583+
break;
584+
}
585+
}
586+
587+
return ( i == n ) ? 0 : 1;
588+
}
589+
590+
/*-----------------------------------------------------------*/
591+
556592
static void processCompleteHeader( HTTPParsingContext_t * pParsingContext )
557593
{
558594
HTTPResponse_t * pResponse = NULL;
@@ -2237,7 +2273,7 @@ static int findHeaderFieldParserCallback( http_parser * pHttpParser,
22372273
/* Check whether the parsed header matches the header we are looking for. */
22382274
/* Each header field consists of a case-insensitive field name (RFC 7230, section 3.2). */
22392275
if( ( fieldLen == pContext->fieldLen ) &&
2240-
( strncasecmp( pContext->pField, pFieldLoc, fieldLen ) == 0 ) )
2276+
( caseInsensitiveStringCmp( pContext->pField, pFieldLoc, fieldLen ) == 0 ) )
22412277
{
22422278
LogDebug( ( "Found header field in response: "
22432279
"HeaderName=%.*s, HeaderLocation=0x%p",

test/cbmc/proofs/findHeaderFieldParserCallback/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ HARNESS_FILE=$(HARNESS_ENTRY)
77

88
# The header field length is bounded, so strncasecmp can be unwound to an expected
99
# amount that won't make the proof run too long.
10-
MAX_HEADER_FIELD_LENGTH=10
10+
MAX_HEADER_FIELD_LENGTH=11
1111

1212
DEFINES += -DMAX_HEADER_FIELD_LENGTH=$(MAX_HEADER_FIELD_LENGTH)
1313
INCLUDES +=
1414

1515
REMOVE_FUNCTION_BODY +=
16-
UNWINDSET += strncasecmp.0:$(MAX_HEADER_FIELD_LENGTH)
16+
UNWINDSET += __CPROVER_file_local_core_http_client_c_caseInsensitiveStringCmp.0:$(MAX_HEADER_FIELD_LENGTH)
1717

1818
PROOF_SOURCES += $(PROOFDIR)/$(HARNESS_FILE).c
1919
PROOF_SOURCES += $(SRCDIR)/test/cbmc/sources/http_cbmc_state.c

test/cbmc/proofs/findHeaderFieldParserCallback/findHeaderFieldParserCallback_harness.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void findHeaderFieldParserCallback_harness()
4646
pResponse->pBuffer != NULL &&
4747
pResponse->bufferLen > 0 );
4848

49-
__CPROVER_assume( 0 < fieldLen && fieldLen <= MAX_HEADER_FIELD_LENGTH && fieldLen <= pResponse->bufferLen );
49+
__CPROVER_assume( 0 < fieldLen && fieldLen < MAX_HEADER_FIELD_LENGTH && fieldLen <= pResponse->bufferLen );
5050
__CPROVER_assume( fieldOffset <= pResponse->bufferLen - fieldLen );
5151
pFieldLoc = pResponse->pBuffer + fieldOffset;
5252

0 commit comments

Comments
 (0)