From 92a4dffb11c78830cd4b26993f67c9ea3c7c9964 Mon Sep 17 00:00:00 2001 From: perfectworks Date: Mon, 20 Feb 2012 22:15:22 +0800 Subject: [PATCH 1/2] FIX: LineReader.hasNextLine go to a endless loop when LineReader.buffer is empty --- lib/wrench.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/wrench.js b/lib/wrench.js index 7334757..592aa6f 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -175,7 +175,7 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) { var checkDir = fs.statSync(sourceDir); try { fs.mkdirSync(newDirLocation, checkDir.mode); - } catch (e) { + } catch (e) { //if the directory already exists, that's okay if (e.code !== 'EEXIST') throw e; } @@ -378,6 +378,7 @@ exports.LineReader.prototype = { while(this.buffer.indexOf('\n') === -1) { this.getBufferAndSetCurrentPosition(this.currentPosition); if(this.currentPosition === -1) return false; + if(this.buffer.length === 0) return false; } if(this.buffer.indexOf("\n") > -1) return true; From 48aa600052eba8c61be825fc953f6be3a34f60ba Mon Sep 17 00:00:00 2001 From: perfectworks Date: Wed, 22 Feb 2012 17:43:31 +0800 Subject: [PATCH 2/2] FIX: LineReader.hasNextLine go to an endless loop LineReader.hasNextLine will go to an endless loop when buffer is empty or file not end with \n --- lib/wrench.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/wrench.js b/lib/wrench.js index 592aa6f..07386f8 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -368,9 +368,12 @@ exports.LineReader.prototype = { var res = fs.readSync(this.fd, this.bufferSize, position, "ascii"); this.buffer += res[0]; - if(res[1] === 0) return -1; + if(res[1] === 0) { + this.currentPosition = -1; + } else { + this.currentPosition = position + res[1]; + } - this.currentPosition = position + res[1]; return this.currentPosition; }, @@ -378,7 +381,6 @@ exports.LineReader.prototype = { while(this.buffer.indexOf('\n') === -1) { this.getBufferAndSetCurrentPosition(this.currentPosition); if(this.currentPosition === -1) return false; - if(this.buffer.length === 0) return false; } if(this.buffer.indexOf("\n") > -1) return true;