From 40d034b7e37dd3d19f324ec8bcef5fafc03fed2a Mon Sep 17 00:00:00 2001 From: Chris Heller Date: Fri, 16 Dec 2016 01:33:44 -0800 Subject: [PATCH 1/4] Guard against calling split with an empty string as a separator. Fixes #5119 --- lib/pure/strutils.nim | 5 +++++ tests/stdlib/tsplit2.nim | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/stdlib/tsplit2.nim diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 14877eb4d5..45c2669f9c 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -808,6 +808,11 @@ proc split*(s: string, sep: string, maxsplit: int = -1): seq[string] {.noSideEff ## ## Substrings are separated by the string `sep`. This is a wrapper around the ## `split iterator <#split.i,string,string>`_. + ## + ## If `sep` is an empty string, `ValueError` is raised. + if sep.len == 0: + raise newException(ValueError, "invalid separator: empty string not allowed") + accumulateResult(split(s, sep, maxsplit)) proc rsplit*(s: string, seps: set[char] = Whitespace, diff --git a/tests/stdlib/tsplit2.nim b/tests/stdlib/tsplit2.nim new file mode 100644 index 0000000000..5c4245d54d --- /dev/null +++ b/tests/stdlib/tsplit2.nim @@ -0,0 +1,19 @@ +discard """ + file: "tsplit2.nim" + output: "true" +""" +import strutils + +var s = "" +for w in split("|abc|xy|z", {'|'}): + s.add("#") + s.add(w) + +try: + discard "hello".split("") + echo "false" +except ValueError: + echo "true" + +#OUT true + From 80a727f1ce74ebb3b4cf0c5a6f75d894638512c8 Mon Sep 17 00:00:00 2001 From: Chris Heller Date: Wed, 21 Dec 2016 12:15:33 -0800 Subject: [PATCH 2/4] Change error handling to use assert as per Araq's comment --- lib/pure/strutils.nim | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 45c2669f9c..314c94909a 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -808,10 +808,7 @@ proc split*(s: string, sep: string, maxsplit: int = -1): seq[string] {.noSideEff ## ## Substrings are separated by the string `sep`. This is a wrapper around the ## `split iterator <#split.i,string,string>`_. - ## - ## If `sep` is an empty string, `ValueError` is raised. - if sep.len == 0: - raise newException(ValueError, "invalid separator: empty string not allowed") + assert(sep.len > 0) accumulateResult(split(s, sep, maxsplit)) From af63bd4e0b45bad7985de829d311a8c84a99006b Mon Sep 17 00:00:00 2001 From: Chris Heller Date: Wed, 21 Dec 2016 19:06:11 -0800 Subject: [PATCH 3/4] Update unit test to handle AssertionError instead of ValueError for #5119 --- tests/stdlib/tsplit2.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/stdlib/tsplit2.nim b/tests/stdlib/tsplit2.nim index 5c4245d54d..7fd9dda747 100644 --- a/tests/stdlib/tsplit2.nim +++ b/tests/stdlib/tsplit2.nim @@ -12,7 +12,7 @@ for w in split("|abc|xy|z", {'|'}): try: discard "hello".split("") echo "false" -except ValueError: +except AssertionError: echo "true" #OUT true From 7ad32ea0edc3f980f54b4bdd50ff9ac09bf74c8b Mon Sep 17 00:00:00 2001 From: Chris Heller Date: Wed, 21 Dec 2016 22:24:08 -0800 Subject: [PATCH 4/4] Change error handling in split to use doAssert so it is not compiled out when running tests --- lib/pure/strutils.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pure/strutils.nim b/lib/pure/strutils.nim index 314c94909a..8b5db49ed2 100644 --- a/lib/pure/strutils.nim +++ b/lib/pure/strutils.nim @@ -808,7 +808,7 @@ proc split*(s: string, sep: string, maxsplit: int = -1): seq[string] {.noSideEff ## ## Substrings are separated by the string `sep`. This is a wrapper around the ## `split iterator <#split.i,string,string>`_. - assert(sep.len > 0) + doAssert(sep.len > 0) accumulateResult(split(s, sep, maxsplit))