Skip to content

Commit d3de937

Browse files
mhdawsondanielleadams
authored andcommitted
src: fix limit calculation
Coverity reported that the use of sizeof along with pointer arithmetic was likely an error as the pointer arithmetic would already be accounting for the size of what the pointer points to. Looking at the code that looked right but removing the extra sizeOf caused tests to fail. Looking more closely it seems like we were not allocating a big enough buffer but the extra sizeof was allowing us to convert even though it might have been corrupting memory. Signed-off-by: Michael Dawson <[email protected]> PR-URL: #41026 Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 70ed4ef commit d3de937

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/node_i18n.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,9 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
447447

448448
// When flushing the final chunk, the limit is the maximum
449449
// of either the input buffer length or the number of pending
450-
// characters times the min char size.
451-
size_t limit = converter->min_char_size() *
450+
// characters times the min char size, multiplied by 2 as unicode may
451+
// take up to 2 UChars to encode a character
452+
size_t limit = 2 * converter->min_char_size() *
452453
(!flush ?
453454
input.length() :
454455
std::max(
@@ -474,7 +475,7 @@ void ConverterObject::Decode(const FunctionCallbackInfo<Value>& args) {
474475
UChar* target = *result;
475476
ucnv_toUnicode(converter->conv(),
476477
&target,
477-
target + (limit * sizeof(UChar)),
478+
target + limit,
478479
&source,
479480
source + source_length,
480481
nullptr,

0 commit comments

Comments
 (0)