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 Mar 17, 2025

As previously promised, this PR comprises three commits that allow us to re-enable the new GCC 15 warning Wunterminated-string-initialization.

  • The first commit adds a wrapper around __attribute__ ((__nonstring__))

As a reminder, this attribute is used to mark character arrays that are intentionally not NUL terminated.

  • The second commit tags various character arrays with this attribute.

  • The third commit removes the disabling of this new warning.

The following changes since commit d7afeb2b94f1cd72ed02403609e5484f9514e5eb:

  java: websocket: Additional payload length validation (2025-02-21 22:49:15 +0000)

are available in the Git repository at:

  git@github.com:ac000/unit.git wusi

for you to fetch changes up to ca60536172916b7c3e4ff62e20be1ae2f3de039d:

  auto/cc: gcc: Don't disable -Wunterminated-string-initialization (2025-03-19 17:58:37 +0000)

----------------------------------------------------------------
Andrew Clayton (3):
      auto/clang: Add a NXT_NONSTRING macro
      Tag various character arrays with NXT_NONSTRING
      auto/cc: gcc: Don't disable -Wunterminated-string-initialization

 auto/cc/test         |  4 ----
 auto/clang           | 14 ++++++++++++++
 src/nxt_clang.h      | 11 +++++++++++
 src/nxt_http_parse.c |  2 +-
 src/nxt_http_parse.h |  2 +-
 src/nxt_sprintf.c    |  4 ++--
 src/nxt_string.c     |  4 ++--
 7 files changed, 31 insertions(+), 10 deletions(-)

@ac000 ac000 force-pushed the wusi branch 3 times, most recently from 8dca58f to 6a27240 Compare March 18, 2025 05:49
@ac000 ac000 changed the title [WIP] Tag things with __attribute__ ((__nonstring__)) Tag some char arrays with __attribute__ ((__nonstring__)) Mar 18, 2025
@ac000 ac000 marked this pull request as ready for review March 18, 2025 20:58
@ac000
Copy link
Member Author

ac000 commented Mar 19, 2025

Cc: @alejandro-colomar

This is a wrapper around __attribute__ ((__nonstring__)). Traditionally
this was used to mark char array variables that intentionally lacked a
terminating NUL byte, this would then cause warning to either be quelled
or emitted for various memory/string functions.

GCC 15 introduced a new warning, Wunterminated-string-initialization,
which will always warn on things like

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

However this is very much intentionally not NUL terminated.

When the Wunterminated-string-initialization patch went in, the
"nonstriong" attribute didn't quell this warning, however a patch has
since gone in (prior to the GCC 15 release) to enable this attribute to
quell this warning.

In Unit we disabled this new warning with an eye to being able to
re-enable it again, this patch is the first in a series to do just that.

So the above example would become

  static const char hex[16] NXT_NONSTRING = "0123456789ABCDEF";

Link: <https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=44c9403ed1833ae71a59e84f9e37af3182be0df5>
Link: <https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=622968990beee7499e951590258363545b4a3b57>
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 Mar 19, 2025

@hongzhidao

Thanks to @alejandro-colomar pointing out my idiocy, your qualms about the nxt_http_ver_t union initialisation changes should be neutralised, I.e. they don't change now.

$ git range-diff 6a272400...ca605361
1:  52529aa2 ! 1:  764ad73f auto/clang: Add a NXT_NONSTRING macro
    @@ Commit message
         Link: <https://gcc.gnu.org/git/gitweb.cgi?p=gcc.git;h=622968990beee7499e951590258363545b4a3b57>
         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/clang ##
