diff options
author | Fedor Indutny <fedor.indutny@gmail.com> | 2014-03-27 21:45:15 +0400 |
---|---|---|
committer | Fedor Indutny <fedor@indutny.com> | 2014-04-11 01:20:43 +0400 |
commit | 4c36f3e7e6108660fc3629d76036e40c1938f639 (patch) | |
tree | 094b5214eeae8c230550402a4fc5da821ec8e52c /lib/buffer.js | |
parent | 525fad473bfafc2f44e1ab591152ca338fe45f2b (diff) | |
download | node-new-4c36f3e7e6108660fc3629d76036e40c1938f639.tar.gz |
buffer: truncate buffer after string decode
When our estimates for a storage size are higher than the actual length
of decoded data, the destination buffer should be truncated. Otherwise
`Buffer::Length` will give misleading information to C++ layer.
fix #7365
Signed-off-by: Fedor Indutny <fedor@indutny.com>
Diffstat (limited to 'lib/buffer.js')
-rw-r--r-- | lib/buffer.js | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/lib/buffer.js b/lib/buffer.js index ab16cd8ad7..91c13521ac 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -23,6 +23,7 @@ var buffer = process.binding('buffer'); var smalloc = process.binding('smalloc'); var util = require('util'); var alloc = smalloc.alloc; +var truncate = smalloc.truncate; var sliceOnto = smalloc.sliceOnto; var kMaxLength = smalloc.kMaxLength; var internal = {}; @@ -79,7 +80,13 @@ function Buffer(subject, encoding) { // In the case of base64 it's possible that the size of the buffer // allocated was slightly too large. In this case we need to rewrite // the length to the actual length written. - this.length = this.write(subject, encoding); + var len = this.write(subject, encoding); + + // Buffer was truncated after decode, realloc internal ExternalArray + if (len !== this.length) { + this.length = len; + truncate(this, this.length); + } } else { if (util.isBuffer(subject)) subject.copy(this, 0, 0, this.length); |