tls: fix assertion when ssl is destroyed at read
authorFedor Indutny <fedor.indutny@gmail.com>
Wed, 21 Aug 2013 11:58:33 +0000 (15:58 +0400)
committerFedor Indutny <fedor.indutny@gmail.com>
Wed, 21 Aug 2013 12:15:08 +0000 (16:15 +0400)
`maybeInitFinished()` can emit the 'secure' event which
in turn destroys the connection in case of authentication
failure and sets `this.pair.ssl` to `null`.

If such condition appeared after non-empty read - loop will continue
and `clearOut` will be called on `null` object instead of
`crypto::Connection` instance. Resulting in the following assertion:

    ERROR: Error: Hostname/IP doesn't match certificate's altnames
    Assertion failed: handle->InternalFieldCount() > 0

fix #5756

lib/tls.js

index 0907b29..ea3d2e4 100644 (file)
@@ -461,7 +461,14 @@ CryptoStream.prototype._read = function read(size) {
 
     // Get NPN and Server name when ready
     this.pair.maybeInitFinished();
-  } while (read > 0 && !this._buffer.isFull && bytesRead < size);
+
+    // `maybeInitFinished()` can emit the 'secure' event which
+    // in turn destroys the connection in case of authentication
+    // failure and sets `this.pair.ssl` to `null`.
+  } while (read > 0 &&
+           !this._buffer.isFull &&
+           bytesRead < size &&
+           this.pair.ssl !== null);
 
   // Create new buffer if previous was filled up
   var pool = this._buffer.pool;