Skip to content

Commit c30cc4e

Browse files
bnoordhuisindutny
authored andcommitted
src: don't call DecodeWrite() on Buffers
Don't call DecodeWrite() with a Buffer as its argument because it in turn calls StringBytes::Write() and that method expects a Local<String>. "Why then does that function take a Local<Value>?" I hear you ask. Good question but I don't have the answer. I added a CHECK for good measure and what do you know, all of a sudden a large number of crypto tests started failing. Calling DecodeWrite(BINARY) on a buffer is nonsensical anyway: if you want the contents of the buffer, just copy out the data, there is no need to decode it - and that's exactly what this commit does. Fixes a great many instances of the following run-time error in debug builds: FATAL ERROR: v8::String::Cast() Could not convert to string
1 parent e87ceb2 commit c30cc4e

2 files changed

Lines changed: 8 additions & 28 deletions

File tree

src/node_crypto.cc

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -764,20 +764,9 @@ void SecureContext::LoadPKCS12(const FunctionCallbackInfo<Value>& args) {
764764

765765
if (args.Length() >= 2) {
766766
ASSERT_IS_BUFFER(args[1]);
767-
768-
int passlen = Buffer::Length(args[1]);
769-
if (passlen < 0) {
770-
BIO_free_all(in);
771-
return env->ThrowTypeError("Bad password");
772-
}
767+
size_t passlen = Buffer::Length(args[1]);
773768
pass = new char[passlen + 1];
774-
int pass_written = DecodeWrite(env->isolate(),
775-
pass,
776-
passlen,
777-
args[1],
778-
BINARY);
779-
780-
assert(pass_written == passlen);
769+
memcpy(pass, Buffer::Data(args[1]), passlen);
781770
pass[passlen] = '\0';
782771
}
783772

@@ -1167,18 +1156,12 @@ void SSLWrap<Base>::SetSession(const FunctionCallbackInfo<Value>& args) {
11671156
}
11681157

11691158
ASSERT_IS_BUFFER(args[0]);
1170-
ssize_t slen = Buffer::Length(args[0]);
1171-
1172-
if (slen < 0)
1173-
return env->ThrowTypeError("Bad argument");
1174-
1159+
size_t slen = Buffer::Length(args[0]);
11751160
char* sbuf = new char[slen];
1176-
1177-
ssize_t wlen = DecodeWrite(env->isolate(), sbuf, slen, args[0], BINARY);
1178-
assert(wlen == slen);
1161+
memcpy(sbuf, Buffer::Data(args[0]), slen);
11791162

11801163
const unsigned char* p = reinterpret_cast<const unsigned char*>(sbuf);
1181-
SSL_SESSION* sess = d2i_SSL_SESSION(NULL, &p, wlen);
1164+
SSL_SESSION* sess = d2i_SSL_SESSION(NULL, &p, slen);
11821165

11831166
delete[] sbuf;
11841167

@@ -3810,8 +3793,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
38103793
ssize_t passlen = -1;
38113794
ssize_t saltlen = -1;
38123795
ssize_t keylen = -1;
3813-
ssize_t pass_written = -1;
3814-
ssize_t salt_written = -1;
38153796
ssize_t iter = -1;
38163797
PBKDF2Request* req = NULL;
38173798
Local<Object> obj;
@@ -3832,8 +3813,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
38323813
if (pass == NULL) {
38333814
FatalError("node::PBKDF2()", "Out of Memory");
38343815
}
3835-
pass_written = DecodeWrite(env->isolate(), pass, passlen, args[0], BINARY);
3836-
assert(pass_written == passlen);
3816+
memcpy(pass, Buffer::Data(args[0]), passlen);
38373817

38383818
ASSERT_IS_BUFFER(args[1]);
38393819
saltlen = Buffer::Length(args[1]);
@@ -3846,8 +3826,7 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) {
38463826
if (salt == NULL) {
38473827
FatalError("node::PBKDF2()", "Out of Memory");
38483828
}
3849-
salt_written = DecodeWrite(env->isolate(), salt, saltlen, args[1], BINARY);
3850-
assert(salt_written == saltlen);
3829+
memcpy(salt, Buffer::Data(args[1]), saltlen);
38513830

38523831
if (!args[2]->IsNumber()) {
38533832
type_error = "Iterations not a number";

src/string_bytes.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ size_t StringBytes::Write(Isolate* isolate,
301301
size_t len = 0;
302302
bool is_extern = GetExternalParts(isolate, val, &data, &len);
303303

304+
CHECK(val->IsString() == true);
304305
Local<String> str = val.As<String>();
305306
len = len < buflen ? len : buflen;
306307

0 commit comments

Comments
 (0)