2:  34ae3a0f ! 2:  b53d03c2 Tag various character arrays with NXT_NONSTRING
    @@ Commit message
         happening.
     
         Cc: Alejandro Colomar <alx@kernel.org>
    +    Co-authored-by: Alejandro Colomar <alx@kernel.org>
    +    Signed-off-by: Alejandro Colomar <alx@kernel.org>
         Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
     
      ## src/nxt_http_parse.c ##
    -@@ src/nxt_http_parse.c: nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
    -     nxt_http_ver_t           ver;
    -     nxt_http_target_traps_e  trap;
    - 
    --    static const nxt_http_ver_t  http11 = { "HTTP/1.1" };
    --    static const nxt_http_ver_t  http10 = { "HTTP/1.0" };
    -+    static const u_char          http11_str[8] NXT_NONSTRING = "HTTP/1.1";
    -+    static const u_char          http10_str[8] NXT_NONSTRING = "HTTP/1.0";
    -+    static const nxt_http_ver_t  *http11 = (nxt_http_ver_t *)http11_str;
    -+    static const nxt_http_ver_t  *http10 = (nxt_http_ver_t *)http10_str;
    - 
    -     p = *pos;
    - 
    -@@ src/nxt_http_parse.c: nxt_http_parse_request_line(nxt_http_request_parse_t *rp, u_char **pos,
    - 
    -     nxt_memcpy(ver.str, &p[1], 8);
    - 
    --    if (nxt_fast_path(ver.ui64 == http11.ui64
    --                      || ver.ui64 == http10.ui64
    -+    if (nxt_fast_path(ver.ui64 == http11->ui64
    -+                      || ver.ui64 == http10->ui64
    -                       || (memcmp(ver.str, "HTTP/1.", 7) == 0
    -                           && ver.s.minor >= '0' && ver.s.minor <= '9')))
    -     {
     @@ src/nxt_http_parse.c: nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_char **pos,
          size_t    len;
          uint32_t  hash;
    @@ src/nxt_http_parse.c: nxt_http_parse_field_name(nxt_http_request_parse_t *rp, u_
          /*   \s ! " # $ % & ' ( ) * + ,        . /                 : ; < = > ?   */
              "\0\1\0\1\1\1\1\1\0\0\1\1\0" "-" "\1\0" "0123456789" "\0\0\0\0\0\0"
     
    + ## src/nxt_http_parse.h ##
    +@@ src/nxt_http_parse.h: typedef struct nxt_http_fields_hash_s    nxt_http_fields_hash_t;
    + 
    + 
    + typedef union {
    +-    u_char                    str[8];
    ++    u_char                    str[8] NXT_NONSTRING;
    +     uint64_t                  ui64;
    + 
    +     struct {
    +
      ## src/nxt_sprintf.c ##
     @@ src/nxt_sprintf.c: nxt_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args)
          nxt_sprintf_t        spf;
3:  6a272400 ! 3:  ca605361 auto/cc: gcc: Don't disable -Wunterminated-string-initialization
    @@ Commit message
     
         Fixes: 150378224 ("Fix build with GCC 15")
         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 ##

ac000 and others added 2 commits March 20, 2025 15:28
In Unit we have a number of character arrays which are intentionally not
NUL terminated.

With GCC 15 this

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

will trigger a warning like

  $ gcc -Wextra -c nonstring.c
  nonstring.c: In function ‘hexit’:
  nonstring.c:9:37: warning: initializer-string for array of ‘char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (17 chars into 16 available) [-Wunterminated-string-initialization]
      9 |         static const char hex[16] = "0123456789ABCDEF";
        |                                     ^~~~~~~~~~~~~~~~~~

By adding NXT_NONSTRING like

  static const char hex[16] NXT_NONSTRING = "0123456789ABCDEF";

we no longer get the warning.

Cc: Alejandro Colomar <alx@kernel.org>
Co-authored-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Now that we are able to use the "nonstring" variable attribute to quell
this warning, we no longer need to disable it.

The good thing is there was never a released version of GCC where the
warning couldn't be quelled by the attribute.

Fixes: 1503782 ("Fix build with GCC 15")
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 Mar 20, 2025

Fix a commit message.

$ git range-diff ca605361...ec1608b4
1:  b53d03c2 ! 1:  d9c2fd79 Tag various character arrays with NXT_NONSTRING
    @@ Commit message
     
         we no longer get the warning.
     
    -    We need to split the funky union initialisation in
    -    nxt_http_parse_request_line() into two parts, initialise the HTTP
    -    version string arrays then use them to initialise the unions, as you
    -    can't apply the attribute to non char base types.
    -
    -    It could be argued that this actually makes it clearer what is
    -    happening.
    -
         Cc: Alejandro Colomar <alx@kernel.org>
         Co-authored-by: Alejandro Colomar <alx@kernel.org>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
2:  ca605361 = 2:  ec1608b4 auto/cc: gcc: Don't disable -Wunterminated-string-initialization

@ac000 ac000 merged commit ec1608b into nginx:master Mar 20, 2025
24 checks passed
@ac000
Copy link
Member Author

ac000 commented Mar 20, 2025

Thanks guys!

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.

3 participants