From 314c6b306038526209d284cf9fdbe978d2a634ec Mon Sep 17 00:00:00 2001 From: isaacs Date: Thu, 4 Oct 2012 17:43:15 -0700 Subject: [PATCH] Don't allow invalid encodings in StringDecoder class --- lib/string_decoder.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/string_decoder.js b/lib/string_decoder.js index 31d4b24702..6b1e30895a 100644 --- a/lib/string_decoder.js +++ b/lib/string_decoder.js @@ -19,8 +19,15 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. +function assertEncoding(encoding) { + if (encoding && !Buffer.isEncoding(encoding)) { + throw new Error('Unknown encoding: ' + encoding); + } +} + var StringDecoder = exports.StringDecoder = function(encoding) { this.encoding = (encoding || 'utf8').toLowerCase().replace(/[-_]/, ''); + assertEncoding(encoding); switch (this.encoding) { case 'utf8': // CESU-8 represents each of Surrogate Pair by 3-bytes