From 4c4223a16a2bb1d22d9acf889004abbd3e8e122c Mon Sep 17 00:00:00 2001 From: Tony Josi Date: Tue, 18 Jun 2024 10:27:32 +0530 Subject: [PATCH] Replace strcpy with strncpy (#1157) * Replace strcpy with strncpy * Fix CBMC --- source/FreeRTOS_DNS.c | 2 +- source/FreeRTOS_DNS_Cache.c | 2 +- source/FreeRTOS_DNS_Callback.c | 2 +- test/cbmc/proofs/DNS/CreateDNSMessage/Makefile.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/source/FreeRTOS_DNS.c b/source/FreeRTOS_DNS.c index 7ebda549a..0eb249e4c 100644 --- a/source/FreeRTOS_DNS.c +++ b/source/FreeRTOS_DNS.c @@ -1517,7 +1517,7 @@ uxIndex = uxStart + 1U; /* Copy in the host name. */ - ( void ) strcpy( ( char * ) &( pucUDPPayloadBuffer[ uxIndex ] ), pcHostName ); + ( void ) strncpy( ( char * ) &( pucUDPPayloadBuffer[ uxIndex ] ), pcHostName, strlen( pcHostName ) + 1U ); /* Walk through the string to replace the '.' characters with byte * counts. pucStart holds the address of the byte count. Walking the diff --git a/source/FreeRTOS_DNS_Cache.c b/source/FreeRTOS_DNS_Cache.c index 4c3f5715a..7c8e23638 100644 --- a/source/FreeRTOS_DNS_Cache.c +++ b/source/FreeRTOS_DNS_Cache.c @@ -448,7 +448,7 @@ /* Add or update the item. */ if( strlen( pcName ) < ( size_t ) ipconfigDNS_CACHE_NAME_LENGTH ) { - ( void ) strcpy( xDNSCache[ uxFreeEntry ].pcName, pcName ); + ( void ) strncpy( xDNSCache[ uxFreeEntry ].pcName, pcName, ipconfigDNS_CACHE_NAME_LENGTH ); ( void ) memcpy( &( xDNSCache[ uxFreeEntry ].xAddresses[ 0 ] ), pxIP, sizeof( *pxIP ) ); diff --git a/source/FreeRTOS_DNS_Callback.c b/source/FreeRTOS_DNS_Callback.c index 5a040c90b..15bddbfe5 100644 --- a/source/FreeRTOS_DNS_Callback.c +++ b/source/FreeRTOS_DNS_Callback.c @@ -155,7 +155,7 @@ vDNSTimerReload( FreeRTOS_min_uint32( 1000U, ( uint32_t ) uxTimeout ) ); } - ( void ) strcpy( pxCallback->pcName, pcHostName ); + ( void ) strncpy( pxCallback->pcName, pcHostName, lLength + 1U ); pxCallback->pCallbackFunction = pCallbackFunction; pxCallback->pvSearchID = pvSearchID; pxCallback->uxRemainingTime = uxTimeout; diff --git a/test/cbmc/proofs/DNS/CreateDNSMessage/Makefile.json b/test/cbmc/proofs/DNS/CreateDNSMessage/Makefile.json index 5bb6f67d9..8961c19ff 100644 --- a/test/cbmc/proofs/DNS/CreateDNSMessage/Makefile.json +++ b/test/cbmc/proofs/DNS/CreateDNSMessage/Makefile.json @@ -7,7 +7,7 @@ [ "--unwind 1", "--unwindset strlen.0:{HOSTNAME_UNWIND}", - "--unwindset strcpy.0:{HOSTNAME_UNWIND}", + "--unwindset strncpy.0:{HOSTNAME_UNWIND}", "--unwindset __CPROVER_file_local_FreeRTOS_DNS_c_prvCreateDNSMessage.0:{HOSTNAME_UNWIND}", "--unwindset __CPROVER_file_local_FreeRTOS_DNS_c_prvCreateDNSMessage.1:{HOSTNAME_UNWIND}", "--nondet-static"