Skip to content

Commit 083ccd2

Browse files
committed
src: reduce allocations in exportPublicKey()
1 parent b8e3a3e commit 083ccd2

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/node_crypto.cc

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5835,7 +5835,7 @@ void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
58355835
}
58365836

58375837

5838-
const char* ExportPublicKey(const char* data, int len) {
5838+
char* ExportPublicKey(const char* data, int len, size_t* size) {
58395839
char* buf = nullptr;
58405840
EVP_PKEY* pkey = nullptr;
58415841
NETSCAPE_SPKI* spki = nullptr;
@@ -5855,12 +5855,12 @@ const char* ExportPublicKey(const char* data, int len) {
58555855
if (PEM_write_bio_PUBKEY(bio, pkey) <= 0)
58565856
goto exit;
58575857

5858-
BIO_write(bio, "\0", 1);
58595858
BUF_MEM* ptr;
58605859
BIO_get_mem_ptr(bio, &ptr);
58615860

5862-
buf = new char[ptr->length];
5863-
memcpy(buf, ptr->data, ptr->length);
5861+
*size = ptr->length;
5862+
buf = Malloc(*size);
5863+
memcpy(buf, ptr->data, *size);
58645864

58655865
exit:
58665866
if (pkey != nullptr)
@@ -5891,14 +5891,12 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
58915891
char* data = Buffer::Data(args[0]);
58925892
CHECK_NE(data, nullptr);
58935893

5894-
const char* pkey = ExportPublicKey(data, length);
5894+
size_t pkey_size;
5895+
char* pkey = ExportPublicKey(data, length, &pkey_size);
58955896
if (pkey == nullptr)
58965897
return args.GetReturnValue().SetEmptyString();
58975898

5898-
Local<Value> out = Encode(env->isolate(), pkey, strlen(pkey), BUFFER);
5899-
5900-
delete[] pkey;
5901-
5899+
Local<Value> out = Buffer::New(env, pkey, pkey_size).ToLocalChecked();
59025900
args.GetReturnValue().Set(out);
59035901
}
59045902

0 commit comments

Comments
 (0)