From 817d371826ec344884d20edad52e9d339ee4a207 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 23 Nov 2016 08:56:58 -0500 Subject: [PATCH] fix readUntil infinite loop bug --- compiler/parse/index.js | 15 +++++++++++++-- .../error-unexpected-end-of-input-b/error.json | 8 ++++++++ .../error-unexpected-end-of-input-b/input.html | 1 + .../error-unexpected-end-of-input-c/error.json | 8 ++++++++ .../error-unexpected-end-of-input-c/input.html | 1 + .../error-unexpected-end-of-input/error.json | 6 +++--- test/test.js | 2 +- 7 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 test/parser/error-unexpected-end-of-input-b/error.json create mode 100644 test/parser/error-unexpected-end-of-input-b/input.html create mode 100644 test/parser/error-unexpected-end-of-input-c/error.json create mode 100644 test/parser/error-unexpected-end-of-input-c/input.html diff --git a/compiler/parse/index.js b/compiler/parse/index.js index 80a9c44855..b988dd9ee2 100644 --- a/compiler/parse/index.js +++ b/compiler/parse/index.js @@ -94,8 +94,19 @@ export default function parse ( template ) { }, readUntil ( pattern ) { - const match = pattern.exec( this.template.slice( this.index ) ); - return this.template.slice( this.index, match ? ( this.index += match.index ) : this.template.length ); + if ( this.index >= this.template.length ) parser.error( 'Unexpected end of input' ); + + const start = this.index; + const match = pattern.exec( this.template.slice( start ) ); + + if ( match ) { + const start = this.index; + this.index = start + match.index; + return this.template.slice( start, this.index ); + } + + this.index = this.template.length; + return this.template.slice( start ); }, remaining () { diff --git a/test/parser/error-unexpected-end-of-input-b/error.json b/test/parser/error-unexpected-end-of-input-b/error.json new file mode 100644 index 0000000000..c3ddfee08d --- /dev/null +++ b/test/parser/error-unexpected-end-of-input-b/error.json @@ -0,0 +1,8 @@ +{ + "message": "Unexpected end of input", + "loc": { + "line": 1, + "column": 2 + }, + "pos": 2 +} diff --git a/test/parser/error-unexpected-end-of-input-b/input.html b/test/parser/error-unexpected-end-of-input-b/input.html new file mode 100644 index 0000000000..65504cdfb9 --- /dev/null +++ b/test/parser/error-unexpected-end-of-input-b/input.html @@ -0,0 +1 @@ + { const solo = exists( `test/parser/${dir}/solo` ); ( solo ? it.only : it )( dir, () => { - const input = fs.readFileSync( `test/parser/${dir}/input.html`, 'utf-8' ); + const input = fs.readFileSync( `test/parser/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' ); try { const actual = parse( input );