From 94bd04544c462437caf5895b7014b8e20c94469f Mon Sep 17 00:00:00 2001 From: Brian White Date: Thu, 13 Feb 2014 14:14:06 -0500 Subject: [PATCH] Parser: check regexp success before trying to use captures --- lib/Parser.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/Parser.js b/lib/Parser.js index 040af78..a1a370c 100644 --- a/lib/Parser.js +++ b/lib/Parser.js @@ -161,27 +161,23 @@ Parser.prototype._parse = function(data) { }; Parser.prototype._resTagged = function() { + var m; if (m = RE_LITERAL.exec(this._buffer)) { // non-BODY literal -- buffer it this._buffer = this._buffer.replace(RE_LITERAL, LITPLACEHOLDER); this._literallen = parseInt(m[1], 10); - } else { - var m = RE_TAGGED.exec(this._buffer), - tagnum = parseInt(m[1], 10), - type = m[2].toLowerCase(), - textCode = (m[3] ? parseTextCode(m[3], this._literals) : m[3]), - text = m[4]; + } else if (m = RE_TAGGED.exec(this._buffer)) { this._buffer = ''; - this._literals = []; this.emit('tagged', { - type: type, - tagnum: tagnum, - textCode: textCode, - text: text + type: m[2].toLowerCase(), + tagnum: parseInt(m[1], 10), + textCode: (m[3] ? parseTextCode(m[3], this._literals) : m[3]), + text: m[4] }); - } + } else + this._buffer = ''; }; Parser.prototype._resUntagged = function() { @@ -282,9 +278,15 @@ Parser.prototype._resUntagged = function() { Parser.prototype._resContinue = function() { var m = RE_CONTINUE.exec(this._buffer), textCode, - text = m[2]; + text; + this._buffer = ''; + if (!m) + return; + + text = m[2]; + if (m[1] !== undefined) textCode = parseTextCode(m[1], this._literals);