Skip to content

Commit 9990dc7

Browse files
kvakilruyadorno
authored andcommitted
src,buffer: remove unused chars_written parameter
This parameter was always being set to `nullptr` by its callers, either explicitly or implicitly via the default argument. It was also buggy, as in some cases it wouldn't be written to, potentially leaking stack memory (see the early returns in `StringBytes::WriteUCS2`). Remove it entirely. PR-URL: #44092 Reviewed-By: Feng Yu <F3n67u@outlook.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent c3d8756 commit 9990dc7

File tree

4 files changed

+15
-42
lines changed

4 files changed

+15
-42
lines changed

src/api/encoding.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ ssize_t DecodeWrite(Isolate* isolate,
150150
size_t buflen,
151151
Local<Value> val,
152152
enum encoding encoding) {
153-
return StringBytes::Write(isolate, buf, buflen, val, encoding, nullptr);
153+
return StringBytes::Write(isolate, buf, buflen, val, encoding);
154154
}
155155

156156
} // namespace node

src/node_buffer.cc

+4-12
Original file line numberDiff line numberDiff line change
@@ -666,12 +666,8 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
666666
// Write initial String to Buffer, then use that memory to copy remainder
667667
// of string. Correct the string length for cases like HEX where less than
668668
// the total string length is written.
669-
str_length = StringBytes::Write(env->isolate(),
670-
ts_obj_data + start,
671-
fill_length,
672-
str_obj,
673-
enc,
674-
nullptr);
669+
str_length = StringBytes::Write(
670+
env->isolate(), ts_obj_data + start, fill_length, str_obj, enc);
675671
}
676672

677673
start_fill:
@@ -730,12 +726,8 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
730726
if (max_length == 0)
731727
return args.GetReturnValue().Set(0);
732728

733-
uint32_t written = StringBytes::Write(env->isolate(),
734-
ts_obj_data + offset,
735-
max_length,
736-
str,
737-
encoding,
738-
nullptr);
729+
uint32_t written = StringBytes::Write(
730+
env->isolate(), ts_obj_data + offset, max_length, str, encoding);
739731
args.GetReturnValue().Set(written);
740732
}
741733

src/string_bytes.cc

+8-25
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,8 @@ static size_t hex_decode(char* buf,
260260
return i;
261261
}
262262

263-
size_t StringBytes::WriteUCS2(Isolate* isolate,
264-
char* buf,
265-
size_t buflen,
266-
Local<String> str,
267-
int flags,
268-
size_t* chars_written) {
263+
size_t StringBytes::WriteUCS2(
264+
Isolate* isolate, char* buf, size_t buflen, Local<String> str, int flags) {
269265
uint16_t* const dst = reinterpret_cast<uint16_t*>(buf);
270266

271267
size_t max_chars = buflen / sizeof(*dst);
@@ -277,15 +273,16 @@ size_t StringBytes::WriteUCS2(Isolate* isolate,
277273
size_t nchars;
278274
if (aligned_dst == dst) {
279275
nchars = str->Write(isolate, dst, 0, max_chars, flags);
280-
*chars_written = nchars;
281276
return nchars * sizeof(*dst);
282277
}
283278

284279
CHECK_EQ(reinterpret_cast<uintptr_t>(aligned_dst) % sizeof(*dst), 0);
285280

286281
// Write all but the last char
287282
max_chars = std::min(max_chars, static_cast<size_t>(str->Length()));
288-
if (max_chars == 0) return 0;
283+
if (max_chars == 0) {
284+
return 0;
285+
}
289286
nchars = str->Write(isolate, aligned_dst, 0, max_chars - 1, flags);
290287
CHECK_EQ(nchars, max_chars - 1);
291288

@@ -298,23 +295,16 @@ size_t StringBytes::WriteUCS2(Isolate* isolate,
298295
memcpy(buf + nchars * sizeof(*dst), &last, sizeof(last));
299296
nchars++;
300297

301-
*chars_written = nchars;
302298
return nchars * sizeof(*dst);
303299
}
304300

305-
306301
size_t StringBytes::Write(Isolate* isolate,
307302
char* buf,
308303
size_t buflen,
309304
Local<Value> val,
310-
enum encoding encoding,
311-
int* chars_written) {
305+
enum encoding encoding) {
312306
HandleScope scope(isolate);
313307
size_t nbytes;
314-
int nchars;
315-
316-
if (chars_written == nullptr)
317-
chars_written = &nchars;
318308

319309
CHECK(val->IsString() == true);
320310
Local<String> str = val.As<String>();
@@ -334,19 +324,15 @@ size_t StringBytes::Write(Isolate* isolate,
334324
uint8_t* const dst = reinterpret_cast<uint8_t*>(buf);
335325
nbytes = str->WriteOneByte(isolate, dst, 0, buflen, flags);
336326
}
337-
*chars_written = nbytes;
338327
break;
339328

340329
case BUFFER:
341330
case UTF8:
342-
nbytes = str->WriteUtf8(isolate, buf, buflen, chars_written, flags);
331+
nbytes = str->WriteUtf8(isolate, buf, buflen, nullptr, flags);
343332
break;
344333

345334
case UCS2: {
346-
size_t nchars;
347-
348-
nbytes = WriteUCS2(isolate, buf, buflen, str, flags, &nchars);
349-
*chars_written = static_cast<int>(nchars);
335+
nbytes = WriteUCS2(isolate, buf, buflen, str, flags);
350336

351337
// Node's "ucs2" encoding wants LE character data stored in
352338
// the Buffer, so we need to reorder on BE platforms. See
@@ -368,7 +354,6 @@ size_t StringBytes::Write(Isolate* isolate,
368354
String::Value value(isolate, str);
369355
nbytes = base64_decode(buf, buflen, *value, value.length());
370356
}
371-
*chars_written = nbytes;
372357
break;
373358

374359
case HEX:
@@ -379,7 +364,6 @@ size_t StringBytes::Write(Isolate* isolate,
379364
String::Value value(isolate, str);
380365
nbytes = hex_decode(buf, buflen, *value, value.length());
381366
}
382-
*chars_written = nbytes;
383367
break;
384368

385369
default:
@@ -390,7 +374,6 @@ size_t StringBytes::Write(Isolate* isolate,
390374
return nbytes;
391375
}
392376

393-
394377
// Quick and dirty size calculation
395378
// Will always be at least big enough, but may have some extra
396379
// UTF8 can be as much as 3x the size, Base64 can have 1-2 extra bytes

src/string_bytes.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ class StringBytes {
7575
char* buf,
7676
size_t buflen,
7777
v8::Local<v8::Value> val,
78-
enum encoding enc,
79-
int* chars_written = nullptr);
78+
enum encoding enc);
8079

8180
// Take the bytes in the src, and turn it into a Buffer or String.
8281
static v8::MaybeLocal<v8::Value> Encode(v8::Isolate* isolate,
@@ -111,8 +110,7 @@ class StringBytes {
111110
char* buf,
112111
size_t buflen,
113112
v8::Local<v8::String> str,
114-
int flags,
115-
size_t* chars_written);
113+
int flags);
116114
};
117115

118116
} // namespace node

0 commit comments

Comments
 (0)