[njs] Fixed build with gcc-15 and -O3.

noreply at nginx.com noreply at nginx.com
Wed Sep 17 18:56:02 UTC 2025


details:   https://github.com/nginx/njs/commit/6b12358be530b158375f97b9b45a90d593da15f9
branches:  master
commit:    6b12358be530b158375f97b9b45a90d593da15f9
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue, 16 Sep 2025 18:28:05 -0700
description:
Fixed build with gcc-15 and -O3.

build/src/njs_object.dep -MT build/src/njs_object.o \ src/njs_object.c
In file included from src/njs_main.h:18, from src/njs_object.c:8: In
function ‘njs_utf8_copy’, inlined from ‘njs_object_enumerate_string’ at
src/njs_object.c:769:21: src/njs_utf8.h:115:20: error: writing 1 byte
into a region of size 0 [-Werror=stringop-overflow=] 115 |
*dst++ = c; |             ~~~~~~~^~~ src/njs_object.c: In function
‘njs_object_enumerate_string’: src/njs_object.c:719:24: note: at offset
4 into destination object ‘buf’ of size 4 719 |     u_char
buf[4], *c; |                        ^~~ In function ‘njs_utf8_copy’,
inlined from ‘njs_object_enumerate_string’ at src/njs_object.c:769:21:
src/njs_utf8.h:115:20: error: writing 1 byte into a region of size 0
[-Werror=stringop-overflow=]

GCC-15 does not know that the loop in njs_utf8_copy() is bounded
because the input is a valid UTF-8 here.

This fixes #967 issue on Github.

---
 src/njs_array.c  | 11 ++++-------
 src/njs_object.c | 17 ++++++-----------
 2 files changed, 10 insertions(+), 18 deletions(-)

diff --git a/src/njs_array.c b/src/njs_array.c
index bcf428e9..b8cb9f94 100644
--- a/src/njs_array.c
+++ b/src/njs_array.c
@@ -788,7 +788,6 @@ static njs_int_t
 njs_array_prototype_slice_copy(njs_vm_t *vm, njs_value_t *this,
     int64_t start, int64_t length, njs_value_t *retval)
 {
-    u_char             *c, buf[4];
     size_t             size;
     uint32_t           n;
     njs_int_t          ret;
@@ -829,17 +828,15 @@ njs_array_prototype_slice_copy(njs_vm_t *vm, njs_value_t *this,
 
             do {
                 value = &array->start[n++];
-                c = buf;
-                c = njs_utf8_copy(c, &src, end);
-                size = c - buf;
+                size = njs_utf8_next(src, end) - src;
 
-                ret = njs_string_new(vm, value, buf, size, 1);
+                ret = njs_string_new(vm, value, src, size, 1);
                 if (njs_slow_path(ret != NJS_OK)) {
                     return NJS_ERROR;
                 }
 
-                length--;
-            } while (length != 0);
+                src += size;
+            } while (src != end);
 
         } else if (njs_is_object(this)) {
 
diff --git a/src/njs_object.c b/src/njs_object.c
index 7f0b1822..26e8182b 100644
--- a/src/njs_object.c
+++ b/src/njs_object.c
@@ -715,7 +715,6 @@ static njs_int_t
 njs_object_enumerate_string(njs_vm_t *vm, const njs_value_t *value,
     njs_array_t *items, uint32_t flags)
 {
-    u_char             buf[4], *c;
     uint32_t           i, len, size;
     njs_int_t          ret;
     njs_value_t        *item, *string;
@@ -763,17 +762,15 @@ njs_object_enumerate_string(njs_vm_t *vm, const njs_value_t *value,
             end = src + str_prop.size;
 
             do {
-                c = buf;
+                size = njs_utf8_next(src, end) - src;
 
-                c = njs_utf8_copy(c, &src, end);
-                size = c - buf;
-
-                ret = njs_string_new(vm, item, buf, size, 1);
+                ret = njs_string_new(vm, item, src, size, 1);
                 if (njs_slow_path(ret != NJS_OK)) {
                     return NJS_ERROR;
                 }
 
                 item++;
+                src += size;
 
             } while (src != end);
         }
@@ -828,12 +825,9 @@ njs_object_enumerate_string(njs_vm_t *vm, const njs_value_t *value,
 
                 string = &entry->start[1];
 
-                c = buf;
-
-                c = njs_utf8_copy(c, &src, end);
-                size = c - buf;
+                size = njs_utf8_next(src, end) - src;
 
-                ret = njs_string_new(vm, string, buf, size, 1);
+                ret = njs_string_new(vm, string, src, size, 1);
                 if (njs_slow_path(ret != NJS_OK)) {
                     return NJS_ERROR;
                 }
@@ -841,6 +835,7 @@ njs_object_enumerate_string(njs_vm_t *vm, const njs_value_t *value,
                 njs_set_array(item, entry);
 
                 item++;
+                src += size;
 
             } while (src != end);
         }


More information about the nginx-devel mailing list