Skip to content
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

tx/textual/valuerenderer: should formatInteger return "0" for an empty string "" instead of crashing? #12809

Closed
Tracked by #11970
odeke-em opened this issue Aug 3, 2022 · 3 comments
Assignees

Comments

@odeke-em
Copy link
Collaborator

odeke-em commented Aug 3, 2022

Summary of Bug

While auditing the code in TX_SIGNMODE_TEXTUAL, I read through the code in

func formatInteger(v string) (string, error) {
if v[0] == '-' {
which assumes the first leading digits will always be non-empty and confirmed alas that if one passes in the variants of a valid decimal string like ".99" or "10.", the former crashes with

$ go test -run=FormatxDecimal -v
=== RUN   TestFormatxDecimal
Callers: /Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1446
Parts: []string{"", "10"}
--- FAIL: TestFormatxDecimal (0.00s)
panic: runtime error: index out of range [0] with length 0 [recovered]
	panic: runtime error: index out of range [0] with length 0

goroutine 19 [running]:
testing.tRunner.func1.2({0x1446b60, 0xc0000d0288})
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1396 +0x24e
testing.tRunner.func1()
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1399 +0x39f
panic({0x1446b60, 0xc0000d0288})
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/runtime/panic.go:884 +0x212
cosmossdk.io/tx/textual/valuerenderer.formatInteger({0x1470b3a?, 0xc0000ba008?})
	/Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/tx/textual/valuerenderer/int.go:33 +0xef
cosmossdk.io/tx/textual/valuerenderer.formatDecimal({0x1470b3a, 0x3})
	/Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/tx/textual/valuerenderer/dec.go:41 +0x16c
cosmossdk.io/tx/textual/valuerenderer.TestFormatxDecimal(0xc00017a000)
	/Users/emmanuelodeke/go/src/github.com/cosmos/cosmos-sdk/tx/textual/valuerenderer/dec_test.go:6 +0x31
testing.tRunner(0xc00017a000, 0x14a6280)
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1446 +0x10b
created by testing.(*T).Run
	/Users/emmanuelodeke/go/src/go.googlesource.com/go/src/testing/testing.go:1493 +0x35f
exit status 2
FAIL	cosmossdk.io/tx/textual/valuerenderer	0.219s

Version

ccc8003

Steps to Reproduce

package valuerenderer

import "testing"
        
func TestFormatxDecimal(t *testing.T) {
        s, err := formatDecimal(".10")
        if err != nil {
                t.Fatal(err)
        }
        if g, w := s, "0.10"; g != w {
                t.Errorf("got =%q\nwant=%q", g, w)
        }
}

and the question is, should we support "" and return "0" so that ".99" can work just the way "10." works? Fuzzers will also flag this.

Kindly cc-ing @AmauryM @marbar3778.

@tac0turtle
Copy link
Member

@AmauryM @facundomedica is this still an issue?

@amaury1093
Copy link
Contributor

Yes, probably an edge case that's good to be fixed. I'll add it to the Textual tracking issue.

@amaury1093 amaury1093 self-assigned this Mar 6, 2023
@facundomedica
Copy link
Member

  • Passing any of: .99, 1., ., "empty" as amounts in the coin tests results in decimal string cannot be empty or invalid decimal length
  • Passing an empty string to the integer renderers returns invalid syntax

So no crashes now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants