[njs] Fixed Array.prototype.sort() with --debug=YES and --debug-memory=YES.

2023-10-06 Thread Dmitry Volyntsev
details:   https://hg.nginx.org/njs/rev/b49a98886c02
branches:  
changeset: 2219:b49a98886c02
user:  Dmitry Volyntsev 
date:  Fri Oct 06 16:52:23 2023 -0700
description:
Fixed Array.prototype.sort() with --debug=YES and --debug-memory=YES.

Previously, --debug-memory=YES activated a different allocation
mechanism that was not able to properly handle the 0 size allocation.
Specifically, njs_mp_free() failed to find a block to free when the size
of the block is 0.

The fix is to alloc at least 1 byte in the --debug-memory=YES mode.

diffstat:

 src/njs_array.c |  28 +++-
 src/njs_mp.c|   8 
 2 files changed, 31 insertions(+), 5 deletions(-)

diffs (77 lines):

diff -r b67fc7398a83 -r b49a98886c02 src/njs_array.c
--- a/src/njs_array.c   Fri Oct 06 16:51:53 2023 -0700
+++ b/src/njs_array.c   Fri Oct 06 16:52:23 2023 -0700
@@ -2782,6 +2782,8 @@ njs_sort_indexed_properties(njs_vm_t *vm
 njs_array_sort_ctx_t   ctx;
 njs_array_sort_slot_t  *p, *end, *slots, *newslots;
 
+njs_assert(length != 0);
+
 slots = NULL;
 keys = NULL;
 ctx.vm = vm;
@@ -2993,6 +2995,12 @@ njs_array_prototype_sort(njs_vm_t *vm, n
 return ret;
 }
 
+slots = NULL;
+
+if (length == 0) {
+goto done;
+}
+
 /* Satisfy gcc -O3 */
 nslots = 0;
 
@@ -3027,6 +3035,8 @@ njs_array_prototype_sort(njs_vm_t *vm, n
 }
 }
 
+done:
+
 njs_value_assign(retval, this);
 
 ret = NJS_OK;
@@ -3083,11 +3093,19 @@ njs_array_prototype_to_sorted(njs_vm_t *
 return NJS_ERROR;
 }
 
