[PATCH] Psxtest: Fix String truncation warning

Chris Johns chrisj at rtems.org
Wed Sep 16 03:49:06 UTC 2020


On 15/9/20 7:04 pm, Aschref Ben-Thabet wrote:
> From: Aschref Ben Thabet <aschref.ben-thabet at embedded-brains.de>
> 
> Replace strncpy() with strdup() to silence this warning since it tries
> to allocate enough memory to hold the old string (plus a '\0' character
> to mark the end of the string).
> ---
>  testsuites/psxtests/psxndbm01/init.c | 11 +++--------
>  1 file changed, 3 insertions(+), 8 deletions(-)
> 
> diff --git a/testsuites/psxtests/psxndbm01/init.c b/testsuites/psxtests/psxndbm01/init.c
> index a13afa7315..ddcc1b5bf6 100644
> --- a/testsuites/psxtests/psxndbm01/init.c
> +++ b/testsuites/psxtests/psxndbm01/init.c
> @@ -217,14 +217,9 @@ rtems_task Init(rtems_task_argument ignored)
>    rtems_test_assert( strcmp( (const char*)get_phone_no.dptr, PHONE_NO2 ) == 0 );
>  
>    puts( "Fetch non-existing record and confirm error." );
> -  test_strings = (char*)malloc(6);
> -  strncpy( test_strings, "Hello", 5 );
> -
> -  test_strings[5] = '\0';
> +  test_strings = strdup( "Hello" );
>  
>  /* The data pointed by test_string is now pointed by key.dptr */
> -  key.dptr = test_strings;
> -  key.dsize = sizeof( test_strings );
>    get_phone_no = dbm_fetch( db, key );

Is key now uninitialised as the assignment above have been removed?

>    rtems_test_assert( get_phone_no.dptr == NULL );
>    dbm_close( db );
> @@ -237,10 +232,10 @@ rtems_task Init(rtems_task_argument ignored)
>    db = dbm_open( DB_NAME, O_RDWR, S_IRWXU );
>    rtems_test_assert( db != NULL );
>  
> -  puts( "Delete non-existing record and confirm error." );
> + /* puts( "Delete non-existing record and confirm error." );
>    rtems_test_assert( dbm_delete( db, key ) != 0 );
>    free( test_strings );
> -  rtems_test_assert( count_no_of_records( db ) == 2);
> +  rtems_test_assert( count_no_of_records( db ) == 2);*/

Why is this being commented out? Does this leaking the memory from strdup?

What about moving test_strings to the file scope and making it be:

char test_strings[PATH_MAX];

Then remove the pair of malloc and free calls.

Chris

>  
>    puts( "Delete existing record and "
>          "confirm that total number of records is successful 1." );
> 


More information about the devel mailing list