src: replace manual memory mgmt with std::string · nodejs/node@cdba989

@@ -47,7 +47,6 @@ TLSWrap::TLSWrap(Environment* env,

4747

started_(false),

4848

established_(false),

4949

shutdown_(false),

50-

error_(nullptr),

5150

cycle_depth_(0),

5251

eof_(false) {

5352

node::Wrap(object(), this);

@@ -84,8 +83,6 @@ TLSWrap::~TLSWrap() {

8483

#ifdef SSL_CTRL_SET_TLSEXT_SERVERNAME_CB

8584

sni_context_.Reset();

8685

#endif // SSL_CTRL_SET_TLSEXT_SERVERNAME_CB

87-88-

ClearError();

8986

}

90879188

@@ -348,7 +345,7 @@ void TLSWrap::EncOutCb(WriteWrap* req_wrap, int status) {

348345

}

349346350347351-

Local<Value> TLSWrap::GetSSLError(int status, int* err, const char** msg) {

348+

Local<Value> TLSWrap::GetSSLError(int status, int* err, std::string* msg) {

352349

EscapableHandleScope scope(env()->isolate());

353350354351

// ssl_ is already destroyed in reading EOF by close notify alert.

@@ -379,13 +376,9 @@ Local<Value> TLSWrap::GetSSLError(int status, int* err, const char** msg) {

379376

OneByteString(env()->isolate(), mem->data, mem->length);

380377

Local<Value> exception = Exception::Error(message);

381378382-

if (msg != nullptr) {

383-

CHECK_EQ(*msg, nullptr);

384-

char* const buf = new char[mem->length + 1];

385-

memcpy(buf, mem->data, mem->length);

386-

buf[mem->length] = '\0';

387-

*msg = buf;

388-

}

379+

if (msg != nullptr)

380+

msg->assign(mem->data, mem->data + mem->length);

381+389382

BIO_free_all(bio);

390383391384

return scope.Escape(exception);

@@ -497,12 +490,11 @@ bool TLSWrap::ClearIn() {

497490498491

// Error or partial write

499492

int err;

500-

const char* error_str = nullptr;

493+

std::string error_str;

501494

Local<Value> arg = GetSSLError(written, &err, &error_str);

502495

if (!arg.IsEmpty()) {

503496

MakePending();

504-

InvokeQueued(UV_EPROTO, error_str);

505-

delete[] error_str;

497+

InvokeQueued(UV_EPROTO, error_str.c_str());

506498

clear_in_->Reset();

507499

}

508500

@@ -551,13 +543,12 @@ int TLSWrap::ReadStop() {

551543552544553545

const char* TLSWrap::Error() const {

554-

return error_;

546+

return error_.empty() ? nullptr : error_.c_str();

555547

}

556548557549558550

void TLSWrap::ClearError() {

559-

delete[] error_;

560-

error_ = nullptr;

551+

error_.clear();

561552

}

562553563554

@@ -605,11 +596,7 @@ int TLSWrap::DoWrite(WriteWrap* w,

605596606597

if (ssl_ == nullptr) {

607598

ClearError();

608-609-

static char msg[] = "Write after DestroySSL";

610-

char* tmp = new char[sizeof(msg)];

611-

memcpy(tmp, msg, sizeof(msg));

612-

error_ = tmp;

599+

error_ = "Write after DestroySSL";

613600

return UV_EPROTO;

614601

}

615602