Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macro checks for DNS Cache to fix build issue #1200

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/.cSpellWords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ BLXNS
bmcr
BMSR
BPDG
BPIALL
brgintclr
brginten
brgintstat
Expand Down
206 changes: 109 additions & 97 deletions source/FreeRTOS_DNS_Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,110 +56,114 @@
*
* @return If a fully formed name was found, then return the number of bytes processed in pucByte.
*/
size_t DNS_ReadNameField( ParseSet_t * pxSet,
size_t uxDestLen )
{
size_t uxNameLen = 0U;
size_t uxIndex = 0U;
size_t uxSourceLen = pxSet->uxSourceBytesRemaining;
const uint8_t * pucByte = pxSet->pucByte;

/* uxCount gets the values from pucByte and counts down to 0.
* No need to have a different type than that of pucByte */
size_t uxCount;
#if ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) )

if( uxSourceLen == ( size_t ) 0U )
size_t DNS_ReadNameField( ParseSet_t * pxSet,
size_t uxDestLen )
{
/* Return 0 value in case of error. */
uxIndex = 0U;
}
size_t uxNameLen = 0U;
size_t uxIndex = 0U;
size_t uxSourceLen = pxSet->uxSourceBytesRemaining;
const uint8_t * pucByte = pxSet->pucByte;

/* Determine if the name is the fully coded name, or an offset to the name
* elsewhere in the message. */
else if( ( pucByte[ uxIndex ] & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
{
/* Jump over the two byte offset. */
if( uxSourceLen > sizeof( uint16_t ) )
{
uxIndex += sizeof( uint16_t );
}
else
/* uxCount gets the values from pucByte and counts down to 0.
* No need to have a different type than that of pucByte */
size_t uxCount;

if( uxSourceLen == ( size_t ) 0U )
{
/* Return 0 value in case of error. */
uxIndex = 0U;
}
}
else
{
/* 'uxIndex' points to the full name. Walk over the string. */
while( ( uxIndex < uxSourceLen ) && ( pucByte[ uxIndex ] != ( uint8_t ) 0x00U ) )
{
/* If this is not the first time through the loop, then add a
* separator in the output. */
if( ( uxNameLen > 0U ) )
{
/*
* uxNameLen can never be greater than uxDestLen, since there are checks
* outside this condition, so the check is removed.
*/
pxSet->pcName[ uxNameLen ] = '.';
uxNameLen++;
}

/* Process the first/next sub-string. */
uxCount = ( size_t ) pucByte[ uxIndex ];

/* uxIndex should point to the first character now, unless uxCount
* is an offset field. */
uxIndex++;

if( ( uxIndex + uxCount ) > uxSourceLen )
/* Determine if the name is the fully coded name, or an offset to the name
* elsewhere in the message. */
else if( ( pucByte[ uxIndex ] & dnsNAME_IS_OFFSET ) == dnsNAME_IS_OFFSET )
{
/* Jump over the two byte offset. */
if( uxSourceLen > sizeof( uint16_t ) )
{
uxIndex = 0U;
break;
uxIndex += sizeof( uint16_t );
}

if( ( uxNameLen + uxCount ) >= uxDestLen )
else
{
uxIndex = 0U;
break;
}

while( uxCount-- != 0U )
{
/*
* uxNameLen can never be greater than uxDestLen, since there are checks
* outside this condition, so the check is removed.
*/
pxSet->pcName[ uxNameLen ] = ( char ) pucByte[ uxIndex ];
uxNameLen++;
uxIndex++;
}
}

/* Confirm that a fully formed name was found. */
if( uxIndex > 0U )
else
{
/* Here, there is no need to check for pucByte[ uxindex ] == 0 because:
* When we break out of the above while loop, uxIndex is made 0 thereby
* failing above check. Whenever we exit the loop otherwise, either
* pucByte[ uxIndex ] == 0 (which makes the check here unnecessary) or
* uxIndex >= uxSourceLen (which makes sure that we do not go in the 'if'
* case).
*/
if( uxIndex < uxSourceLen )
/* 'uxIndex' points to the full name. Walk over the string. */
while( ( uxIndex < uxSourceLen ) && ( pucByte[ uxIndex ] != ( uint8_t ) 0x00U ) )
{
pxSet->pcName[ uxNameLen ] = '\0';
/* If this is not the first time through the loop, then add a
* separator in the output. */
if( ( uxNameLen > 0U ) )
{
/*
* uxNameLen can never be greater than uxDestLen, since there are checks
* outside this condition, so the check is removed.
*/
pxSet->pcName[ uxNameLen ] = '.';
uxNameLen++;
}

/* Process the first/next sub-string. */
uxCount = ( size_t ) pucByte[ uxIndex ];

/* uxIndex should point to the first character now, unless uxCount
* is an offset field. */
uxIndex++;

if( ( uxIndex + uxCount ) > uxSourceLen )
{
uxIndex = 0U;
break;
}

if( ( uxNameLen + uxCount ) >= uxDestLen )
{
uxIndex = 0U;
break;
}

while( uxCount-- != 0U )
{
/*
* uxNameLen can never be greater than uxDestLen, since there are checks
* outside this condition, so the check is removed.
*/
pxSet->pcName[ uxNameLen ] = ( char ) pucByte[ uxIndex ];
uxNameLen++;
uxIndex++;
}
}
else

/* Confirm that a fully formed name was found. */
if( uxIndex > 0U )
{
uxIndex = 0U;
/* Here, there is no need to check for pucByte[ uxindex ] == 0 because:
* When we break out of the above while loop, uxIndex is made 0 thereby
* failing above check. Whenever we exit the loop otherwise, either
* pucByte[ uxIndex ] == 0 (which makes the check here unnecessary) or
* uxIndex >= uxSourceLen (which makes sure that we do not go in the 'if'
* case).
*/
if( uxIndex < uxSourceLen )
{
pxSet->pcName[ uxNameLen ] = '\0';
uxIndex++;
}
else
{
uxIndex = 0U;
}
}
}
}

return uxIndex;
}
return uxIndex;
}
#endif /* ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) ) */

/**
* @brief Simple routine that jumps over the NAME field of a resource record.
Expand Down Expand Up @@ -456,19 +460,23 @@
#if ( ipconfigUSE_IPv6 != 0 )
{
/*logging*/
FreeRTOS_printf( ( "prvParseDNS_HandleLLMNRRequest[%s]: type %04X\n", xSet.pcName, xSet.usType ) );
#if ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) )
FreeRTOS_printf( ( "prvParseDNS_HandleLLMNRRequest[%s]: type %04X\n", xSet.pcName, xSet.usType ) );
#endif /* ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) ) */

xEndPoint.usDNSType = ( uint8_t ) xSet.usType;
}
#endif /* ( ipconfigUSE_IPv6 != 0 ) */

/* If this is not a reply to our DNS request, it might be an mDNS or an LLMNR
* request. Ask the application if it uses the name. */
#if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 )
xDNSHookReturn = xApplicationDNSQueryHook( xSet.pcName );
#else
xDNSHookReturn = xApplicationDNSQueryHook_Multi( &xEndPoint, xSet.pcName );
#endif
#if ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change forces user to enable CACHE or CALLBACK while using MDNS/LLMNR, which I think is incorrect.

#if ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 )
xDNSHookReturn = xApplicationDNSQueryHook( xSet.pcName );
#else
xDNSHookReturn = xApplicationDNSQueryHook_Multi( &xEndPoint, xSet.pcName );
#endif /* ( ipconfigIPv4_BACKWARD_COMPATIBLE == 1 ) */
#endif /* ( ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) ) */

/* During the early stages of boot or after a DHCP lease expires, our end-point
* may have an IP address of 0.0.0.0. Do not respond to name queries with that address. */
Expand Down Expand Up @@ -735,10 +743,12 @@
&( pxSet->pucByte[ sizeof( DNSAnswerRecord_t ) ] ),
ipSIZE_OF_IPv6_ADDRESS );

