Skip to content

Commit

Permalink
test_eARPGetCacheEntryByMac_OneMatchingEntry(): Arrest dangling pointer
Browse files Browse the repository at this point in the history
This test was using the stack of a previously returned function
(probably a previous test). Highlights from AddressSanitizer output:

    ==15832==ERROR: AddressSanitizer: stack-use-after-return
    READ of size 8 at 0x7fdefb013670 thread T0
    #0 0x4325bf in eARPGetCacheEntryByMac source/FreeRTOS_ARP.c:930
    FreeRTOS#1 0x421a71 in test_eARPGetCacheEntryByMac_OneMatchingEntry
        (test/unit-test/build/bin/tests/FreeRTOS_ARP_utest+0x421a71)

    Address 0x7fdefb013670 is located in stack of thread T0 at offset 624 in frame
    #0 0x41f941 in test_vARPRefreshCacheEntry_IPAndMACInDifferentLocations1
        (test/unit-test/build/bin/tests/FreeRTOS_ARP_utest+0x41f941)

    This frame has 2 object(s):
    [48, 54) 'xMACAddress' (line 1937)
    [80, 640) 'xEndPoint' (line 1941) <== Memory access at offset 624 is inside this variable

Nulling the dangling pointer is enough to fix the test,
but in order to keep the 100% line coverage,
it must point at somewhere valid.
Therefore doing that.
  • Loading branch information
anordal committed May 26, 2024
1 parent 7f536eb commit 1117c6b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2034,29 +2034,33 @@ void test_eARPGetCacheEntryByMac_OneMatchingEntry( void )
eARPLookupResult_t eResult;
MACAddress_t xMACAddress = { 0x22, 0x22, 0x22, 0x22, 0x22, 0x22 };
int i;
struct xNetworkInterface * xInterface;
NetworkEndPoint_t xNetworkEndPoint = { 0 };
NetworkInterface_t xInterface, * pxInterface = NULL;
xNetworkEndPoint.pxNetworkInterface = &xInterface;

/* =================================================== */
/* Make sure one entry matches. */
for( i = 0; i < ipconfigARP_CACHE_ENTRIES; i++ )
{
xARPCache[ i ].ulIPAddress = 0xAABBCCDD;
xARPCache[ i ].pxEndPoint = &xNetworkEndPoint;
memset( xARPCache[ i ].xMACAddress.ucBytes, 0x11, sizeof( xMACAddress.ucBytes ) );
}

ulEntryToTest = 1;
memset( xARPCache[ ulEntryToTest ].xMACAddress.ucBytes, 0x22, sizeof( xMACAddress.ucBytes ) );
xARPCache[ ulEntryToTest ].ulIPAddress = 0xAABBCCEE;
eResult = eARPGetCacheEntryByMac( &xMACAddress, &ulIPAddress, &xInterface );
eResult = eARPGetCacheEntryByMac( &xMACAddress, &ulIPAddress, &pxInterface );
TEST_ASSERT_EQUAL( eARPCacheHit, eResult );
TEST_ASSERT_EQUAL( xARPCache[ ulEntryToTest ].ulIPAddress, ulIPAddress );
TEST_ASSERT_EQUAL( &xInterface, pxInterface );
/* =================================================== */
eResult = eARPGetCacheEntryByMac( &xMACAddress, &ulIPAddress, NULL );
TEST_ASSERT_EQUAL( eARPCacheHit, eResult );
TEST_ASSERT_EQUAL( xARPCache[ ulEntryToTest ].ulIPAddress, ulIPAddress );
/* =================================================== */
xARPCache[ ulEntryToTest ].pxEndPoint = NULL;
eResult = eARPGetCacheEntryByMac( &xMACAddress, &ulIPAddress, &xInterface );
eResult = eARPGetCacheEntryByMac( &xMACAddress, &ulIPAddress, &pxInterface );
TEST_ASSERT_EQUAL( eARPCacheHit, eResult );
TEST_ASSERT_EQUAL( xARPCache[ ulEntryToTest ].ulIPAddress, ulIPAddress );
/* =================================================== */
Expand Down Expand Up @@ -2305,6 +2309,7 @@ void test_eARPGetCacheEntry_NoCacheHit( void )
{
xARPCache[ i ].ulIPAddress = 0;
xARPCache[ i ].ucValid = ( uint8_t ) pdTRUE;
xARPCache[ i ].pxEndPoint = NULL;
}

ulSavedGatewayAddress = xNetworkAddressing.ulGatewayAddress;
Expand Down

0 comments on commit 1117c6b

Please sign in to comment.