-slots = njs_sort_indexed_properties(vm, this, length, compare, 0, ,
-);
-if (njs_slow_path(slots == NULL)) {
-ret = NJS_ERROR;
-goto exception;
+if (length != 0) {
+slots = njs_sort_indexed_properties(vm, this, length, compare, 0,
+, );
+if (njs_slow_path(slots == NULL)) {
+ret = NJS_ERROR;
+goto exception;
+}
+
+} else {
+slots = NULL;
+length = 0;
+nslots = 0;
+nunds = 0;
 }
 
 njs_assert(length == (nslots + nunds));
diff -r b67fc7398a83 -r b49a98886c02 src/njs_mp.c
--- a/src/njs_mp.c  Fri Oct 06 16:51:53 2023 -0700
+++ b/src/njs_mp.c  Fri Oct 06 16:52:23 2023 -0700
@@ -592,6 +592,14 @@ njs_mp_alloc_large(njs_mp_t *mp, size_t 
 return NULL;
 }
 
+#if (NJS_DEBUG)
+/*
+ * Ensure that the size is not zero, otherwise njs_mp_find_block()
+ * will not be able to find the block.
+ */
+size += size == 0;
+#endif
+
 if (njs_is_power_of_two(size)) {
 block = njs_malloc(sizeof(njs_mp_block_t));
 if (njs_slow_path(block == NULL)) {
___
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel


[njs] Fixed RegExp.prototype.replace().

2023-10-06 Thread Dmitry Volyntsev
details:   https://hg.nginx.org/njs/rev/b67fc7398a83
branches:  
changeset: 2218:b67fc7398a83
user:  Dmitry Volyntsev 
date:  Fri Oct 06 16:51:53 2023 -0700
description:
Fixed RegExp.prototype.replace().

The issue was introduced in cf85d0f8640a.

diffstat:

 src/njs_regexp.c |  13 +
 1 files changed, 9 insertions(+), 4 deletions(-)

diffs (30 lines):

diff -r c16f64d334f2 -r b67fc7398a83 src/njs_regexp.c
--- a/src/njs_regexp.c  Fri Oct 06 16:49:59 2023 -0700
+++ b/src/njs_regexp.c  Fri Oct 06 16:51:53 2023 -0700
@@ -1541,10 +1541,6 @@ njs_regexp_prototype_symbol_replace(njs_
   arguments, ncaptures, ,
   replace, retval);
 
-if (njs_object_slots(r)) {
-njs_regexp_exec_result_free(vm, njs_array(r));
-}
-
 } else {
 ret = njs_array_expand(vm, array, 0,
njs_is_defined() ? 3 : 2);
@@ -1586,6 +1582,15 @@ njs_regexp_prototype_symbol_replace(njs_
 
 next_pos = pos + (int64_t) m.length;
 }
+
+if (!func_replace && njs_object_slots(r)) {
+/*
+  * Doing free here ONLY for non-function replace, because
+  * otherwise we cannot be certain the result of match
+  * was not stored elsewhere.
+  */
+njs_regexp_exec_result_free(vm, njs_array(r));
+}
 }
 
 if (next_pos < (int64_t) s.size) {
___
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel


[njs] Fixed RegExp.prototype.split().

2023-10-06 Thread Dmitry Volyntsev
details:   https://hg.nginx.org/njs/rev/c16f64d334f2
branches:  
changeset: 2217:c16f64d334f2
user:  Dmitry Volyntsev 
date:  Fri Oct 06 16:49:59 2023 -0700
description:
Fixed RegExp.prototype.split().

The issue was introduced in c0aad58cfadb.

diffstat:

 src/njs_regexp.c |  15 +--
 src/test/njs_unit_test.c |   3 +++
 2 files changed, 12 insertions(+), 6 deletions(-)

diffs (48 lines):

diff -r 632002c161b1 -r c16f64d334f2 src/njs_regexp.c
--- a/src/njs_regexp.c  Wed Oct 04 15:00:02 2023 -0700
+++ b/src/njs_regexp.c  Fri Oct 06 16:49:59 2023 -0700
@@ -1625,7 +1625,7 @@ njs_regexp_prototype_symbol_split(njs_vm
 njs_value_tr, z, this, s_lvalue, setval, constructor;
 njs_object_t   *object;
 const u_char   *start, *end;
-njs_string_prop_t  s;
+njs_string_prop_t  s, sv;
 njs_value_targuments[2];
 
 static const njs_value_t  string_lindex = njs_string("lastIndex");
@@ -1815,14 +1815,17 @@ njs_regexp_prototype_symbol_split(njs_vm
 ncaptures = njs_max(ncaptures - 1, 0);
 
 for (i = 1; i <= ncaptures; i++) {
-value = njs_array_push(vm, array);
-if (njs_slow_path(value == NULL)) {
+ret = njs_value_property_i64(vm, , i, retval);
+if (njs_slow_path(ret == NJS_ERROR)) {
 return NJS_ERROR;
 }
 
-ret = njs_value_property_i64(vm, , i, value);
-if (njs_slow_path(ret == NJS_ERROR)) {
-return NJS_ERROR;
+(void) njs_string_prop(, retval);
+
+ret = njs_array_string_add(vm, array, sv.start, sv.size,
+   sv.length);
+if (njs_slow_path(ret != NJS_OK)) {
+return ret;
 }
 
 if (array->length == limit) {
diff -r 632002c161b1 -r c16f64d334f2 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c  Wed Oct 04 15:00:02 2023 -0700
+++ b/src/test/njs_unit_test.c  Fri Oct 06 16:49:59 2023 -0700
@@ -9734,6 +9734,9 @@ static njs_unit_test_t  njs_test[] =
 { njs_str("'мояВерблюжьяСтрока'.split(/(?=[А-Я])/)"),
   njs_str("моя,Верблюжья,Строка") },
 
+{ njs_str("`a`.split(/(.*)/)"),
+  njs_str(",a,") },
+
 { njs_str("'Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand 
'.split( /\\s*(?:;|$)\\s*/)"),
   njs_str("Harry Trump,Fred Barney,Helen Rigby,Bill Abel,Chris Hand,") },
 
___
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel


optimize the code for searching file in ngx_conf_open_file

2023-10-06 Thread Davood Falahati
# HG changeset patch
# User Davood Falahati <0x0dav...@gmail.com>
# Date 1696647746 -7200
#  Sat Oct 07 05:02:26 2023 +0200
# Node ID ab14ea51bbb15331c9f44f14901d0fd378168647
# Parent  c073e545e1cdcc736f8869a012a78b2dd836eac9
optimize the code for searching file in ngx_conf_open_file

This patch combines two consecutive if statements into one and leveraging
short circuits circuiting. In the current code, we check the lengths of
file names and if they match, the file names are being compared.

I see few other places in the code that writing multiple if statements are
preferred over short circuit evaluation (e.g.
http://hg.nginx.org/nginx/file/tip/src/http/ngx_http_file_cache.c#l1153). I
wanted to make sure if it's a matter of community's taste or if it's in
line with some performance considerations?

diff -r c073e545e1cd -r ab14ea51bbb1 src/core/ngx_conf_file.c
--- a/src/core/ngx_conf_file.c Thu May 18 23:42:22 2023 +0200
+++ b/src/core/ngx_conf_file.c Sat Oct 07 05:02:26 2023 +0200
@@ -927,11 +927,7 @@
 i = 0;
 }

-if (full.len != file[i].name.len) {
-continue;
-}
-
-if (ngx_strcmp(full.data, file[i].name.data) == 0) {
+if (full.len == file[i].name.len && ngx_strcmp(full.data,
file[i].name.data) == 0) {
 return [i];
 }
 }
___
nginx-devel mailing list
nginx-devel@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx-devel


Re: logrotate (?) screws it badly

2023-10-06 Thread Ralph Seichter via nginx
* lejeczek via nginx:

> For after logs got rotated Nginx logs into:
> access.log.1 & error.log.1
> and now as it should, you know
> access.log & error.log

You may want to try logrotate's "copytruncate" option.

-Ralph
___
nginx mailing list
nginx@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx


RE: logrotate (?) screws it badly

2023-10-06 Thread Reinis Rozitis
>postrotate
>   /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
 >  endscript
>}
```
> and I wonder...
> if it is logrotate's _fault_ or perhaps I screwed Nginx's configs somewhere? 
> For after logs got rotated Nginx logs into: access.log.1 & error.log.1 and 
> now as it should, you 
> know access.log & error.log


You need to check if the postrotate command completes successfully (for example 
- is the pid file in correct path? / I would remove all the || true parts and 
just leave kill -USR1 `cat /run/nginx.pid`).

Now it seems that logrotate just renames the files but nginx is not getting the 
signal and still has open handle to them so keeps logging in the renamed file.

rr
___
nginx mailing list
nginx@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx


logrotate (?) screws it badly

2023-10-06 Thread lejeczek via nginx

Hi guys.

I run off distro's vanilla-default logrotate, like so:
```
/var/log/nginx/*log {
    daily
    rotate 10
    missingok
    notifempty
    compress
    delaycompress
    sharedscripts
    postrotate
    /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 
2>/dev/null || true

    endscript
}
```

and I wonder...
if it is logrotate's _fault_ or perhaps I screwed Nginx's 
configs somewhere? For after logs got rotated Nginx logs into:

access.log.1 & error.log.1
and now as it should, you know
access.log & error.log

many thanks, L.___
nginx mailing list
nginx@nginx.org
https://mailman.nginx.org/mailman/listinfo/nginx