From 135f144cf07e2769ba4f96f3ad8c00e4f9112144 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Sun, 20 Mar 2022 15:03:25 +0530 Subject: [PATCH] src,inspector: fix empty MaybeLocal crash 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 --- src/inspector_js_api.cc | 8 ++++---- .../parallel/test-repl-empty-maybelocal-crash.js | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-repl-empty-maybelocal-crash.js diff --git a/src/inspector_js_api.cc b/src/inspector_js_api.cc index 8de1f8e7b0a88d..30a2e31361f47d 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/parallel/test-repl-empty-maybelocal-crash.js b/test/parallel/test-repl-empty-maybelocal-crash.js new file mode 100644 index 00000000000000..3e2f3f42d98f11 --- /dev/null +++ b/test/parallel/test-repl-empty-maybelocal-crash.js @@ -0,0 +1,16 @@ +'use strict'; +require('../common'); + +// 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();