Skip to content

Commit da065eb

Browse files
Unittest: Fix, enable and forbid specific warnings (#1228)
* Unittest: Fix building with -Werror=uninitialized * Unittest: Enable & ban specific warnings This commit also fixes 4 of these warnings, that occured once each, namely -Werror={switch,memset-elt-size,comment,unused-value}. * Fix uninitialized variables for vReleaseSinglePacketFromUDPSocket unit tests --------- Co-authored-by: Tony Josi <[email protected]>
1 parent 2994394 commit da065eb

25 files changed

+414
-659
lines changed

test/unit-test/CMakeLists.txt

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,51 @@ if( ${PROJECT_SOURCE_DIR} STREQUAL ${PROJECT_BINARY_DIR} )
1818
message( FATAL_ERROR "In-source build is not allowed. Please build in a separate directory, such as ${PROJECT_SOURCE_DIR}/build." )
1919
endif()
2020

21-
set(SANITIZE "" CACHE STRING "Comma-separated list of compiler sanitizers to enable; empty string disables.")
22-
if(NOT ${SANITIZE} STREQUAL "")
21+
set( SANITIZE "" CACHE STRING "Comma-separated list of compiler sanitizers to enable; empty string disables." )
22+
if( NOT ${SANITIZE} STREQUAL "" )
2323
add_compile_options(-fsanitize=${SANITIZE} -fno-sanitize-recover)
2424
add_link_options(-fsanitize=${SANITIZE} -fno-sanitize-recover)
2525
endif()
2626

27+
# Align with: test/build-combination/CMakeLists.txt
28+
add_compile_options(
29+
$<$<COMPILE_LANG_AND_ID:C,GNU>:-fdiagnostics-color=always>
30+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-fcolor-diagnostics>
31+
32+
### On the road to -Wall: Forbid warnings that need not be tolerated.
33+
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang>:-Werror=uninitialized> # definite use of uninitialized variable
34+
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang>:-Werror=array-bounds> # indexing outside sized array
35+
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang>:-Werror=switch> # enum switch with unhandled value
36+
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang>:-Werror=format> # format string wrong
37+
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang>:-Werror=format-security> # format string missing
38+
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang>:-Werror=comment> # comment within a comment
39+
$<$<COMPILE_LANG_AND_ID:C,GNU,Clang>:-Werror=unused-value> # "statement has no effect"
40+
$<$<COMPILE_LANG_AND_ID:C,GNU>:-Werror=maybe-uninitialized> # possible use of uninitialized variable
41+
$<$<COMPILE_LANG_AND_ID:C,GNU>:-Werror=memset-elt-size> # memsetting array by length instead of size
42+
# FIXME (part of -Wall):
43+
# -Werror=return-type # "control reaches end of non-void function"
44+
# -Werror=array-parameter # name clash
45+
# -Werror=implicit-function-declaration # need header
46+
# -Werror=unused-but-set-variable
47+
# -Werror=unused-variable
48+
# -Werror=unused-function
49+
# -Werror=unused-parameter
50+
# -Werror=missing-braces
51+
# -Werror=pointer-sign
52+
# -Werror=dangling-pointer
53+
# -Werror=vla
54+
55+
### Currently needed to build cleanly with Clang 19:
56+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-Werror>
57+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-Wno-pragma-pack>
58+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-Wno-unused-parameter>
59+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-Wno-return-type>
60+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-Wno-pointer-sign>
61+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-Wno-typedef-redefinition>
62+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-Wno-strict-prototypes>
63+
$<$<COMPILE_LANG_AND_ID:C,Clang>:-Wno-sometimes-uninitialized>
64+
)
65+
2766
# Set global path variables.
2867
get_filename_component( __MODULE_ROOT_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE )
2968
set( MODULE_ROOT_DIR ${__MODULE_ROOT_DIR} CACHE INTERNAL "FreeRTOS-Plus-TCP repository root." )

test/unit-test/FreeRTOS_BitConfig/FreeRTOS_BitConfig_utest.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,14 @@ void test_xBitConfig_read_uc_xHasError( void )
158158
void test_xBitConfig_read_uc_OutOfBoundRead( void )
159159
{
160160
BitConfig_t xConfig, * pxConfig = &xConfig;
161-
uint8_t * pucData;
162161
BaseType_t xResult = pdFALSE;
163162

164163
memset( pxConfig, 0, sizeof( BitConfig_t ) );
165164
pxConfig->xHasError = pdFALSE;
166165
pxConfig->uxIndex = 1;
167166
pxConfig->uxSize = SIZE_OF_BINARY_STREAM;
168167

169-
xResult = xBitConfig_read_uc( pxConfig, pucData, SIZE_OF_BINARY_STREAM );
168+
xResult = xBitConfig_read_uc( pxConfig, NULL, SIZE_OF_BINARY_STREAM );
170169

171170
TEST_ASSERT_EQUAL( pdFALSE, xResult );
172171
TEST_ASSERT_EQUAL( pdTRUE, pxConfig->xHasError );
@@ -463,13 +462,13 @@ void test_ulBitConfig_read_32_HappyPath( void )
463462

464463
void test_vBitConfig_write_uc_xHasError( void )
465464
{
466-
BitConfig_t xConfig;
467-
uint8_t * pucData;
465+
BitConfig_t xConfig = { 0 };
468466

469-
memset( &xConfig, 0, sizeof( BitConfig_t ) );
470467
xConfig.xHasError = pdTRUE;
471468

472-
vBitConfig_write_uc( &xConfig, pucData, SIZE_OF_BINARY_STREAM );
469+
vBitConfig_write_uc( &xConfig, NULL, SIZE_OF_BINARY_STREAM );
470+
471+
TEST_ASSERT_EQUAL( pdTRUE, xConfig.xHasError );
473472
}
474473

475474
/**
@@ -480,15 +479,13 @@ void test_vBitConfig_write_uc_xHasError( void )
480479

481480
void test_vBitConfig_write_uc_OutOfBoundWrite( void )
482481
{
483-
BitConfig_t xConfig;
484-
uint8_t * pucData;
482+
BitConfig_t xConfig = { 0 };
485483

486-
memset( &xConfig, 0, sizeof( BitConfig_t ) );
487484
xConfig.xHasError = pdFALSE;
488485
xConfig.uxIndex = SIZE_OF_BINARY_STREAM;
489486
xConfig.uxSize = SIZE_OF_BINARY_STREAM;
490487

491-
vBitConfig_write_uc( &xConfig, pucData, SIZE_OF_BINARY_STREAM );
488+
vBitConfig_write_uc( &xConfig, NULL, SIZE_OF_BINARY_STREAM );
492489

493490
TEST_ASSERT_EQUAL( pdTRUE, xConfig.xHasError );
494491
}

test/unit-test/FreeRTOS_DHCPv6/FreeRTOS_DHCPv6_utest.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ static void vAddBitOperation( eTestDHCPv6BitOperationType_t eType,
128128
TEST_ASSERT_LESS_THAN( TEST_DHCPv6_BIT_OPERATION_MAX_SIZE, ulSize );
129129
memcpy( xTestDHCPv6BitOperation[ ulTestDHCPv6BitOperationWriteIndex ].val.ucValCustom, pvVal, ulSize );
130130
break;
131+
132+
case eTestDHCPv6BitOperationNone:
133+
case eTestDHCPv6BitOperationSetError:
134+
case eTestDHCPv6BitOperationReturnFalse:
135+
break;
131136
}
132137

133138
TEST_ASSERT_LESS_THAN_size_t( TEST_DHCPv6_BIT_OPERATION_DEBUG_MSG_MAX_SIZE, strlen( pucDebugMsg ) );

test/unit-test/FreeRTOS_DNS_Cache/FreeRTOS_DNS_Cache_utest.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,6 @@ void test_processDNS_CACHE_exceed_IP_entry_limit( void )
331331
pxIP_2.xIs_IPv6 = 0;
332332
pxIP_2.xIPAddress.ulIP_IPv4 = pulIP;
333333

334-
memset( pulIP_arr, 123, ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY );
335-
336334
for( i = 0; i < ipconfigDNS_CACHE_ADDRESSES_PER_ENTRY; i++ )
337335
{
338336
pxIP[ i ].xIPAddress.ulIP_IPv4 = pulIP_arr[ i ];

test/unit-test/FreeRTOS_DNS_Callback/FreeRTOS_DNS_Callback_utest.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ typedef void (* FOnDNSEvent ) ( const char * /* pcName */,
6666
static int callback_called = 0;
6767

6868
/* The second element is for the flexible array member
69-
* /* when pvPortMalloc is mocked to return this object.
69+
* when pvPortMalloc is mocked to return this object.
7070
*/
7171
static DNSCallback_t dnsCallback[ 2 ];
7272

test/unit-test/FreeRTOS_DNS_Networking/FreeRTOS_DNS_Networking_utest.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,36 +132,18 @@ void test_CreateSocket_success( void )
132132
TEST_ASSERT_EQUAL( ( Socket_t ) 235, s );
133133
}
134134

135-
/**
136-
* @brief Ensures that when the socket could not be created or could not be found, null is returned
137-
*/
138-
void test_BindSocket_fail( void )
139-
{
140-
struct freertos_sockaddr xAddress;
141-
Socket_t xSocket;
142-
uint16_t usPort;
143-
uint32_t ret;
144-
145-
FreeRTOS_bind_ExpectAnyArgsAndReturn( 0 );
146-
147-
ret = DNS_BindSocket( xSocket, usPort );
148-
149-
TEST_ASSERT_EQUAL( 0, ret );
150-
}
151-
152135
/**
153136
* @brief Happy path!
154137
*/
155138
void test_BindSocket_success( void )
156139
{
157140
struct freertos_sockaddr xAddress;
158-
Socket_t xSocket;
159-
uint16_t usPort;
141+
struct xSOCKET xSocket;
160142
uint32_t ret;
161143

162144
FreeRTOS_bind_ExpectAnyArgsAndReturn( 1 );
163145

164-
ret = DNS_BindSocket( xSocket, usPort );
146+
ret = DNS_BindSocket( &xSocket, 80 );
165147

166148
TEST_ASSERT_EQUAL( 1, ret );
167149
}

0 commit comments

Comments
 (0)