From 7c635975d1b48937ff040ec515d9e05aa62b3edf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kuczborski?= Date: Fri, 9 Apr 2021 22:51:55 +0200 Subject: [PATCH 1/5] Fix for crashing range indexes when variable length is 1 --- Sources/Variable.swift | 1 + Tests/StencilTests/VariableSpec.swift | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Sources/Variable.swift b/Sources/Variable.swift index 2da56d9..9d47651 100644 --- a/Sources/Variable.swift +++ b/Sources/Variable.swift @@ -50,6 +50,7 @@ public struct Variable: Equatable, Resolvable { public func resolve(_ context: Context) throws -> Any? { if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) { // String literal + guard variable.count > 1 else { return "" } return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)]) } diff --git a/Tests/StencilTests/VariableSpec.swift b/Tests/StencilTests/VariableSpec.swift index 6445deb..da712ce 100644 --- a/Tests/StencilTests/VariableSpec.swift +++ b/Tests/StencilTests/VariableSpec.swift @@ -63,13 +63,25 @@ final class VariableTests: XCTestCase { let result = try variable.resolve(self.context) as? String try expect(result) == "name" } + + it("can resolve a string literal with one double quote") { + let variable = Variable("\"") + let result = try variable.resolve(self.context) as? String + try expect(result) == "" + } it("can resolve a string literal with single quotes") { let variable = Variable("'name'") let result = try variable.resolve(self.context) as? String try expect(result) == "name" } - + + it("can resolve a string literal with one single quote") { + let variable = Variable("'") + let result = try variable.resolve(self.context) as? String + try expect(result) == "" + } + it("can resolve an integer literal") { let variable = Variable("5") let result = try variable.resolve(self.context) as? Int From 61919c5e8eb0e8fc496a1f663e0b1aedd4ee1f95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kuczborski?= Date: Fri, 9 Apr 2021 23:21:46 +0200 Subject: [PATCH 2/5] PR fixes --- CHANGELOG.md | 3 +++ Sources/Variable.swift | 5 +++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0d6a05..0ae78a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,6 +49,9 @@ _None_ - Throw syntax error on empty variable tags (`{{ }}`) instead `fatalError`. [Ilya Puchka](https://github.com/ilyapuchka) [#263](https://github.com/stencilproject/Stencil/pull/263) +- Fix for crashing range indexes when variable length is 1. + [Łukasz Kuczborski](https://github.com/lkuczborski) + [#306](https://github.com/stencilproject/Stencil/pull/306) ### Internal Changes diff --git a/Sources/Variable.swift b/Sources/Variable.swift index 9d47651..b0180b1 100644 --- a/Sources/Variable.swift +++ b/Sources/Variable.swift @@ -48,10 +48,11 @@ public struct Variable: Equatable, Resolvable { /// Resolve the variable in the given context public func resolve(_ context: Context) throws -> Any? { - if (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) { + if variable.count > 1 && (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) { // String literal - guard variable.count > 1 else { return "" } return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)]) + } else { + return "" } // Number literal From 371a4737d9d93f1fbc419557443d2a0546af6846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kuczborski?= Date: Fri, 9 Apr 2021 23:27:00 +0200 Subject: [PATCH 3/5] Fixed missing braces --- Sources/Variable.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Variable.swift b/Sources/Variable.swift index b0180b1..dbb97b3 100644 --- a/Sources/Variable.swift +++ b/Sources/Variable.swift @@ -48,7 +48,7 @@ public struct Variable: Equatable, Resolvable { /// Resolve the variable in the given context public func resolve(_ context: Context) throws -> Any? { - if variable.count > 1 && (variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\"")) { + if variable.count > 1 && ((variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\""))) { // String literal return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)]) } else { From 521a599a60206b61b1ac76dd0f01d20f95d3d588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kuczborski?= Date: Fri, 9 Apr 2021 23:37:54 +0200 Subject: [PATCH 4/5] Fixed logic and tests --- Sources/Variable.swift | 2 -- Tests/StencilTests/VariableSpec.swift | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Sources/Variable.swift b/Sources/Variable.swift index dbb97b3..122fa4a 100644 --- a/Sources/Variable.swift +++ b/Sources/Variable.swift @@ -51,8 +51,6 @@ public struct Variable: Equatable, Resolvable { if variable.count > 1 && ((variable.hasPrefix("'") && variable.hasSuffix("'")) || (variable.hasPrefix("\"") && variable.hasSuffix("\""))) { // String literal return String(variable[variable.index(after: variable.startIndex) ..< variable.index(before: variable.endIndex)]) - } else { - return "" } // Number literal diff --git a/Tests/StencilTests/VariableSpec.swift b/Tests/StencilTests/VariableSpec.swift index da712ce..e25da2e 100644 --- a/Tests/StencilTests/VariableSpec.swift +++ b/Tests/StencilTests/VariableSpec.swift @@ -67,7 +67,7 @@ final class VariableTests: XCTestCase { it("can resolve a string literal with one double quote") { let variable = Variable("\"") let result = try variable.resolve(self.context) as? String - try expect(result) == "" + try expect(result).to.beNil() } it("can resolve a string literal with single quotes") { @@ -79,7 +79,7 @@ final class VariableTests: XCTestCase { it("can resolve a string literal with one single quote") { let variable = Variable("'") let result = try variable.resolve(self.context) as? String - try expect(result) == "" + try expect(result).to.beNil() } it("can resolve an integer literal") { From 8480648bd39e67362b74ca13f87b6f32e3e741fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kuczborski?= Date: Sat, 10 Apr 2021 05:57:40 +0200 Subject: [PATCH 5/5] Fixed changelog entry --- CHANGELOG.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ae78a9..2e08c4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,9 @@ _None_ ### Bug Fixes -_None_ +- Fix for crashing range indexes when variable length is 1. + [Łukasz Kuczborski](https://github.com/lkuczborski) + [#306](https://github.com/stencilproject/Stencil/pull/306) ### Internal Changes @@ -49,9 +51,6 @@ _None_ - Throw syntax error on empty variable tags (`{{ }}`) instead `fatalError`. [Ilya Puchka](https://github.com/ilyapuchka) [#263](https://github.com/stencilproject/Stencil/pull/263) -- Fix for crashing range indexes when variable length is 1. - [Łukasz Kuczborski](https://github.com/lkuczborski) - [#306](https://github.com/stencilproject/Stencil/pull/306) ### Internal Changes