From e5346932bcbc523489c9418b82fde31cb666ee99 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Wed, 13 Nov 2013 17:19:53 -0800 Subject: [PATCH] src: make buffer size errors more explicit Fixes #6490 --- lib/buffer.js | 12 ++++++++---- lib/fs.js | 5 +++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index d5e5042568..bfaee2c5ec 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -58,8 +58,10 @@ function Buffer(subject, encoding) { else throw new TypeError('must start with number, buffer, array or string'); - if (this.length > kMaxLength) - throw new RangeError('length > kMaxLength'); + if (this.length > kMaxLength) { + throw new RangeError('Attempt to allocate Buffer larger than maximum ' + + 'size: 0x' + kMaxLength.toString(16) + ' bytes'); + } if (this.length < Buffer.poolSize / 2 && this.length > 0) { if (this.length > poolSize - poolOffset) @@ -92,8 +94,10 @@ function Buffer(subject, encoding) { function SlowBuffer(length) { length = length >>> 0; - if (length > kMaxLength) - throw new RangeError('length > kMaxLength'); + if (this.length > kMaxLength) { + throw new RangeError('Attempt to allocate Buffer larger than maximum ' + + 'size: 0x' + kMaxLength.toString(16) + ' bytes'); + } var b = new NativeBuffer(length); alloc(b, length); return b; diff --git a/lib/fs.js b/lib/fs.js index 77266ebe8e..b5683e5331 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -38,6 +38,7 @@ var Readable = Stream.Readable; var Writable = Stream.Writable; var kMinPoolSpace = 128; +var kMaxLength = require('smalloc').kMaxLength; var O_APPEND = constants.O_APPEND || 0; var O_CREAT = constants.O_CREAT || 0; @@ -204,6 +205,10 @@ fs.readFile = function(path, options, callback_) { return read(); } + if (size > kMaxLength) + throw new RangeError('File size is greater than possible Buffer: ' + + '0x3FFFFFFF bytes'); + buffer = new Buffer(size); read(); });