Skip to content

Commit 8be9bd1

Browse files
bnoordhuisaddaleax
authored andcommitted
src: remove extra heap allocation in GetSession()
Don't allocate + copy + free; allocate and fill in place, then hand off the pointer to Buffer::New(). Avoids unnecessary heap allocations in the following methods: - crypto.CryptoStream#getSession() - tls.TLSSocket#getSession() PR-URL: #14122 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8dd6866 commit 8be9bd1

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

src/node_crypto.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -1804,11 +1804,10 @@ void SSLWrap<Base>::GetSession(const FunctionCallbackInfo<Value>& args) {
18041804
int slen = i2d_SSL_SESSION(sess, nullptr);
18051805
CHECK_GT(slen, 0);
18061806

1807-
char* sbuf = new char[slen];
1807+
char* sbuf = Malloc(slen);
18081808
unsigned char* p = reinterpret_cast<unsigned char*>(sbuf);
18091809
i2d_SSL_SESSION(sess, &p);
1810-
args.GetReturnValue().Set(Encode(env->isolate(), sbuf, slen, BUFFER));
1811-
delete[] sbuf;
1810+
args.GetReturnValue().Set(Buffer::New(env, sbuf, slen).ToLocalChecked());
18121811
}
18131812

18141813

0 commit comments

Comments
 (0)