Skip to content

gnovm: using escape character "\x1b" in test.Run name crash #4578

@paulogarithm

Description

@paulogarithm

Using escape character ("\x1b" or "\033") in t.Run function in testing make testing crash

Description

you can not use a string that contains the escape character in the first arg of t.Run in a test file:

Your environment

go version: go1.24.4 linux/amd64
uname: Linux prout 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 GNU/Linux
distro Debian
gno version: ufmt-enhancement.2650+5afa07849

Steps to reproduce

package foo

import "testing"

func TestCrash(t *testing.T) {
	str := "\x1b[31mhello\x1b[m"
	println(str) // no crash
	t.Run(str, func(t *testing.T) {
		return
	})
}

Expected behaviour

should just work like

=== RUN   TestCrash
hello
--- PASS: TestCrash (0.00s)

(hello would be in red)

Actual behaviour

Image

Logs

panic: invalid slice index 1 > 0
Stacktrace:
rewrite<VPBlock(3,4)>(name<VPBlock(1,1)>)
    testing/base/match.gno:0
t<VPBlock(1,0)>.Run(str<VPBlock(1,1)>,(const-type testing/base.testingFunc)(func(t *(typeval{testing/base.T})){ ... }))
    testing/base/testing.gno:162
fn<VPBlock(1,1)>(t<~VPBlock(1,0)>)
    gno.land/p/demo/ufmt/ufmt_test.gno:249
tRunner<VPBlock(3,27)>(t<VPBlock(1,5)>,test<VPBlock(1,3)>.F,verbose<VPBlock(1,1)>)
    testing/base/testing.gno:410
RunTest(,true,false,(const-type testing/base.InternalTest)<len=3>)
    testing/base/testing.gno:319

Proposed solution

in testing.Run there is this function rewrite

func (t *T) Run(name string, f testingFunc) bool {
	fullName := t.name + "/" + rewrite(name) // here
        ...
}

that looks like this

func rewrite(s string) string {
	b := []byte{}
	for _, r := range s {
		switch {
		case isSpace(r):
			b = append(b, '_')
		case !unicode.IsPrint(r):
			s := simpleQuoteRune(r) // *here
			b = append(b, s[1:len(s)-1]...)
		default:
			b = append(b, string(r)...)
		}
	}
	return string(b)
}

while debugging, the result of the simpleQuoteRune call (where is the *here) is "." when using the string with escape characters

so when doing s[1:len(s)-1] its doing (".")[1:1-1] which is a range [1:0] and crashes with panic: invalid slice index 1 > 0

why ? because the function simpleQuoteRune is litterally

// simpleQuoteRune does not follow the original strconv.QuoteRune.
func simpleQuoteRune(r rune) string {
	return "."
}

what is it used for ? why does someone coded this so this switch case will always crash ???

Metadata

Metadata

Assignees

Labels

🐞 bugSomething isn't working

Type

No type

Projects

Status

Triage

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions