Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.

Conversation

@ac000
Copy link
Member

@ac000 ac000 commented Jan 27, 2025

    Fix build with GCC 15
    
    The upcoming GCC 15 release introduces a new compiler warning,
    Wunterminated-string-initialization.
    
    This is intended to catch things like
    
      static const u_char  hex[16] = "0123456789ABCDEF";
    
    Where we are creating a character array from a string literal, but the
    specified size is not enough for the terminating NUL byte.
    
    In the above example that is intended as it is used as a lookup table
    and only the individual indices are accessed.
    
    As it happens, Unit uses the above idiom in a few places, triggering
    this warning (which we treat as an error by default).
    
    While I don't like disabling compiler warnings, lets just disable this
    one temporarily, as there is a patch in the works to make the
    "nonstring" variable attribute quell this warning.
    
    We just disable this on GCC as this isn't in Clang and we don't need to
    worry about older compilers as GCC silently ignores unknown -Wno-*
    options.
    
    Link: <https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=44c9403ed1833ae71a59e84f9e37af3182be0df5>
    Link: <https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Common-Variable-Attributes.html>
    Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178#c21>
    Cc: Alejandro Colomar <alx@kernel.org>
    Reviewed-by: Alejandro Colomar <alx@kernel.org>
    Signed-off-by: Andrew Clayton <a.clayton@nginx.com>

@ac000
Copy link
Member Author

ac000 commented Jan 27, 2025

This supplants #1543

Cc: @alejandro-colomar

@ac000 ac000 marked this pull request as ready for review January 27, 2025 17:57
@ac000 ac000 requested review from avahahn and hongzhidao January 27, 2025 17:57
Copy link
Contributor

@alejandro-colomar alejandro-colomar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed-by: Alejandro Colomar <alx@kernel.org>

(But I think hex[16] is precisely one of the places where this is not a false positive, and should be addressed by removing the 16.)

@ac000
Copy link
Member Author

ac000 commented Jan 27, 2025

Reviewed-by: Alejandro Colomar <alx@kernel.org>

(But I think hex[16] is precisely one of the places where this is not a false positive, and should be addressed by removing the 16.)

Hmm, but we don't need the terminating NUL byte... We simply access the individual characters (indices), 0-15... It's one of these lookup tables cases...

I do like specifying the size/length in these cases, if you always follow the rule, [n] for character arrays that don't require the NUL byte and [] for creating character string literals.

@ac000
Copy link
Member Author

ac000 commented Jan 27, 2025

  • Added a link to the commit that added this new warning
  • Added @alejandro-colomar 's Reviewed-By
$ git range-diff 3fc00a72...928cfe68
1:  3fc00a72 ! 1:  928cfe68 Fix build with GCC 15
    @@ Commit message
         worry about older compilers as GCC silently ignores unknown -Wno-*
         options.
     
    +    Link: <https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=44c9403ed1833ae71a59e84f9e37af3182be0df5>
         Link: <https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Common-Variable-Attributes.html>
         Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178#c21>
         Cc: Alejandro Colomar <alx@kernel.org>
    +    Reviewed-by: Alejandro Colomar <alx@kernel.org>
         Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
     
      ## auto/cc/test ##

@alejandro-colomar
Copy link
Contributor

alejandro-colomar commented Jan 27, 2025

Reviewed-by: Alejandro Colomar <alx@kernel.org>

(But I think hex[16] is precisely one of the places where this is not a false positive, and should be addressed by removing the 16.)

Hmm, but we don't need the terminating NUL byte... We simply access the individual characters (indices), 0-15... It's one of these lookup tables cases...

I do like specifying the size/length in these cases, if you always follow the rule, [n] for character arrays that don't require the NUL byte and [] for creating character string literals.

The thing is that the NUL wouldn't hurt at all, so I would do something slightly different:

[] for when a NUL doesn't hurt. [n] for when the NUL would hurt, and thus we need to actively remove it. This would favor strings, having non-strings only when strictly necessary.

But that's a minor thing. Whatever you prefer should be good. :)

@ac000
Copy link
Member Author

ac000 commented Jan 27, 2025

