[rtems commit] psxndbm01 - Fixing string truncation warning

Sebastian Huber sebh at rtems.org
Mon Oct 12 06:50:02 UTC 2020


Module:    rtems
Branch:    master
Commit:    4763ef8d9b3d80599b8ac9f1f4a4c613f087c0e0
Changeset: http://git.rtems.org/rtems/commit/?id=4763ef8d9b3d80599b8ac9f1f4a4c613f087c0e0

Author:    Frank Kühndel <frank.kuehndel at embedded-brains.de>
Date:      Mon Oct  5 16:23:01 2020 +0200

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 a13afa7..cc95684 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 "



More information about the vc mailing list