@@ -53,9 +53,6 @@ using v8::Object;
5353using v8::String;
5454using v8::Value;
5555
56- size_t TLSCallbacks::error_off_;
57- char TLSCallbacks::error_buf_[1024 ];
58-
5956
6057TLSCallbacks::TLSCallbacks (Environment* env,
6158 Kind kind,
@@ -118,6 +115,8 @@ TLSCallbacks::~TLSCallbacks() {
118115 WriteItem* wi = ContainerOf (&WriteItem::member_, q);
119116 delete wi;
120117 }
118+
119+ ClearError ();
121120}
122121
123122
@@ -378,32 +377,6 @@ void TLSCallbacks::EncOutCb(uv_write_t* req, int status) {
378377}
379378
380379
381- int TLSCallbacks::PrintErrorsCb (const char * str, size_t len, void * arg) {
382- size_t to_copy = error_off_;
383- size_t avail = sizeof (error_buf_) - error_off_ - 1 ;
384-
385- if (avail > to_copy)
386- to_copy = avail;
387-
388- memcpy (error_buf_, str, avail);
389- error_off_ += avail;
390- CHECK_LT (error_off_, sizeof (error_buf_));
391-
392- // Zero-terminate
393- error_buf_[error_off_] = ' \0 ' ;
394-
395- return 0 ;
396- }
397-
398-
399- const char * TLSCallbacks::PrintErrors () {
400- error_off_ = 0 ;
401- ERR_print_errors_cb (PrintErrorsCb, this );
402-
403- return error_buf_;
404- }
405-
406-
407380Local<Value> TLSCallbacks::GetSSLError (int status, int * err, const char ** msg) {
408381 EscapableHandleScope scope (env ()->isolate ());
409382
@@ -420,16 +393,24 @@ Local<Value> TLSCallbacks::GetSSLError(int status, int* err, const char** msg) {
420393 {
421394 CHECK (*err == SSL_ERROR_SSL || *err == SSL_ERROR_SYSCALL);
422395
423- const char * buf = PrintErrors ();
396+ BIO* bio = BIO_new (BIO_s_mem ());
397+ ERR_print_errors (bio);
398+
399+ BUF_MEM* mem;
400+ BIO_get_mem_ptr (bio, &mem);
424401
425402 Local<String> message =
426- OneByteString (env ()->isolate (), buf, strlen (buf) );
403+ OneByteString (env ()->isolate (), mem-> data , mem-> length );
427404 Local<Value> exception = Exception::Error (message);
428405
429406 if (msg != nullptr ) {
430407 CHECK_EQ (*msg, nullptr );
408+ char * const buf = new char [mem->length + 1 ];
409+ memcpy (buf, mem->data , mem->length );
410+ buf[mem->length ] = ' \0 ' ;
431411 *msg = buf;
432412 }
413+ static_cast <void >(BIO_reset (bio));
433414
434415 return scope.Escape (exception);
435416 }
@@ -523,18 +504,22 @@ bool TLSCallbacks::ClearIn() {
523504 if (!arg.IsEmpty ()) {
524505 MakePending ();
525506 if (!InvokeQueued (UV_EPROTO))
526- error_ = nullptr ;
507+ ClearError () ;
527508 clear_in_->Reset ();
528509 }
529510
530511 return false ;
531512}
532513
533514
534- const char * TLSCallbacks::Error () {
535- const char * ret = error_;
515+ const char * TLSCallbacks::Error () const {
516+ return error_;
517+ }
518+
519+
520+ void TLSCallbacks::ClearError () {
521+ delete[] error_;
536522 error_ = nullptr ;
537- return ret;
538523}
539524
540525
0 commit comments