Reviewed-by: Alejandro Colomar <alx@kernel.org>

(But I think hex[16] is precisely one of the places where this is not a false positive, and should be addressed by removing the 16.)

Hmm, but we don't need the terminating NUL byte... We simply access the individual characters (indices), 0-15... It's one of these lookup tables cases...
I do like specifying the size/length in these cases, if you always follow the rule, [n] for character arrays that don't require the NUL byte and [] for creating character string literals.

The thing is that the NUL wouldn't hurt at all, so I would do something slightly different:

[] for when a NUL doesn't hurt. [n] for when the NUL would hurt, and thus we need to actively remove it. This would favor strings, having non-strings only when strictly necessary.

Heh, I think that's basically what I said (at least it's what I was trying to say...)

@alejandro-colomar
Copy link
Contributor

alejandro-colomar commented Jan 27, 2025 via email

The upcoming GCC 15 release introduces a new compiler warning,
Wunterminated-string-initialization.

This is intended to catch things like

  static const u_char  hex[16] = "0123456789ABCDEF";

Where we are creating a character array from a string literal, but the
specified size is not enough for the terminating NUL byte.

In the above example that is intended as it is used as a lookup table
and only the individual indices are accessed.

As it happens, Unit uses the above idiom in a few places, triggering
this warning (which we treat as an error by default).

While I don't like disabling compiler warnings, lets just disable this
one temporarily, as there is a patch in the works to make the
"nonstring" variable attribute quell this warning.

We just disable this on GCC as this isn't in Clang and we don't need to
worry about older compilers as GCC silently ignores unknown -Wno-*
options.

Link: <https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=44c9403ed1833ae71a59e84f9e37af3182be0df5>
Link: <https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Common-Variable-Attributes.html>
Link: <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117178#c21>
Cc: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
@ac000
Copy link
Member Author

ac000 commented Feb 3, 2025

Rebased with master

$ git range-diff 928cfe68...15037822
-:  -------- > 1:  7b7b29fc ruby: Fix build failures with Ruby 3.4
1:  928cfe68 = 2:  15037822 Fix build with GCC 15

@ac000 ac000 merged commit 1503782 into nginx:master Feb 3, 2025
23 checks passed
@ac000 ac000 deleted the no-wusi branch February 3, 2025 19:03
@andypost
Copy link
Contributor

FYI building 1.35.0 with gcc15 still reports warnings in test file

src/test/nxt_http_parse_test.c:74:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
   74 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:86:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
   86 |             "HTTP/1.2",
      |             ^~~~~~~~~~
src/test/nxt_utf8_file_name_test.c: In function 'nxt_utf8_file_name_test':
src/test/nxt_utf8_file_name_test.c:51:36: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (5 chars into 4 available) [-Werror=unterminated-string-initialization]
   51 |     static const u_char  utf8[4] = "UTF8";
      |                                    ^~~~~~
src/test/nxt_http_parse_test.c:98:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
   98 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:140:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
  140 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:152:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
  152 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:164:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
  164 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:176:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
  176 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:188:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
  188 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:200:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
  200 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:212:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
  212 |             "HTTP/1.0",
      |             ^~~~~~~~~~
src/test/nxt_http_parse_test.c:224:13: error: initializer-string for array of 'unsigned char' truncates NUL terminator but destination lacks 'nonstring' attribute (9 chars into 8 available) [-Werror=unterminated-string-initialization]
  224 |             "HTTP/1.1",
      |             ^~~~~~~~~~
cc1: all warnings being treated as errors
make: *** [build/Makefile:2290: build/utf8_file_name_test] Error 1
make: *** Waiting for unfinished jobs....
cc1: all warnings being treated as errors
make: *** [build/Makefile:2040: build/src/test/nxt_http_parse_test.o] Error 1

@ac000
Copy link
Member Author

ac000 commented Sep 14, 2025

Dammit! I keep forgetting those things exist... oh well, I guess if you
need to build those tests, build them with -Wno-unterminated-string-initialization. E.g.

$ make EXTRA_CFLAGS=-Wno-unterminated-string-initialization tests

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants