[PATCH] selftests/nolibc: avoid GCC 15 -Wunterminated-string-initialization

Thomas Weißschuh posted 1 patch 3 months, 2 weeks ago
tools/testing/selftests/nolibc/nolibc-test.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] selftests/nolibc: avoid GCC 15 -Wunterminated-string-initialization
Posted by Thomas Weißschuh 3 months, 2 weeks ago
On GCC 15 the following warnings is emitted:

nolibc-test.c: In function ‘run_stdlib’:
nolibc-test.c:1416:32: warning: initializer-string for array of ‘char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (11 chars into 10 available) [-Wunterminated-string-initialization]
 1416 |                 char buf[10] = "test123456";
      |                                ^~~~~~~~~~~~

Increase the size of buf to avoid the warning.

It would also be possible to use __attribute__((nonstring)) but that
would require some ifdeffery to work with older compilers.

Fixes: 1063649cf531 ("selftests/nolibc: Add tests for strlcat() and strlcpy()")
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
 tools/testing/selftests/nolibc/nolibc-test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index dbe13000fb1ac153e9a89f627492daeb584a05d4..52640d8ae402b9e34174ae798e74882ca750ec2b 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -1413,7 +1413,7 @@ int run_stdlib(int min, int max)
 		 * Add some more chars after the \0, to test functions that overwrite the buffer set
 		 * the \0 at the exact right position.
 		 */
-		char buf[10] = "test123456";
+		char buf[11] = "test123456";
 		buf[4] = '\0';
 
 

---
base-commit: eb135311083100b6590a7545618cd9760d896a86
change-id: 20250623-nolibc-nonstring-7fe6974552b5

Best regards,
-- 
Thomas Weißschuh <linux@weissschuh.net>

Re: [PATCH] selftests/nolibc: avoid GCC 15 -Wunterminated-string-initialization
Posted by Rodrigo Campos 3 months, 2 weeks ago
On 6/23/25 11:34 PM, Thomas Weißschuh wrote:
> On GCC 15 the following warnings is emitted:
> 
> nolibc-test.c: In function ‘run_stdlib’:
> nolibc-test.c:1416:32: warning: initializer-string for array of ‘char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (11 chars into 10 available) [-Wunterminated-string-initialization]
>   1416 |                 char buf[10] = "test123456";
>        |                                ^~~~~~~~~~~~
> 
> Increase the size of buf to avoid the warning.
> 
> It would also be possible to use __attribute__((nonstring)) but that
> would require some ifdeffery to work with older compilers.
> 
> Fixes: 1063649cf531 ("selftests/nolibc: Add tests for strlcat() and strlcpy()")
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
>   tools/testing/selftests/nolibc/nolibc-test.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index dbe13000fb1ac153e9a89f627492daeb584a05d4..52640d8ae402b9e34174ae798e74882ca750ec2b 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -1413,7 +1413,7 @@ int run_stdlib(int min, int max)
>   		 * Add some more chars after the \0, to test functions that overwrite the buffer set
>   		 * the \0 at the exact right position.
>   		 */
> -		char buf[10] = "test123456";
> +		char buf[11] = "test123456";
>   		buf[4] = '\0';

Reviewed-by: Rodrigo Campos <rodrigo@sdfg.com.ar>

Sorry for the typo :)



Best,
Rodrigo
Re: [PATCH] selftests/nolibc: avoid GCC 15 -Wunterminated-string-initialization
Posted by Willy Tarreau 3 months, 2 weeks ago
On Mon, Jun 23, 2025 at 11:34:32PM +0200, Thomas Weißschuh wrote:
> On GCC 15 the following warnings is emitted:
> 
> nolibc-test.c: In function 'run_stdlib':
> nolibc-test.c:1416:32: warning: initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute (11 chars into 10 available) [-Wunterminated-string-initialization]
>  1416 |                 char buf[10] = "test123456";
>       |                                ^~~~~~~~~~~~
> 
> Increase the size of buf to avoid the warning.
> 
> It would also be possible to use __attribute__((nonstring)) but that
> would require some ifdeffery to work with older compilers.

I agree with you, that's not an important enough fight to spend time
on :-)

> Fixes: 1063649cf531 ("selftests/nolibc: Add tests for strlcat() and strlcpy()")
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Acked-by: Willy Tarreau <w@1wt.eu>

willy