toml: panic if access fails to a key that was checked (#12384)

pull/12399/head
Larpon 2021-11-05 13:14:50 +01:00 committed by GitHub
parent 24cd619ff8
commit 9b00564d98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 41 deletions

View File

@ -262,28 +262,18 @@ pub fn (mut p Parser) find_in_table(mut table map[string]ast.Value, key string)
ks := key.split('.') ks := key.split('.')
unsafe { unsafe {
for k in ks { for k in ks {
if k in t.keys() { if val := t[k] {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'found key "$k" in $t.keys()') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'found key "$k" in $t.keys()')
if val := t[k] or {
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' this should never happen. Key "$k" was checked before access')
}
{
if val is map[string]ast.Value { if val is map[string]ast.Value {
// unsafe { t = &(val as map[string]ast.Value)
t = &(t[k] as map[string]ast.Value)
//}
} else { } else {
return error(@MOD + '.' + @STRUCT + '.' + @FN + ' "$k" is not a map') return error(@MOD + '.' + @STRUCT + '.' + @FN + ' "$k" is not a map')
} }
}
} else { } else {
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'no key "$k" found, allocating new map "$k" in map ${ptr_str(t)}"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'no key "$k" found, allocating new map "$k" in map ${ptr_str(t)}"')
// unsafe {
t[k] = map[string]ast.Value{} t[k] = map[string]ast.Value{}
t = &(t[k] as map[string]ast.Value) t = &(t[k] as map[string]ast.Value)
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'allocated new map ${ptr_str(t)}"') util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'allocated new map ${ptr_str(t)}"')
//}
} }
} }
} }
@ -534,12 +524,7 @@ pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Value) ? {
key_str := key.str() key_str := key.str()
unsafe { unsafe {
if key_str in table.keys() { if val := table[key_str] {
if val := table[key_str] or {
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' this should never happen. Key "$key_str" was checked before access')
}
{
if val is []ast.Value { if val is []ast.Value {
arr := &(table[key_str] as []ast.Value) arr := &(table[key_str] as []ast.Value)
arr << p.array_of_tables_contents() ? arr << p.array_of_tables_contents() ?
@ -548,7 +533,6 @@ pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Value) ? {
return error(@MOD + '.' + @STRUCT + '.' + @FN + return error(@MOD + '.' + @STRUCT + '.' + @FN +
' table[$key_str] is not an array. (excerpt): "...${p.excerpt()}..."') ' table[$key_str] is not an array. (excerpt): "...${p.excerpt()}..."')
} }
}
} else { } else {
table[key_str] = p.array_of_tables_contents() ? table[key_str] = p.array_of_tables_contents() ?
} }
@ -619,12 +603,7 @@ pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Value) ? {
mut t := &(t_map as map[string]ast.Value) mut t := &(t_map as map[string]ast.Value)
if last in t.keys() { if val := t[last] {
if val := t[last] or {
return error(@MOD + '.' + @STRUCT + '.' + @FN +
' this should never happen. Key "$last" was checked before access')
}
{
if val is []ast.Value { if val is []ast.Value {
arr := &(val as []ast.Value) arr := &(val as []ast.Value)
arr << p.array_of_tables_contents() ? arr << p.array_of_tables_contents() ?
@ -633,7 +612,6 @@ pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Value) ? {
return error(@MOD + '.' + @STRUCT + '.' + @FN + return error(@MOD + '.' + @STRUCT + '.' + @FN +
' t[$last] is not an array. (excerpt): "...${p.excerpt()}..."') ' t[$last] is not an array. (excerpt): "...${p.excerpt()}..."')
} }
}
} else { } else {
t[last] = p.array_of_tables_contents() ? t[last] = p.array_of_tables_contents() ?
} }