From 4763ef8d9b3d80599b8ac9f1f4a4c613f087c0e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20K=C3=BChndel?= Date: Mon, 5 Oct 2020 16:23:01 +0200 Subject: psxndbm01 - Fixing string truncation warning This fixes the following compiler warning: testsuites/psxtests/psxndbm01/init.c:221:3: warning: 'strncpy' output truncated before terminating nul copying 5 bytes from a string of the same length 221 | strncpy( test_strings, "Hello", 5 ); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In addition, the comments from Sebastian Huber on an old version of such a patch have been taken into account: 1) The use of `sizeof()` in `key.dsize = sizeof( test_strings );` is wrong. 2) There is no need to allocate the string. One can simply use a string constant. (See https://lists.rtems.org/pipermail/devel/2020-August/061418.html) --- 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..cc95684622 100644 --- a/testsuites/psxtests/psxndbm01/init.c +++ b/testsuites/psxtests/psxndbm01/init.c @@ -89,6 +89,7 @@ rtems_task Init(rtems_task_argument ignored) int i; char *test_strings; + char hello_string[] = "hello"; DBM *db; @@ -217,14 +218,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'; - /* The data pointed by test_string is now pointed by key.dptr */ - key.dptr = test_strings; - key.dsize = sizeof( test_strings ); + key.dptr = hello_string; + key.dsize = strlen( hello_string ) + 1; get_phone_no = dbm_fetch( db, key ); rtems_test_assert( get_phone_no.dptr == NULL ); dbm_close( db ); @@ -239,7 +235,6 @@ rtems_task Init(rtems_task_argument ignored) 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); puts( "Delete existing record and " -- cgit v1.2.3