summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFedor Indutny <fedor.indutny@gmail.com>2013-11-29 20:09:59 +0400
committerTimothy J Fontaine <tjfontaine@gmail.com>2013-12-02 15:18:04 -0800
commit60f777d343c5aea9021f008c4fb07541ccba4ad4 (patch)
tree7ad316a50df9c12ef682337f9d5ea207c4760aed /lib
parentbd7fa92de421048b0eb8fb810eb66829424fc07f (diff)
downloadnode-60f777d343c5aea9021f008c4fb07541ccba4ad4.tar.gz
tls: fix pool usage race
When calling `encOut` in loop, `maybeInitFinished()` may invoke `clearOut`'s loop, leading to the writing of interleaved data (encrypted and cleartext) into the one shared pool. Move `maybeInitFinished()` out of the loop and add assertion for future.
Diffstat (limited to 'lib')
-rw-r--r--lib/tls.js15
1 files changed, 7 insertions, 8 deletions
diff --git a/lib/tls.js b/lib/tls.js
index 2077b8f5c..ab2704445 100644
--- a/lib/tls.js
+++ b/lib/tls.js
@@ -445,30 +445,29 @@ CryptoStream.prototype._read = function read(size) {
}
var bytesRead = 0,
- start = this._buffer.offset;
+ start = this._buffer.offset,
+ last = start;
do {
+ assert(last === this._buffer.offset);
var read = this._buffer.use(this.pair.ssl, out, size - bytesRead);
if (read > 0) {
bytesRead += read;
}
+ last = this._buffer.offset;
// Handle and report errors
if (this.pair.ssl && this.pair.ssl.error) {
this.pair.error();
break;
}
-
- // Get NPN and Server name when ready
- this.pair.maybeInitFinished();
-
- // `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);
+ // Get NPN and Server name when ready
+ this.pair.maybeInitFinished();
+
// Create new buffer if previous was filled up
var pool = this._buffer.pool;
if (this._buffer.isFull) this._buffer.create();