if( ppxAddressInfo != NULL )
{
pxNewAddress = pxNew_AddrInfo( pxSet->pcName, FREERTOS_AF_INET6, xIP_Address.xIPAddress.xIP_IPv6.ucBytes );
}
#if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
Copy link
Member

@ActoryOu ActoryOu Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some more comments in the code to help developers understand why wrapping this is still working if pxNewAddress is NULL when CACHE/CALLBACK are disabled.

if( ppxAddressInfo != NULL )
{
pxNewAddress = pxNew_AddrInfo( pxSet->pcName, FREERTOS_AF_INET6, xIP_Address.xIPAddress.xIP_IPv6.ucBytes );
}
#endif /* ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) */

xIP_Address.xIs_IPv6 = pdTRUE;

Expand All @@ -763,12 +773,14 @@
pvCopyDest = &( pxSet->ulIPAddress );
( void ) memcpy( pvCopyDest, pvCopySource, pxSet->uxAddressLength );

if( ppxAddressInfo != NULL )
{
const uint8_t * ucBytes = ( uint8_t * ) &( pxSet->ulIPAddress );
#if ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 )
if( ppxAddressInfo != NULL )
{
const uint8_t * ucBytes = ( uint8_t * ) &( pxSet->ulIPAddress );

pxNewAddress = pxNew_AddrInfo( pxSet->pcName, FREERTOS_AF_INET4, ucBytes );
}
pxNewAddress = pxNew_AddrInfo( pxSet->pcName, FREERTOS_AF_INET4, ucBytes );
}
#endif /* ( ipconfigUSE_DNS_CACHE == 1 ) || ( ipconfigDNS_USE_CALLBACKS == 1 ) */

xIP_Address.xIPAddress.ulIP_IPv4 = pxSet->ulIPAddress;
xIP_Address.xIs_IPv6 = pdFALSE;
Expand Down
2 changes: 1 addition & 1 deletion test/Coverity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ see the [MISRA.md](https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA

## Getting Started
### Prerequisites
You can run this on a platform supported by Coverity. The list and other details can be found [here](https://sig-docs.synopsys.com/polaris/topics/c_coverity-compatible-platforms.html).
You can run this on a platform supported by Coverity. The list and other details can be found [here](https://documentation.blackduck.com/bundle/coverity-docs/page/deploy-install-guide/topics/supported_platforms_for_coverity_analysis.html).
To compile and run the Coverity target successfully, you must have the following:

1. CMake version > 3.13.0 (You can check whether you have this by typing `cmake --version`)
Expand Down
Loading