cast icon indicating copy to clipboard operation
cast copied to clipboard

Unexpected results with padded strings

Open jsumners opened this issue 1 year ago • 3 comments

https://play.golang.com/p/3oTZVLEkdas

package main

import (
	"fmt"
	"github.com/spf13/cast"
)

func main() {
	x := cast.ToInt("08")
	fmt.Printf("08 => %d\n", x)
	
	x = cast.ToInt("8")
	fmt.Printf("8 => %d\n", x)
}

I'd expect the result to be 8 in each scenario. However, "08" ends up hitting a path where the base is changed and the parsing returns an error that we don't see.

jsumners avatar Apr 01 '23 14:04 jsumners

By reading the source code, I found that this problem occurs because 0 is the prefix of octal (just like 0x for hex). Maybe you can discard the leading 0 first.

shplume avatar Apr 05 '23 12:04 shplume

Yes, that is what I stated in the original report. The path is

  1. https://github.com/spf13/cast/blob/ba0a5b63c8972d97f400e6d235d07a2f52532f01/caste.go#L491-L496
  2. https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/strconv/atoi.go;l=202
  3. https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/strconv/atoi.go;l=78
  4. https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/strconv/atoi.go;l=107
  5. https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/strconv/atoi.go;l=131
  6. https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/strconv/atoi.go;l=145
  7. https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/strconv/atoi.go;l=153
  8. https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/strconv/atoi.go;l=53
  9. https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/strconv/atoi.go;l=225
  10. https://github.com/spf13/cast/blob/ba0a5b63c8972d97f400e6d235d07a2f52532f01/caste.go#L496

The point is, cast.ToInt should consider leading 0 characters and do the right thing. Basically, I would only support 0o (0O) to indicate octals when casting (https://go.dev/ref/spec#Integer_literals).

jsumners avatar Apr 05 '23 13:04 jsumners

https://play.golang.com/p/mPCpDwylr7T

Thank you for the explanation. In my opinion, if cast.ToInt considers leading 0 characters, cast.ToInt("012") will return 12, but int variables with value 012 is 10 in Go(like above). I think this might not be appropriate.

shplume avatar Apr 05 '23 14:04 shplume