From f1fc74789733aad6268fbcbd94a865df763eccfd Mon Sep 17 00:00:00 2001 From: Jan Berkel Date: Fri, 1 Sep 2017 14:21:41 +0200 Subject: [PATCH] =?UTF-8?q?fix(for=20tag):=20Potential=20=E2=80=98Index=20?= =?UTF-8?q?out=20of=20range=E2=80=99=20crash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #136 --- CHANGELOG.md | 8 ++++++++ Sources/ForTag.swift | 2 +- Tests/StencilTests/ForNodeSpec.swift | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eaeba7..e7aac18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Stencil Changelog +## Master + +### Bug Fixes + +- Fixes a potential crash when using the `{% for %}` template tag with the + incorrect amount of arguments. + + ## 0.9.0 ### Enhancements diff --git a/Sources/ForTag.swift b/Sources/ForTag.swift index c0c3919..77228d0 100644 --- a/Sources/ForTag.swift +++ b/Sources/ForTag.swift @@ -10,7 +10,7 @@ class ForNode : NodeType { class func parse(_ parser:TokenParser, token:Token) throws -> NodeType { let components = token.components() - guard components.count >= 2 && components[2] == "in" && + guard components.count >= 3 && components[2] == "in" && (components.count == 4 || (components.count >= 6 && components[4] == "where")) else { throw TemplateSyntaxError("'for' statements should use the following 'for x in y where condition' `\(token.contents)`.") } diff --git a/Tests/StencilTests/ForNodeSpec.swift b/Tests/StencilTests/ForNodeSpec.swift index 91ae53d..3c6cdc8 100644 --- a/Tests/StencilTests/ForNodeSpec.swift +++ b/Tests/StencilTests/ForNodeSpec.swift @@ -134,6 +134,15 @@ func testForNode() { let node = ForNode(resolvable: Variable("dict"), loopVariables: ["key", "value"], nodes: nodes, emptyNodes: emptyNodes, where: nil) try expect(try node.render(context)) == "oneItwoII" } + + $0.it("handles invalid input") { + let tokens: [Token] = [ + .block(value: "for i"), + ] + let parser = TokenParser(tokens: tokens, environment: Environment()) + let error = TemplateSyntaxError("'for' statements should use the following 'for x in y where condition' `for i`.") + try expect(try parser.parse()).toThrow(error) + } } }