[njs] Fixed Fixed Buffer.prototype.write().

noreply at nginx.com noreply at nginx.com
Fri Aug 16 01:36:02 UTC 2024


details:   https://github.com/nginx/njs/commit/5cf4fd025664ea7b100757865ca9c59e44830cba
branches:  master
commit:    5cf4fd025664ea7b100757865ca9c59e44830cba
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed, 7 Aug 2024 22:48:04 -0700
description:
Fixed Fixed Buffer.prototype.write().


---
 src/njs_buffer.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/njs_buffer.c b/src/njs_buffer.c
index fa087a19..1ce98ef2 100644
--- a/src/njs_buffer.c
+++ b/src/njs_buffer.c
@@ -1577,7 +1577,7 @@ encoding:
         return NJS_ERROR;
     }
 
-    if (offset >= array->byte_length) {
+    if (offset > array->byte_length) {
         njs_range_error(vm, "\"offset\" is out of range");
         return NJS_ERROR;
     }
@@ -1596,6 +1596,7 @@ njs_buffer_write_string(njs_vm_t *vm, njs_value_t *value,
     njs_int_t           ret;
     njs_str_t           str;
     njs_value_t         dst;
+    const u_char        *p, *end, *prev;
     njs_array_buffer_t  *buffer;
 
     buffer = njs_typed_array_buffer(array);
@@ -1618,6 +1619,23 @@ njs_buffer_write_string(njs_vm_t *vm, njs_value_t *value,
         goto done;
     }
 
+    length = njs_min(str.length, (size_t) length);
+
+    if (encoding->decode == njs_string_decode_utf8) {
+        /* Avoid writing incomplete UTF-8 characters. */
+        p = prev = str.start;
+        end = p + length;
+
+        while (p < end) {
+            p = njs_utf8_next(p, str.start + str.length);
+            if (p <= end) {
+                prev = p;
+            }
+        }
+
+        length = prev - str.start;
+    }
+
     memcpy(start, str.start, length);
 
 done:


More information about the nginx-devel mailing list