-
-
Notifications
You must be signed in to change notification settings - Fork 164
Fix non base 10 number parsing #529
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
base: master
Are you sure you want to change the base?
Changes from all commits
87384f1
5cf1594
01434f5
16c95d4
4af26b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -150,7 +150,7 @@ func (j *tomlReader) readNode(p *unstable.Parser, n *unstable.Node) (string, *mo | |
| } | ||
| return "", model.NewFloatValue(f), nil | ||
| case unstable.Integer: | ||
| i64, err := strconv.ParseInt(string(n.Data), 10, 64) | ||
| i64, err := strconv.ParseInt(string(n.Data), 0, 64) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not an expert in TOML, but I took a quick look at the spec
Valid 42, -42, 0x12, 0o7, 0b111, 12_000, -12_000 Valid but as string: "042" Invalid -0x12, -0o7, -0b111, 042 All these values are valid with base 0 with ParseInt but not in TOML Here are also feedbacks provided by an LLM |
||
| if err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -204,6 +204,91 @@ name2: Tom | |
| `, | ||
| }.run) | ||
|
|
||
| t.Run("base numbers", func(t *testing.T) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are some edge cases you could consider
Go’s strconv.ParseInt / ParseFloat won’t accept |
||
| t.Run("standard", rwTestCase{ | ||
| in: `10 | ||
| `, | ||
| out: `10 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("zero", rwTestCase{ | ||
| in: `0 | ||
| `, | ||
| out: `0 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("negative", rwTestCase{ | ||
| in: `-42 | ||
| `, | ||
| out: `-42 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("hex lowercase", rwTestCase{ | ||
| in: `0x10 | ||
| `, | ||
| out: `16 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("hex uppercase letters", rwTestCase{ | ||
| in: `0xff | ||
| `, | ||
| out: `255 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("octal", rwTestCase{ | ||
| in: `0o10 | ||
| `, | ||
| out: `8 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("binary", rwTestCase{ | ||
| in: `0b10 | ||
| `, | ||
| out: `2 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("leading zero is decimal", rwTestCase{ | ||
| in: `010 | ||
| `, | ||
| out: `10 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("hex in map", rwTestCase{ | ||
| in: `val: 0x10 | ||
| `, | ||
| out: `val: 16 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("octal in map", rwTestCase{ | ||
| in: `val: 0o77 | ||
| `, | ||
| out: `val: 63 | ||
| `, | ||
| }.run) | ||
|
|
||
| t.Run("mixed types in map", rwTestCase{ | ||
| in: `dec: 42 | ||
| hex: 0xff | ||
| oct: 0o77 | ||
| bin: 0b1010 | ||
| `, | ||
| out: `dec: 42 | ||
| hex: 255 | ||
| oct: 63 | ||
| bin: 10 | ||
| `, | ||
| }.run) | ||
| }) | ||
|
|
||
| t.Run("bounded yaml expansion", func(t *testing.T) { | ||
| in := `a: &a ["lol","lol","lol","lol","lol","lol","lol","lol","lol"] | ||
| b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been there. I would discourage doing this.
YAML spec is not linked to what Go supports with base 0
Add tests for 0x42 0X42 0o42 0b11 0B11
I feel like at least 0B11 0X42 are invalid in YAML, while valid in Go
Also 1_000 is valid for YAML spec 1.2, but not 1.1
0775 is the decimal number 775 is YAML 1.2, and the octal number 775 in YAML spec 1.1
Go parse 0775 as the octal number 775
1e3 is valid in YAML, but invalid for strconv.ParseInt but valid for strconv.ParseFloat
Please read strconv.ParseInt documentation and check what YAML spec would expect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func_to_intis fine to use go conventions, but all very good points for the yaml parser.I'll revisit the implementation.
Thank you for the in-depth details! While I'm familiar with all these markup languages, I definitely haven't gotten into the finer details like octal parsing for example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's been a while, but I eventually got back to this. I've added some special hanging which I think covers some edge cases.
Do you have any thoughts or test cases you think I've missed? I'm no YAML expert