From 0f1fde850f6c9019eb8e4d7c1f026042201a82e4 Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Wed, 8 Feb 2023 21:04:08 +0000 Subject: [PATCH] Resolve compiler warning from gen_password() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit More recent g++ versions produce these warnings: test_PasswordRAMStore.cc: In member function ‘virtual void GParted::PasswordRAMStoreTest_TotalErasure_Test::TestBody()’: test_PasswordRAMStore.cc:61:32: warning: ‘ ’ directive output truncated writing 20 bytes into a region of size 10 [-Wformat-truncation=] snprintf( buf, sizeof( buf ), "password%03u ", i ); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ test_PasswordRAMStore.cc:61:10: note: ‘snprintf’ output 32 bytes into a destination of size 21 snprintf( buf, sizeof( buf ), "password%03u ", i ); ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ snprintf() [1] truncates the printed string to the specified size so didn't overflow the buffer. However clear the warning by making the formatted string always exactly 20 characters long, followed by the terminating NUL character to exactly fill the buffer. [1] print3(f) - Linux manual page https://man7.org/linux/man-pages/man3/snprintf.3.html "The functions snprintf() and vsnprintf() write at most size bytes (including the terminating null byte ('\0')) to str. " --- tests/test_PasswordRAMStore.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_PasswordRAMStore.cc b/tests/test_PasswordRAMStore.cc index 5cfd0d12..1c5a0288 100644 --- a/tests/test_PasswordRAMStore.cc +++ b/tests/test_PasswordRAMStore.cc @@ -58,7 +58,7 @@ static const char * gen_key( unsigned int i ) static const char * gen_passwd( unsigned int i ) { static char buf[21]; - snprintf( buf, sizeof( buf ), "password%03u ", i ); + snprintf(buf, sizeof(buf), "password%03u ", i%1000); return buf; }