From be011858446f7ec4b914fd1af2cb7555bd04877b Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sat, 9 Apr 2022 11:39:03 +0530 Subject: [PATCH] src,inspector: fix empty MaybeLocal crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Return early when the Inspector StringView to V8 String conversion fails and returns an empty MaybeLocal instead of running the invalid ToLocalChecked() assertion. Fixes: https://github.com/nodejs/node/issues/42407 Signed-off-by: Darshan Sen PR-URL: https://github.com/nodejs/node/pull/42409 Reviewed-By: Richard Lau Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Anna Henningsen Reviewed-By: Tobias Nießen Reviewed-By: Juan José Arboleda Reviewed-By: James M Snell --- src/inspector_js_api.cc | 8 ++++---- .../test-repl-empty-maybelocal-crash.js | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 test/pummel/test-repl-empty-maybelocal-crash.js diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index e605c1c4d6f087..4ebb8acd689d58 100644 --- a/src/inspector_js_api.cc +++ b/src/inspector_js_api.cc @@ -75,10 +75,10 @@ class JSBindingsConnection : public AsyncWrap { Isolate* isolate = env_->isolate(); HandleScope handle_scope(isolate); Context::Scope context_scope(env_->context()); - MaybeLocal v8string = - String::NewFromTwoByte(isolate, message.characters16(), - NewStringType::kNormal, message.length()); - Local argument = v8string.ToLocalChecked().As(); + Local argument; + if (!String::NewFromTwoByte(isolate, message.characters16(), + NewStringType::kNormal, + message.length()).ToLocal(&argument)) return; connection_->OnMessage(argument); } diff --git a/test/pummel/test-repl-empty-maybelocal-crash.js b/test/pummel/test-repl-empty-maybelocal-crash.js new file mode 100644 index 00000000000000..84686e308c6157 --- /dev/null +++ b/test/pummel/test-repl-empty-maybelocal-crash.js @@ -0,0 +1,20 @@ +'use strict'; +const common = require('../common'); + +if (process.config.variables.arm_version === '7') { + common.skip('Too slow for armv7 bots'); +} + +// The process should not crash when the REPL receives the string, 'ss'. +// Test for https://github.com/nodejs/node/issues/42407. + +const repl = require('repl'); + +const r = repl.start(); + +r.write('var buf = Buffer.from({length:200e6},(_,i) => i%256);\n'); +r.write('var ss = buf.toString("binary");\n'); +r.write('ss'); +r.write('.'); + +r.close();