toml: add better float validation (#12640)

pull/12644/head
Larpon 2021-12-02 10:16:55 +01:00 committed by GitHub
parent f7926ec9a4
commit 9cf7af0c75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 9 deletions

View File

@ -210,6 +210,14 @@ fn (c Checker) check_number(num ast.Number) ? {
return error(@MOD + '.' + @STRUCT + '.' + @FN + return error(@MOD + '.' + @STRUCT + '.' + @FN +
' numbers like "$lit" (float) can not have decimal points on either side of the exponent notation in ...${c.excerpt(num.pos)}...') ' numbers like "$lit" (float) can not have decimal points on either side of the exponent notation in ...${c.excerpt(num.pos)}...')
} }
// Check if it contains other chars than the allowed
for r in lit {
if r !in [`0`, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `.`, `e`, `E`, `-`, `+`,
`_`] {
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' numbers like "$lit" (float) can not contain `${byte(r).ascii_str()}` in ...${c.excerpt(num.pos)}...')
}
}
} else { } else {
if lit.len > 1 && lit.starts_with('0') && lit[1] !in [`b`, `o`, `x`] { if lit.len > 1 && lit.starts_with('0') && lit[1] !in [`b`, `o`, `x`] {
ascii = byte(lit[0]).ascii_str() ascii = byte(lit[0]).ascii_str()

View File

@ -17,15 +17,12 @@ const (
] ]
invalid_exceptions = [ invalid_exceptions = [
'invalid/string-bad-line-ending-escape.toml', 'invalid/string-bad-line-ending-escape.toml',
'invalid/float-no-suffix.toml',
] ]
valid_value_exceptions = [ valid_value_exceptions = [
'valid/unicode-escape.toml', 'valid/unicode-escape.toml',
// These have correct values, and should've passed, but the format of arrays is *mixed* in the JSON ?? // These have correct values, and should've passed, but the format of arrays is *mixed* in the JSON ??
'valid/example2.toml', 'valid/example2.toml',
// Float
'valid/float-exponent.toml',
] ]
// These have correct values, and should've passed as-is, but the format of arrays changes in the JSON ?? // These have correct values, and should've passed as-is, but the format of arrays changes in the JSON ??
@ -259,14 +256,16 @@ fn to_alexcrichton(value ast.Value, array_type int) string {
return '{ "type": "null", "value": "$json_text" }' return '{ "type": "null", "value": "$json_text" }'
} }
ast.Number { ast.Number {
if value.text.contains('inf') || value.text.contains('nan') { text := value.text
if text.contains('inf') || text.contains('nan') {
return '{ "type": "float", "value": "$value.text" }' return '{ "type": "float", "value": "$value.text" }'
} }
if !value.text.starts_with('0x') if !text.starts_with('0x') && (text.contains('.') || text.to_lower().contains('e')) {
&& (value.text.contains('.') || value.text.to_lower().contains('e')) { mut val := ''
mut val := '$value.f64()'.replace('.e+', '.0e') // json notation if text.to_lower().contains('e') && !text.contains('-') {
if !val.contains('.') && val != '0' { // json notation val = '${value.f64():.1f}'
val += '.0' } else {
val = '$value.f64()'
} }
return '{ "type": "float", "value": "$val" }' return '{ "type": "float", "value": "$val" }'
} }