From 2c1461541fad111cda931c97e285d2afb302d16a Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Thu, 15 Aug 2024 20:31:10 +0200 Subject: [PATCH] buffer: properly apply dst offset and src length on fast path Refs: https://github.com/nodejs/node/pull/54311#issuecomment-2290453437 PR-URL: https://github.com/nodejs/node/pull/54391 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Yagiz Nizipli Reviewed-By: Jake Yuesong Li --- src/node_buffer.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 0e1881eeb5f16d..2cbcb5461e52d6 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -1479,11 +1479,12 @@ uint32_t FastWriteString(Local receiver, CHECK(offset <= dst.length()); CHECK(dst.length() - offset <= std::numeric_limits::max()); - max_length = std::min(dst.length() - offset, max_length); + const auto size = std::min( + {static_cast(dst.length() - offset), max_length, src.length}); - memcpy(dst_data, src.data, max_length); + memcpy(dst_data + offset, src.data, size); - return max_length; + return size; } static v8::CFunction fast_write_string(v8::CFunction::Make(FastWriteString));