From b7fca2e93e8e9d4439d8acc5c02f5e54a0112dac Mon Sep 17 00:00:00 2001 From: Roland Heinze Date: Tue, 10 Aug 2021 11:41:48 +0200 Subject: [PATCH 1/5] removed bug caused by uninitialized variable h in function HeaderParser.prototype._parseHeader --- lib/HeaderParser.js | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/HeaderParser.js b/lib/HeaderParser.js index 8ccb6e5..ec0ad10 100644 --- a/lib/HeaderParser.js +++ b/lib/HeaderParser.js @@ -82,26 +82,28 @@ HeaderParser.prototype._parseHeader = function() { // folded header content // RFC2822 says to just remove the CRLF and not the whitespace following // it, so we follow the RFC and include the leading whitespace ... - this.header[h][this.header[h].length - 1] += lines[i]; - } else { - m = RE_HDR.exec(lines[i]); - if (m) { - h = m[1].toLowerCase(); - if (m[2]) { - if (this.header[h] === undefined) - this.header[h] = [m[2]]; - else - this.header[h].push(m[2]); - } else - this.header[h] = ['']; - if (++this.npairs === this.maxHeaderPairs) - break; - } else { - this.buffer = lines[i]; - modded = true; - break; + if (h) { + this.header[h][this.header[h].length - 1] += lines[i]; + continue; } } + m = RE_HDR.exec(lines[i]); + if (m) { + h = m[1].toLowerCase(); + if (m[2]) { + if (this.header[h] === undefined) + this.header[h] = [m[2]]; + else + this.header[h].push(m[2]); + } else + this.header[h] = ['']; + if (++this.npairs === this.maxHeaderPairs) + break; + } else { + this.buffer = lines[i]; + modded = true; + break; + } } if (!modded) this.buffer = ''; From dbf3bfa964b5577a5d6f314ad48a2c7ab9a60468 Mon Sep 17 00:00:00 2001 From: Roland Heinze Date: Tue, 10 Aug 2021 11:43:05 +0200 Subject: [PATCH 2/5] removed unnecessary code in funvtion HeaderParser.prototype._parseHeader --- lib/HeaderParser.js | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/lib/HeaderParser.js b/lib/HeaderParser.js index ec0ad10..fd0342e 100644 --- a/lib/HeaderParser.js +++ b/lib/HeaderParser.js @@ -72,8 +72,7 @@ HeaderParser.prototype._parseHeader = function() { if (this.npairs === this.maxHeaderPairs) return; - var lines = this.buffer.split(RE_CRLF), len = lines.length, m, h, - modded = false; + var lines = this.buffer.split(RE_CRLF), len = lines.length, m, h; for (var i = 0; i < len; ++i) { if (lines[i].length === 0) @@ -99,14 +98,9 @@ HeaderParser.prototype._parseHeader = function() { this.header[h] = ['']; if (++this.npairs === this.maxHeaderPairs) break; - } else { - this.buffer = lines[i]; - modded = true; - break; - } + } else + return; } - if (!modded) - this.buffer = ''; }; module.exports = HeaderParser; From 6549a5457d2edaeb96c1dd4df7adaaee6ff60ce4 Mon Sep 17 00:00:00 2001 From: Roland Heinze Date: Tue, 10 Aug 2021 11:43:37 +0200 Subject: [PATCH 3/5] the value of end was calculated incorrectly in constructor function HeaderParser. In the case of start > 0, end would be wrong. --- lib/HeaderParser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/HeaderParser.js b/lib/HeaderParser.js index fd0342e..851f01b 100644 --- a/lib/HeaderParser.js +++ b/lib/HeaderParser.js @@ -26,7 +26,7 @@ function HeaderParser(cfg) { this.ss.on('info', function(isMatch, data, start, end) { if (data && !self.maxed) { if (self.nread + (end - start) > MAX_HEADER_SIZE) { - end = (MAX_HEADER_SIZE - self.nread); + end = (MAX_HEADER_SIZE - self.nread) + start; self.nread = MAX_HEADER_SIZE; } else self.nread += (end - start); From cba4d3c35930e68f7e4714b2120094d28d9ad602 Mon Sep 17 00:00:00 2001 From: Roland Heinze Date: Tue, 10 Aug 2021 11:43:55 +0200 Subject: [PATCH 4/5] removed variable _realFinish in constructor function Dicer and all its occurances, as it is not necessary, increases the code size, and adds unwanted complexity. --- lib/Dicer.js | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/lib/Dicer.js b/lib/Dicer.js index 9d580cb..25fcc96 100644 --- a/lib/Dicer.js +++ b/lib/Dicer.js @@ -31,7 +31,6 @@ function Dicer(cfg) { this._dashes = 0; this._parts = 0; this._finished = false; - this._realFinish = false; this._isPreamble = true; this._justMatched = false; this._firstWrite = true; @@ -54,7 +53,7 @@ function Dicer(cfg) { inherits(Dicer, WritableStream); Dicer.prototype.emit = function(ev) { - if (ev === 'finish' && !this._realFinish) { + if (ev === 'finish') { if (!this._finished) { var self = this; process.nextTick(function() { @@ -64,15 +63,11 @@ Dicer.prototype.emit = function(ev) { self._part.emit('error', new Error(type + ' terminated early due to unexpected end of multipart data')); self._part.push(null); process.nextTick(function() { - self._realFinish = true; - self.emit('finish'); - self._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); }); return; } - self._realFinish = true; - self.emit('finish'); - self._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); }); } } else @@ -160,9 +155,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) { this._finished = true; // no more parts will be added if (self._parts === 0) { - self._realFinish = true; - self.emit('finish'); - self._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); } } if (this._dashes) @@ -207,9 +200,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) { this._part.on('end', function() { if (--self._parts === 0) { if (self._finished) { - self._realFinish = true; - self.emit('finish'); - self._realFinish = false; + WritableStream.prototype.emit.call(self, 'finish'); } else { self._unpause(); } From 8003224f899d36aa240d9eb971e7a17366b8af86 Mon Sep 17 00:00:00 2001 From: Roland Heinze Date: Tue, 10 Aug 2021 11:44:04 +0200 Subject: [PATCH 5/5] removed unnecessary else if clause in function Dicer.prototype._oninfo. The else if clause was exactly the opposite of the if clause. --- lib/Dicer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Dicer.js b/lib/Dicer.js index 25fcc96..3c613a4 100644 --- a/lib/Dicer.js +++ b/lib/Dicer.js @@ -183,7 +183,7 @@ Dicer.prototype._oninfo = function(isMatch, data, start, end) { shouldWriteMore = this._part.push(data.slice(start, end)); if (!shouldWriteMore) this._pause = true; - } else if (!this._isPreamble && this._inHeader) { + } else { if (buf) this._hparser.push(buf); r = this._hparser.push(data.slice(start, end));