From 40d034b7e37dd3d19f324ec8bcef5fafc03fed2a Mon Sep 17 00:00:00 2001 From: Chris Heller Date: Fri, 16 Dec 2016 01:33:44 -0800 Subject: 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 14877eb4d..45c2669f9 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 000000000..5c4245d54 --- /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 + -- cgit 1.4.1-2-gfad0