diff --git a/src/node_crypto.cc b/src/node_crypto.cc
index de301d77c229c1..5634d8b1dc5379 100644
--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
@@ -2317,11 +2317,12 @@ void SSLWrap::GetSession(const FunctionCallbackInfo& args) {
return;
int slen = i2d_SSL_SESSION(sess, nullptr);
- CHECK_GT(slen, 0);
+ if (slen <= 0)
+ return; // Invalid or malformed session.
AllocatedBuffer sbuf = env->AllocateManaged(slen);
unsigned char* p = reinterpret_cast(sbuf.data());
- i2d_SSL_SESSION(sess, &p);
+ CHECK_LT(0, i2d_SSL_SESSION(sess, &p));
args.GetReturnValue().Set(sbuf.ToBuffer().ToLocalChecked());
}