@@ -60,17 +60,19 @@ struct ReaderListenerProxyData {
6060void ReaderListenerProxy (Napi::Env env, Napi::Function jsCallback, ReaderListenerProxyData *data) {
6161 Napi::Object msg = Message::NewInstance ({}, data->cMessage );
6262 Reader *reader = data->reader ;
63-
64- Napi::Value ret = jsCallback.Call ({msg, reader->Value ()});
65- if (ret.IsPromise ()) {
66- Napi::Promise promise = ret.As <Napi::Promise>();
67- Napi::Value thenValue = promise.Get (" then" );
68- if (thenValue.IsFunction ()) {
69- Napi::Function then = thenValue.As <Napi::Function>();
70- Napi::Function callback =
71- Napi::Function::New (env, [data](const Napi::CallbackInfo &info) { data->callback (); });
72- then.Call (promise, {callback});
73- return ;
63+ // `reader` might be null in certain cases, segmentation fault might happend without this null check.
64+ if (reader) {
65+ Napi::Value ret = jsCallback.Call ({msg, reader->Value ()});
66+ if (ret.IsPromise ()) {
67+ Napi::Promise promise = ret.As <Napi::Promise>();
68+ Napi::Value thenValue = promise.Get (" then" );
69+ if (thenValue.IsFunction ()) {
70+ Napi::Function then = thenValue.As <Napi::Function>();
71+ Napi::Function callback =
72+ Napi::Function::New (env, [data](const Napi::CallbackInfo &info) { data->callback (); });
73+ then.Call (promise, {callback});
74+ return ;
75+ }
7476 }
7577 }
7678 data->callback ();
0 commit comments