tls: handle errors before calling C++ methods

Calling `this.pair.encrypted._internallyPendingBytes()` before
handling/resetting error will result in assertion failure:

../src/node_crypto.cc:962: void node::crypto::Connection::ClearError():
Assertion `handle_->Get(String::New("error"))->BooleanValue() == false'
failed.

see #5058
This commit is contained in:
Fedor Indutny committed 2013-03-27 16:26:24 +04:00
1 parent 372911ffc7
commit ae86fa84fe
2 files changed
+75 -9

No files matched your search

+7 -9
View File
@@ -335,7 +335,10 @@ CryptoStream.prototype._write = function write(data, encoding, cb) {
written = this.pair.ssl.encIn(data, 0, data.length);
}
var self = this;
// Handle and report errors
if (this.pair.ssl && this.pair.ssl.error) {
return cb(this.pair.error(true));
}
// Force SSL_read call to cycle some states/data inside OpenSSL
this.pair.cleartext.read(0);
@@ -345,11 +348,6 @@ CryptoStream.prototype._write = function write(data, encoding, cb) {
this.pair.encrypted.read(0);
}
// Handle and report errors
if (this.pair.ssl && this.pair.ssl.error) {
return cb(this.pair.error());
}
// Get NPN and Server name when ready
this.pair.maybeInitFinished();
@@ -919,7 +917,7 @@ SecurePair.prototype.destroy = function() {
};
SecurePair.prototype.error = function() {
SecurePair.prototype.error = function(returnOnly) {
var err = this.ssl.error;
this.ssl.error = null;
@@ -929,14 +927,14 @@ SecurePair.prototype.error = function() {
err.code = 'ECONNRESET';
}
this.destroy();
this.emit('error', err);
if (!returnOnly) this.emit('error', err);
} else if (this._isServer &&
this._rejectUnauthorized &&
/peer did not return a certificate/.test(err.message)) {
// Not really an error.
this.destroy();
} else {
this.cleartext.emit('error', err);
if (!returnOnly) this.cleartext.emit('error', err);
}
return err;
};