Skip to content

strings.Split crashes on empty string, empty split #980

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bytbox opened this issue Jul 30, 2010 · 9 comments
Closed

strings.Split crashes on empty string, empty split #980

bytbox opened this issue Jul 30, 2010 · 9 comments

Comments

@bytbox
Copy link
Contributor

bytbox commented Jul 30, 2010

What steps will reproduce the problem?
1. strings.Split(line, "", -1)
2. compile
3. run

What is the expected output?
It should return an empty array.

What do you see instead?
Crash:
panic PC=0x2b75b4c5e228
runtime.panic+0x7c /usr/local/src/i9/src/pkg/runtime/proc.c:1012
        runtime.panic(0x2b7500000000, 0x405eaa)
panicstring+0x60 /usr/local/src/i9/src/pkg/runtime/runtime.c:83
        panicstring(0x464758, 0x2b75b4c7c1e0)
runtime.panicindex+0x1c /usr/local/src/i9/src/pkg/runtime/runtime.c:44
        runtime.panicindex()
strings.explode+0x145 /usr/local/src/i9/src/pkg/strings/strings.go:32
        strings.explode(0x43e408, 0x0, 0x0, 0x43e408, 0x0, ...)
strings.genSplit+0x4f /usr/local/src/i9/src/pkg/strings/strings.go:145
        strings.genSplit(0x43e408, 0x0, 0x0, 0x6, 0x0, ...)
strings.Split+0x43 /usr/local/src/i9/src/pkg/strings/strings.go:173
        strings.Split(0x43e408, 0x0, 0x43e408, 0x0, 0xffffffff00000000, ...)
etc...


What is your $GOOS?  $GOARCH?
$ echo $GOOS and $GOARCH
linux and amd64


Which revision are you using?  (hg identify)
b69ec24ad6dc+ tip
@bytbox
Copy link
Contributor Author

bytbox commented Jul 30, 2010

Comment 1:

correction - strings.Split(line, "", -1) should read strings.Split("", "", -1).

@rsc
Copy link
Contributor

rsc commented Jul 30, 2010

Comment 2:

Nice.

Owner changed to [email protected].

Status changed to Accepted.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2010

Comment 3:

Status changed to HelpWanted.

@gopherbot
Copy link
Contributor

Comment 4 by jan.hosang:

Analogously
strings.Split("", "a", -1)
should be an empty array as well?
Currently the result is [""].

@rsc
Copy link
Contributor

rsc commented Jul 31, 2010

Comment 5:

Re #4: No, the only way to get an empty array is to pass a max of zero (kind of useless).
Split(",,", ",", -1) = []string{"", "", ""}
Split(",", ",", -1) = []string{"", ""}
Split("", ",", -1) = []string{""}

@gopherbot
Copy link
Contributor

Comment 6 by jan.hosang:

But
Split("", "", -1) = []string{}
? (Just to make sure.)
When I fix this, should I include a test for these cases anywhere?

@rsc
Copy link
Contributor

rsc commented Jul 31, 2010

Comment 7:

> Split("", "", -1) = []string{}
It does appear that way.  That's a bit odd, but consistent with the other "" separators,
because
Split("ab", "", -1) = []string{"a", "b"}
NOT Split("ab", "", -1) = []string{"", "a", "b", ""}

@bytbox
Copy link
Contributor Author

bytbox commented Jul 31, 2010

Comment 8:

Potentially, someone might use Split(str, "", -1) to loop through all characters in a
string (that's pretty much the only use for it, I think) - in which case len(Split(str,
"", -1)) had better be the same as len(str).

@robpike
Copy link
Contributor

robpike commented Aug 3, 2010

Comment 9:

This issue was closed by revision 0cd8777.

Status changed to Fixed.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc removed their assignment Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants