Skip to content

Commit e4c461b

Browse files
bnoordhuisMylesBorins
authored andcommitted
src: replace manual memory mgmt with std::string
PR-URL: #15782 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8f367bb commit e4c461b

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

src/tls_wrap.cc

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ TLSWrap::TLSWrap(Environment* env,
6666
started_(false),
6767
established_(false),
6868
shutdown_(false),
69-
error_(nullptr),
7069
cycle_depth_(0),
7170
eof_(false) {
7271
node::Wrap(object(), this);
@@ -103,8 +102,6 @@ TLSWrap::~TLSWrap() {
103102
#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
104103
sni_context_.Reset();
105104
#endif // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB
106-
107-
ClearError();
108105
}
109106

110107

@@ -367,7 +364,7 @@ void TLSWrap::EncOutCb(WriteWrap* req_wrap, int status) {
367364
}
368365

369366

370-
Local<Value> TLSWrap::GetSSLError(int status, int* err, const char** msg) {
367+
Local<Value> TLSWrap::GetSSLError(int status, int* err, std::string* msg) {
371368
EscapableHandleScope scope(env()->isolate());
372369

373370
// ssl_ is already destroyed in reading EOF by close notify alert.
@@ -398,13 +395,9 @@ Local<Value> TLSWrap::GetSSLError(int status, int* err, const char** msg) {
398395
OneByteString(env()->isolate(), mem->data, mem->length);
399396
Local<Value> exception = Exception::Error(message);
400397

401-
if (msg != nullptr) {
402-
CHECK_EQ(*msg, nullptr);
403-
char* const buf = new char[mem->length + 1];
404-
memcpy(buf, mem->data, mem->length);
405-
buf[mem->length] = '\0';
406-
*msg = buf;
407-
}
398+
if (msg != nullptr)
399+
msg->assign(mem->data, mem->data + mem->length);
400+
408401
BIO_free_all(bio);
409402

410403
return scope.Escape(exception);
@@ -516,12 +509,11 @@ bool TLSWrap::ClearIn() {
516509

517510
// Error or partial write
518511
int err;
519-
const char* error_str = nullptr;
512+
std::string error_str;
520513
Local<Value> arg = GetSSLError(written, &err, &error_str);
521514
if (!arg.IsEmpty()) {
522515
MakePending();
523-
InvokeQueued(UV_EPROTO, error_str);
524-
delete[] error_str;
516+
InvokeQueued(UV_EPROTO, error_str.c_str());
525517
clear_in_->Reset();
526518
}
527519

@@ -570,13 +562,12 @@ int TLSWrap::ReadStop() {
570562

571563

572564
const char* TLSWrap::Error() const {
573-
return error_;
565+
return error_.empty() ? nullptr : error_.c_str();
574566
}
575567

576568

577569
void TLSWrap::ClearError() {
578-
delete[] error_;
579-
error_ = nullptr;
570+
error_.clear();
580571
}
581572

582573

@@ -624,11 +615,7 @@ int TLSWrap::DoWrite(WriteWrap* w,
624615

625616
if (ssl_ == nullptr) {
626617
ClearError();
627-
628-
static char msg[] = "Write after DestroySSL";
629-
char* tmp = new char[sizeof(msg)];
630-
memcpy(tmp, msg, sizeof(msg));
631-
error_ = tmp;
618+
error_ = "Write after DestroySSL";
632619
return UV_EPROTO;
633620
}
634621

src/tls_wrap.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
#include <openssl/ssl.h>
3737

38+
#include <string>
39+
3840
namespace node {
3941

4042
// Forward-declarations
@@ -148,8 +150,7 @@ class TLSWrap : public AsyncWrap,
148150

149151
void DoRead(ssize_t nread, const uv_buf_t* buf, uv_handle_type pending);
150152

151-
// If |msg| is not nullptr, caller is responsible for calling `delete[] *msg`.
152-
v8::Local<v8::Value> GetSSLError(int status, int* err, const char** msg);
153+
v8::Local<v8::Value> GetSSLError(int status, int* err, std::string* msg);
153154

154155
static void OnClientHelloParseEnd(void* arg);
155156
static void Wrap(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -180,7 +181,7 @@ class TLSWrap : public AsyncWrap,
180181
bool started_;
181182
bool established_;
182183
bool shutdown_;
183-
const char* error_;
184+
std::string error_;
184185
int cycle_depth_;
185186

186187
// If true - delivered EOF to the js-land, either after `close_notify`, or

0 commit comments

Comments
 (0)