diff --git a/src/Parser.ts b/src/Parser.ts index 516e557..f141f48 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -290,6 +290,9 @@ class TokenParser { currentTokenType = this.lookahead(0); } this.match(Token.TOK_RBRACKET); + if (parts[2] === 0) { + throw new Error('Invalid slice, step cannot be 0'); + } return { children: parts, type: 'Slice', diff --git a/test/jmespath.spec.js b/test/jmespath.spec.js index 1187aae..af9e32b 100644 --- a/test/jmespath.spec.js +++ b/test/jmespath.spec.js @@ -104,6 +104,14 @@ describe('parsing', () => { it('should parse field node', () => { expect(compile('foo')).toMatchObject({ type: 'Field', name: 'foo' }); }); + it('should fail on invalid slices', () => { + try { + compile('bar[1:1:0]'); + fail('invalid slice expression was successfully parsed'); + } catch (e) { + expect(e.message).toContain('Invalid slice'); + } + }); }); describe('Searches compiled ast', () => {