Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag some char arrays with __attribute__((__nonstring__)) #871

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ac000
Copy link
Member

@ac000 ac000 commented Mar 20, 2025

GCC 15 implements a new warning -Wunterminated-string-initialization that is also enabled by -Wextra that we enable.

This causes compilation failures (due to -Werror) due to the likes of

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

E.g.

src/njs_string.h: In function ‘njs_string_encode’:
src/njs_string.h:212:36: error: initializer-string for array of ‘unsigned char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (17 chars into 16 available) [-Werror=unterminated-string-initialization]
  212 |     static const u_char  hex[16] = "0123456789ABCDEF";
      |                                    ^~~~~~~~~~~~~~~~~~

These are very much meant not to be NUL terminated.

Now we could just disable this new warning. But I think it is worth leaving it enabled (the GCC developers also obviously feel it's useful enough to enable under -Wexta), anything that helps the compiler help us avoid silly mistakes is a good thing(tm), particularly in the current climate.

So rather than disable this warning, we can make use of the GCC "nonstring" variable attribute __attribute__((__nonstring__)).

This attribute is used to mark character arrays that are intentionally not NUL terminated.

So the above example would become (we of course wrap it in a more friendly name NJS_NONSTRING)

static const u_char  hex[16] NJS_NONSTRING = "0123456789ABCDEF";
  • The first commit adds a wrapper around __attribute__((__nonstring__))

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

Note: This was done with an eye towards the atomic strings work which removes the need for one of the attribute tags.

The following changes since commit d34fcb03cf2378a644a3c7366d58cbddc2771cbd:

  HTTP: fixed r.subrequest() error handling. (2024-06-12 15:09:21 -0700)

are available in the Git repository at:

  [email protected]:ac000/njs.git wusi

for you to fetch changes up to a00cc6d3897393f723d9f11f78651f4cbcb8cc76:

  Tag various character arrays with NJS_NONSTRING (2025-03-20 17:32:58 +0000)

----------------------------------------------------------------
Andrew Clayton (2):
      auto/clang: Add a NJS_NONSTRING macro
      Tag various character arrays with NJS_NONSTRING

 auto/clang        | 14 ++++++++++++++
 src/njs_clang.h   |  8 ++++++++
 src/njs_sprintf.c |  4 ++--
 src/njs_string.c  |  2 +-
 src/njs_string.h  |  2 +-
 5 files changed, 26 insertions(+), 4 deletions(-)

@ac000
Copy link
Member Author

ac000 commented Apr 11, 2025

Rebased with master

$ git range-diff a00cc6d3...35e6dcc5
  -:  -------- >   1:  9d4bf6c6 Version 0.8.5.
  -:  -------- >   2:  59bba976 Modules: removed not needed previous location initialization.
  -:  -------- >   3:  a14be61c HTTP: simplified check for subrequest from a subrequest.
  -:  -------- >   4:  1c2c7af3 Fixed ‘length’ may be used uninitialized in Array.prototype.pop().
  -:  -------- >   5:  8c7ade42 Fixed ‘ctx.codepoint’ may be used uninitialized.
  -:  -------- >   6:  184d2a39 Modules: adding NUL byte at the end of the module body.
  -:  -------- >   7:  ed631d25 Fixed maybe-uninitialized warning in error creation.
  -:  -------- >   8:  286d00b9 Fixed attribute initialization for external properties.
  -:  -------- >   9:  69072164 Fixed Object.values() and Object.entries() with shared properties.
  -:  -------- >  10:  89aca305 Fixed "global" property handler when deleting.
  -:  -------- >  11:  7f55a51f Fixed constructor property of an iterator object.
  -:  -------- >  12:  911cacd0 Avoiding explicit length calculation for strings.
  -:  -------- >  13:  c773ebca QuickJS: disabling eval() and string normalize.
  -:  -------- >  14:  7dda4b2f HTTP: simplifed r.subrequest() code.
  -:  -------- >  15:  b593dd4a Fixed Function constructor handling when called without arguments.
  -:  -------- >  16:  7f10ae25 Fixed empty labelled statement in a function.
  -:  -------- >  17:  15c66f2a Version bump.
  -:  -------- >  18:  2197bf31 Tests: ignoring subrequest execution order in js_subrequest.t.
  -:  -------- >  19:  22b2300d Tests: removed njs specific code from js_headers.t.
  -:  -------- >  20:  e45429fe HTTP: making ngx_http_js_header_t handler type generic.
  -:  -------- >  21:  62b80029 HTTP: moving ngx_http_methods table out of subrequest() method.
  -:  -------- >  22:  af7847fa Modules: removed non needed argument from meta handlers.
  -:  -------- >  23:  29c71bf7 Tests: making exception test more portable in js.t.
  -:  -------- >  24:  3ac49680 Tests: adapting unsafe redirect test for QuickJS.
  -:  -------- >  25:  4630230c Types: fixed NgxKeyValuePair definition.
  -:  -------- >  26:  8553c2a6 HTTP: expose capture group variables.
  -:  -------- >  27:  5d15a8d6 Fixed Buffer.prototype.lastIndexOf().
  -:  -------- >  28:  7ba3a8b9 Fixed Buffer.prototype.writeFloat() and friends.
  -:  -------- >  29:  5cf4fd02 Fixed Fixed Buffer.prototype.write().
  -:  -------- >  30:  43dcb8dd Fixed Buffer.prototype.writeInt8() and friends.
  -:  -------- >  31:  4b818885 QuickJS: fixed exception handling in shell output.
  -:  -------- >  32:  d1c615ea QuickJS: added buffer module.
  -:  -------- >  33:  58b58146 Modules: adding NUL byte at the end of the global script.
  -:  -------- >  34:  d47f7bed Making ngx_js_logger() reusable by QuickJS code.
  -:  -------- >  35:  598fc578 Fixed compilation on 32bit platforms.
  -:  -------- >  36:  0d2b8b53 Add badges to README.md
  -:  -------- >  37:  5b7905c5 Modules: added nocache flag for js_set variables.
  -:  -------- >  38:  82c5b5ad Introduced libqjs.a.
  -:  -------- >  39:  b70ab370 QuickJS: moving njs object creation to common code.
  -:  -------- >  40:  cb8296fa QuickJS: qjs_new_context() accepts now additional modules.
  -:  -------- >  41:  65f1e855 QuickJS: added wrappers for strings creation.
  -:  -------- >  42:  8e02600d QuickJS: disabling eval() and Function() in qjs_new_context().
  -:  -------- >  43:  a3a4dd45 Tests: making exception test more portable in stream_js.t.
  -:  -------- >  44:  6259cca2 Fixed handling of encode arg in fs.readdir() and fs.realpath().
  -:  -------- >  45:  28719815 Configure: accept spaces in PATH.
  -:  -------- >  46:  50e6a114 CI: removed DUMP_LEAKS flag from QuickJS build.
  -:  -------- >  47:  9b674412 Modules: introduced engine API.
  -:  -------- >  48:  201b1276 Modules: introduced QuickJS engine.
  -:  -------- >  49:  c2bc8c65 HTTP: fixed r.subrequest() check for nested subrequests.
  -:  -------- >  50:  6e6a9c5f HTTP: fixed r.return() with empty string as a body argument.
  -:  -------- >  51:  603563f2 CI: Run a check-pr job on a specific Ubuntu version.
  -:  -------- >  52:  c5a29a7a Version 0.8.6.
  -:  -------- >  53:  f2e18cde Version bump.
  -:  -------- >  54:  efa5e7d6 Removed deprecated $262.byteString().
  -:  -------- >  55:  0279dff2 Tests: external code for unit tests is rewritten using public API.
  -:  -------- >  56:  34e32252 Fixed dead store assignment in TextEncoder.encodeInto().
  -:  -------- >  57:  4bf2d068 Style.
  -:  -------- >  58:  cf889c11 Removed deprecated njs_string_set().
  -:  -------- >  59:  30a96583 Benchmark: code is rewritten using public API.
  -:  -------- >  60:  3a4349e6 Optimized ngx_qjs_string().
  -:  -------- >  61:  39a2d4bf Optimized qjs_to_bytes().
  -:  -------- >  62:  7b9ae660 Test262: reporting name of the testsuite for a failed test.
  -:  -------- >  63:  6c8084b6 Fixed heap-buffer-overflow in Buffer.prototype.indexOf().
  -:  -------- >  64:  e0563f4d Fixed Buffer.prototype.lastIndexOf() when `from` is provided.
  -:  -------- >  65:  5a8bb2d1 Fixed Buffer.prototype.indexOf() and friends.
  -:  -------- >  66:  78a34bf7 CI: added 32bit test in check-pr job.
  -:  -------- >  67:  6902aaa1 Fixed Buffer.prototype.indexOf() on 32bits platforms.
  -:  -------- >  68:  685b64f0 Modules: removed extra VMs creation when it is not needed.
  -:  -------- >  69:  8759db74 Fixed dead store assignment in r.subrequest().
  -:  -------- >  70:  6ed6c100 CI: aligned asan build with with post-commit CI asan build.
  -:  -------- >  71:  eda6fa7d Implemented lazy stack symbolization.
  -:  -------- >  72:  6ed70de1 HTTP: added strict check for js_body_filter syntax.
  -:  -------- >  73:  19853903 Tests: added js_body_filter test with non-UTF8 data.
  -:  -------- >  74:  1b2a3391 FS: introduced fs.readlinkSync() and friends.
  -:  -------- >  75:  ed36e942 Improved error messages for module loading failures.
  -:  -------- >  76:  ba6b9e15 Version 0.8.7.
  -:  -------- >  77:  ac8b69af Version bump.
  -:  -------- >  78:  073a34c5 Updated README with QuickJS module build instructions.
  -:  -------- >  79:  606ceb24 QuickJS: improved library discovery.
  -:  -------- >  80:  0ed2de6e Tests: removed njs specific code from js_shared_dict.t.
  -:  -------- >  81:  352c2e59 Modules: added js_shared_dict_zone support in QuickJS engine.
  -:  -------- >  82:  98fff325 QuickJS: reworked process object.
  -:  -------- >  83:  361a7fc6 QuickJS: introduced process.kill().
  -:  -------- >  84:  8666e211 Introduced process.kill() function.
  -:  -------- >  85:  07545f16 Modules: fixed process.env object.
  -:  -------- >  86:  1a6cc6d2 FS: removed fs.promises.readSync() added by mistake.
  -:  -------- >  87:  e845d5e5 Fixed building by Clang.
  -:  -------- >  88:  597b1fd1 XML: fixed tests with libxml2 2.13 and later.
  -:  -------- >  89:  e476ef91 XML: improved XMLNode.$tags handler.
  -:  -------- >  90:  72f0b5db Added missing steps for building with QuickJS to README.
  -:  -------- >  91:  5247aac9 Fixed resolving when Promise is inherited.
  -:  -------- >  92:  983b397b Throwing exception when prototype is not found.
  -:  -------- >  93:  11c36e92 Fixed absolute scope in cloned VMs.
  -:  -------- >  94:  16f42c87 Allow to execute some code before cloning.
  -:  -------- >  95:  283282f3 Modules: refactored preloading.
  -:  -------- >  96:  b300a933 HTTP: fixed limit rated output.
  -:  -------- >  97:  f2955c8b Fetch: optimized use of SSL contexts.
  -:  -------- >  98:  78e3edf7 Version 0.8.8.
  -:  -------- >  99:  ca23804e Tests: skipping stream_js_periodic.t for QuickJS engine.
  -:  -------- > 100:  1442b5ea Test262: fixed check for crypto object.
  -:  -------- > 101:  958f8f09 Test262: making "fs" module required.
  -:  -------- > 102:  dfec49b1 Test262: renaming fs tests.
  -:  -------- > 103:  524431e0 Test262: collecting all tests results before exiting.
  -:  -------- > 104:  d6d74ba3 Test262: skipping individual tests.
  -:  -------- > 105:  3ca33c05 Test262: running tests within their own directory.
  -:  -------- > 106:  1f8f9992 QuickJS: added fs module.
  -:  -------- > 107:  4fb1c0ca Version bump.
  -:  -------- > 108:  855aa4c9 Modules: removed extra VM creation per server.
  -:  -------- > 109:  17676e5c 2025 year.
  -:  -------- > 110:  b87ad67a Version 0.8.9.
  -:  -------- > 111:  1aabab0c Tests: added request body test when body is in a file.
  -:  -------- > 112:  e92e2af9 Tests: moving request body tests to js_request_body.t.
  -:  -------- > 113:  1a482317 QuickJS: accept ArrayBuffer as an arument for qjs_typed_array_data().
  -:  -------- > 114:  447d66d4 QuickJS: added TextDecoder and TextEncoder.
  -:  -------- > 115:  13b37cbc QuickJS: fixed Buffer.concat() with a single argument.
  -:  -------- > 116:  de248a1c Version bump.
  -:  -------- > 117:  70f75ed0 QuickJS: reimplemented process.argv.
  -:  -------- > 118:  9f15b698 QuickJS: correctly handling value len for empty query params.
  -:  -------- > 119:  d4f12ad9 QuickJS: added support for QuickJS-NG library.
  -:  -------- > 120:  e257fba8 CI: added test with QuickJS-NG in check-pr job.
  -:  -------- > 121:  75ca26f8 QuickJS: added WebCrypto module.
  -:  -------- > 122:  2d97e804 HTTP: reading r.requestText or r.requestBuffer from a temp file.
  -:  -------- > 123:  a5e4a756 Tests262: fixed merge() with null values.
  -:  -------- > 124:  246f4ca0 QuickJS: fixed shared dict in stream module.
  -:  -------- > 125:  f289dcb9 QuickJS: added querystring module.
  -:  -------- > 126:  ae7d4f42 Modules: fixed name corruption in variable and header processing.
  -:  -------- > 127:  2abc2a12 Add missed syntax error for await in template literal.
  -:  -------- > 128:  0b6eca0a Fixed access to uninitialized alg in SubtleCrypto.import().
  -:  -------- > 129:  b6d108c8 QuickJS: fixed non-NULL terminated strings formatting in exceptions.
  -:  -------- > 130:  b065b411 QuickJS: fixed key usage processing with invalid values in WebCrypto.
  -:  -------- > 131:  74ef8d95 QuickJS: added missed OPENSSL context for errors in WebCrypto.
  -:  -------- > 132:  54f18a10 QuickJS: added WebCrypto import tests forgotten in 75ca26f.
  -:  -------- > 133:  13f0787f QuickJS: fixed SharedDict.incr() with empty init argument.
  -:  -------- > 134:  1b451958 QuickJS: fixed memory leak in js_periodic handler.
  -:  -------- > 135:  89634204 Tests: splitting js_periodic tests into multiple files.
  -:  -------- > 136:  24633b37 Modules: improved reporting of unhandled promise rejections.
  -:  -------- > 137:  62f863a8 Fixed typo introduced in 75ca26f.
  -:  -------- > 138:  3045f312 Tests: making fetch test portable by removing njs.dump().
  -:  -------- > 139:  18977e02 Fetch: accepting response headers with underscore characters.
  -:  -------- > 140:  d145e033 QuickJS: removed unused variable casts introduced in 75ca26f.
  -:  -------- > 141:  18d31701 QuickJS: using helper to declare Symbol.toStringTag properties.
  -:  -------- > 142:  9010aeea QuickJS: making ngx_qjs_*() functions consistent with ngx_js_*().
  -:  -------- > 143:  93e73cf1 Test262: using default prepare_args() where appropriate.
  -:  -------- > 144:  3ad475cb Test262: allowing to omit empty default option argument.
  -:  -------- > 145:  d5359d17 QuickJS: crypto module.
  -:  -------- > 146:  6736121e QuickJS: added error checks in modules initialization.
  -:  -------- > 147:  f6a2a795 QuickJS: fixed njs_qjs_object_completions().
  -:  -------- > 148:  1b7adef4 QuickJS: fixed ngx_qjs_string() to handle strings containing "\0".
  -:  -------- > 149:  1d36e231 QuickJS: calling njs_chb_destroy() in qjs_string_create_chb() internally.
  -:  -------- > 150:  f3454f01 QuickJS: using JS_AddIntrinsicBigInt() if detected.
  -:  -------- > 151:  02ccc116 QuickJS: added missed JS_NewClass() for the SharedDictError class.
  -:  -------- > 152:  87086636 QuickJS: introduced qjs_promise_result().
  -:  -------- > 153:  76a5e458 QuickJS: fixed compatibility with recent change in upstream.
  -:  -------- > 154:  4ee93af0 Test262: improved compatibility with node.js for fs/methods.t.mjs.
  -:  -------- > 155:  1f1dd0ae XML: fixed serializeToString().
  -:  -------- > 156:  f89644e7 XML: allowing to remove the property $text for XMLNode.
  -:  -------- > 157:  cec9a165 QuickJS: added xml module.
  -:  -------- > 158:  211f229e QuickJS: fixed compatibility issues with QuickJS-NG 0.9.0.
  -:  -------- > 159:  fd004b1b QuickJS: fixed ngx_qjs_external_resolver().
  -:  -------- > 160:  a4d0f947 QuickJS: fix exception handling during configuration phase.
  -:  -------- > 161:  f678c904 QuickJS: added xml to nginx modules.
  -:  -------- > 162:  113fac87 QuickJS: ignoring rejected promises while destroying context.
  -:  -------- > 163:  9d3e71ca Version 0.8.10.
  -:  -------- > 164:  b5d64ed6 Version bump.
  -:  -------- > 165:  0c9c847d Fixed typo in stream event handler debug message.
  -:  -------- > 166:  4f96013f Fetch: remove unused parameter in ngx_js_http_error().
  -:  -------- > 167:  48070f44 Fetch: generalize ngx_js_http_error().
  -:  -------- > 168:  3aa2ed4b Fetch: renamed njs_js_http_destructor() as ngx_js_http_destructor().
  -:  -------- > 169:  befd1d60 Fetch: refactored out ngx_js_http_resolve_done().
  -:  -------- > 170:  78b55af1 Fetch: refactored out ngx_js_http_close_peer().
  -:  -------- > 171:  dbf556f2 Fetch: refactored out ngx_js_http_resolve().
  1:  11de5380 ! 172:  deaceec4 auto/clang: Add a NJS_NONSTRING macro
    @@ Metadata
     Author: Andrew Clayton <[email protected]>
     
      ## Commit message ##
    -    auto/clang: Add a NJS_NONSTRING macro
    +    auto/clang: Add a NJS_NONSTRING macro.
     
         This is a wrapper around __attribute__((__nonstring__)). Traditionally
         this was used to mark char array variables that intentionally lacked a
  2:  a00cc6d3 ! 173:  35e6dcc5 Tag various character arrays with NJS_NONSTRING
    @@ Metadata
     Author: Andrew Clayton <[email protected]>
     
      ## Commit message ##
    -    Tag various character arrays with NJS_NONSTRING
    +    Tag various character arrays with NJS_NONSTRING.
     
         In njs we have a number of character arrays which are intentionally not
         NUL terminated.

@xeioex
Copy link
Contributor

xeioex commented Apr 11, 2025

Hi @ac000,

The patches look good. How do you install gcc-15? Do you build it from sources? gcc-15 is not released yet.

@ac000
Copy link
Member Author

ac000 commented Apr 11, 2025

Hi @xeioex

I was testing on Fedora Rawhide.

Fedora 42 is due out on Tuesday and will include GCC 15 (or at least a snapshot of 15.1)...

However building GCC from source is not hard, this is what I do

$ wget http://www.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-x.x.x/gcc-x.x.x.tar.xz
$ tar -xf gcc-x.x.x.tar.xz
$ cd gcc-x.x.x
$ ./contrib/download_prerequisites
$ cd ..

$ mkdir gcc-x.x.x.build
$ cd gcc-x.x.x.build
$ ../gcc-x.x.x/configure --prefix=/opt/gcc-x.x.x --disable-multilib && make -j3 && echo "BUILD OK. Now INSTALL"
$ sudo make install

EDIT: Latest GCC 15 snapshot is available here.

@xeioex
Copy link
Contributor

xeioex commented Apr 12, 2025

@ac000

Is the patch ready for review?

@ac000
Copy link
Member Author

ac000 commented Apr 12, 2025

Sure, let me mark it as such...

@ac000 ac000 marked this pull request as ready for review April 12, 2025 00:04
Copy link
Contributor

@xeioex xeioex left a comment

Choose a reason for hiding this comment

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

auto/clang: Add a NJS_NONSTRING macro.

  1. we use in this repo nginx-like style.
  2. do we need the article here? isn't NJS_NONSTRING unique?

auto/clang: added NJS_NONSTRING macro.

Please, also fix QuickJS parts as well.

To build with QuickJS

  1. build QuickJS
git clone https://github.com/bellard/quickjs
cd quickjs
CFLAGS='-fPIC' make libquickjs.a

./configure --cc-opt='-I/path/to/quickjs' --ld-opt='-L/path/to/quickjs' && make njs

places I found

diff --git a/external/qjs_query_string_module.c b/external/qjs_query_string_module.c
index bb787229..1d8c637f 100644
--- a/external/qjs_query_string_module.c
+++ b/external/qjs_query_string_module.c
@@ -537,7 +537,7 @@ qjs_string_encode(const uint32_t *escape, size_t size, const u_char *src,
     u_char *dst)
 {
     uint8_t              byte;
-    static const u_char  hex[16] = "0123456789ABCDEF";
+    static const u_char  hex[16] NJS_NONSTRING = "0123456789ABCDEF";
 
     do {
         byte = *src++;
diff --git a/src/qjs_buffer.c b/src/qjs_buffer.c
index a45f57ce..a5cbf4c6 100644
--- a/src/qjs_buffer.c
+++ b/src/qjs_buffer.c
@@ -2354,7 +2354,7 @@ qjs_hex_encode(JSContext *ctx, const njs_str_t *src, njs_str_t *dst)
     size_t        i, len;
     const u_char  *start;
 
-    static const u_char  hex[16] = "0123456789abcdef";
+    static const u_char  hex[16] NJS_NONSTRING = "0123456789abcdef";
 
     len = src->length;
     start = src->start;

@xeioex
Copy link
Contributor

xeioex commented Apr 12, 2025

Also, after the patches gcc-15 is still failing to build. But on top of this work it succeeds.

@ac000
Copy link
Member Author

ac000 commented Apr 12, 2025

auto/clang: Add a NJS_NONSTRING macro.

1. we use in this repo nginx-like style.

2. do we need the article here? isn't NJS_NONSTRING unique?

auto/clang: added NJS_NONSTRING macro.

You mean using past-tense rather than the normal imperative mood? I detest it, but if you insist... however there are at least

4f96013f Fetch: remove unused parameter in ngx_js_http_error().
a4d0f947 QuickJS: fix exception handling during configuration phase.
16f42c87 Allow to execute some code before cloning.
603563f2 CI: Run a check-pr job on a specific Ubuntu version.
...

Please, also fix QuickJS parts as well.

I'll take a look.

@ac000
Copy link
Member Author

ac000 commented Apr 12, 2025

Also, after the patches gcc-15 is still failing to build. But on top of this work it succeeds.

master with these patches plus your quickjs patch builds cleanly with GCC 15 for me (with QJS enabled).

What warning/error are you seeing?

@xeioex
Copy link
Contributor

xeioex commented Apr 12, 2025

@ac000

What warning/error are you seeing?

In file included from src/njs_main.h:43,
                 from src/njs_symbol.c:8:
src/njs_symbol.c:24:40: error: initializer-string for array of ‘unsigned char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (15 chars into 14 available) [-Werror=unterminated-string-initialization]
   24 |                             njs_string("Symbol.replace");
      |                                        ^~~~~~~~~~~~~~~~
src/njs_value.h:417:18: note: in definition of macro ‘njs_string’
  417 |         .start = s,                                                           \
      |                  ^
src/njs_symbol.c:28:40: error: initializer-string for array of ‘unsigned char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (15 chars into 14 available) [-Werror=unterminated-string-initialization]
   28 |                             njs_string("Symbol.species");
      |                                        ^~~~~~~~~~~~~~~~
src/njs_value.h:417:18: note: in definition of macro ‘njs_string’
  417 |         .start = s,                                                           \
      |                  ^
cc1: all warnings being treated as errors
make: *** [build/Makefile:480: build/src/njs_symbol.o] Error 1

@xeioex
Copy link
Contributor

xeioex commented Apr 12, 2025

@ac000

You mean using past-tense rather than the normal imperative mood? I detest it, but if you insist... however there are at least

Yes, the past tense. I agree that for new projects something like conventional commits should be used, but I would like to stick to naming convention user in this project for consistency.

however there are at least

yes, sometime it percolates.

@ac000
Copy link
Member Author

ac000 commented Apr 12, 2025

I remember now... these patches were done with a view to the atomic keys work having been merged where a problematic array is removed.

I can either re-spin these patches to fully work with master, or wait until the atomic keys patches are in, keeping in mind GCC 15 will be in one popular distribution from Tuesday... so if it will be a while before that's merged then perhaps I should re-spin...

@xeioex
Copy link
Contributor

xeioex commented Apr 12, 2025

@ac000

I as suggested, it makes sense to merge your patches after atomic strings. We plan to merge the atoms in the next release.
By the end of this month, I hope.

@ac000 ac000 force-pushed the wusi branch 2 times, most recently from f97a2c4 to d7db165 Compare April 12, 2025 06:20
@ac000
Copy link
Member Author

ac000 commented Apr 12, 2025

  • Added NJS_NONSTRING to some quickjs arrays
  • Use __attribute__((nonstring)) which matches more of the other attributes without the surrounding __'s
  • Adjust commit subjects

NOTE: This doesn't include the tagging of the start array in the short_string structure in src/njs_value.h as this is removed by the atomics patches.

$ git range-diff 35e6dcc5...d7db1651
1:  deaceec4 ! 1:  0986e285 auto/clang: Add a NJS_NONSTRING macro.
    @@ Metadata
     Author: Andrew Clayton <[email protected]>
     
      ## Commit message ##
    -    auto/clang: Add a NJS_NONSTRING macro.
    +    auto/clang: Added NJS_NONSTRING macro.
     
    -    This is a wrapper around __attribute__((__nonstring__)). Traditionally
    -    this was used to mark char array variables that intentionally lacked a
    +    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 warnings to either be
         quelled or emitted for various memory/string functions.
     
    @@ auto/clang: njs_feature_test="__attribute__((no_sanitize(\"undefined\"))) int ma
     +njs_feature_incs=
     +njs_feature_libs=
     +njs_feature_test="int main(void) {
    -+                      static const char str[3] __attribute__((__nonstring__)) =
    ++                      static const char str[3] __attribute__((nonstring)) =
     +                          \"ABC\";
     +
     +                      return !!str[0];
    @@ src/njs_clang.h: njs_leading_zeros64(uint64_t x)
      
      
     +#if (NJS_HAVE_GCC_ATTRIBUTE_NONSTRING)
    -+#define NJS_NONSTRING      __attribute__((__nonstring__))
    ++#define NJS_NONSTRING      __attribute__((nonstring))
     +
     +#else
     +#define NJS_NONSTRING
2:  35e6dcc5 ! 2:  d7db1651 Tag various character arrays with NJS_NONSTRING.
    @@ Metadata
     Author: Andrew Clayton <[email protected]>
     
      ## Commit message ##
    -    Tag various character arrays with NJS_NONSTRING.
    +    Tagged various character arrays with NJS_NONSTRING.
     
         In njs we have a number of character arrays which are intentionally not
         NUL terminated.
    @@ Commit message
         Closes: https://github.com/nginx/njs/issues/857
         Signed-off-by: Andrew Clayton <[email protected]>
     
    + ## external/qjs_query_string_module.c ##
    +@@ external/qjs_query_string_module.c: qjs_string_encode(const uint32_t *escape, size_t size, const u_char *src,
    +     u_char *dst)
    + {
    +     uint8_t              byte;
    +-    static const u_char  hex[16] = "0123456789ABCDEF";
    ++    static const u_char  hex[16] NJS_NONSTRING = "0123456789ABCDEF";
    + 
    +     do {
    +         byte = *src++;
    +
      ## src/njs_sprintf.c ##
     @@ src/njs_sprintf.c: njs_vsprintf(u_char *buf, u_char *end, const char *fmt, va_list args)
          njs_bool_t     sign;
    @@ src/njs_string.h: njs_string_encode(const uint32_t *escape, size_t size, const u
      
          do {
              byte = *src++;
    +
    + ## src/qjs_buffer.c ##
    +@@ src/qjs_buffer.c: qjs_hex_encode(JSContext *ctx, const njs_str_t *src, njs_str_t *dst)
    +     size_t        i, len;
    +     const u_char  *start;
    + 
    +-    static const u_char  hex[16] = "0123456789abcdef";
    ++    static const u_char  hex[16] NJS_NONSTRING = "0123456789abcdef";
    + 
    +     len = src->length;
    +     start = src->start;

ac000 added 2 commits April 12, 2025 15:31
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 warnings 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.

The above attribute also quells this new warning (which is also enabled
by -Wextra).

So the above example would become

  static const char hex[16] NJS_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>
Signed-off-by: Andrew Clayton <[email protected]>
In njs we have a number of character arrays which are intentionally not
NUL terminated.

With GCC 15 this

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

will trigger a warning (which we treat as an error) like

  src/njs_string.h: In function ‘njs_string_encode’:
  src/njs_string.h:212:36: error: initializer-string for array of ‘unsigned char’ truncates NUL terminator but destination lacks ‘nonstring’ attribute (17 chars into 16 available) [-Werror=unterminated-string-initialization]
    212 |     static const u_char  hex[16] = "0123456789ABCDEF";
        |                                    ^~~~~~~~~~~~~~~~~~

By adding NJS_NONSTRING like

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

we no longer get the warning.

Closes: nginx#857
Signed-off-by: Andrew Clayton <[email protected]>
@ac000
Copy link
Member Author

ac000 commented Apr 12, 2025

  • Set njs_feature_run to no (which is the default) to match the others.
$ git range-diff d7db1651...d252633b
1:  0986e285 ! 1:  19feff97 auto/clang: Added NJS_NONSTRING macro.
    @@ auto/clang: njs_feature_test="__attribute__((no_sanitize(\"undefined\"))) int ma
      
     +njs_feature="GCC __attribute__ nonstring"
     +njs_feature_name=NJS_HAVE_GCC_ATTRIBUTE_NONSTRING
    -+njs_feature_run=
    ++njs_feature_run=no
     +njs_feature_incs=
     +njs_feature_libs=
     +njs_feature_test="int main(void) {
2:  d7db1651 = 2:  d252633b Tagged various character arrays with NJS_NONSTRING.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants