From 9b00564d98175494aecf819259bdfe4dcbc487bd Mon Sep 17 00:00:00 2001 From: Larpon Date: Fri, 5 Nov 2021 13:14:50 +0100 Subject: [PATCH] toml: panic if access fails to a key that was checked (#12384) --- vlib/toml/parser/parser.v | 60 +++++++++++++-------------------------- 1 file changed, 19 insertions(+), 41 deletions(-) diff --git a/vlib/toml/parser/parser.v b/vlib/toml/parser/parser.v index 2a1fdfabc3749b..6c77921d13305b 100644 --- a/vlib/toml/parser/parser.v +++ b/vlib/toml/parser/parser.v @@ -262,28 +262,18 @@ pub fn (mut p Parser) find_in_table(mut table map[string]ast.Value, key string) ks := key.split('.') unsafe { for k in ks { - if k in t.keys() { + if val := t[k] { 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 { - // unsafe { - t = &(t[k] as map[string]ast.Value) - //} - } else { - return error(@MOD + '.' + @STRUCT + '.' + @FN + ' "$k" is not a map') - } + if val is map[string]ast.Value { + t = &(val as map[string]ast.Value) + } else { + return error(@MOD + '.' + @STRUCT + '.' + @FN + ' "$k" is not a map') } } else { 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 = &(t[k] as map[string]ast.Value) util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'allocated new map ${ptr_str(t)}"') - //} } } } @@ -534,20 +524,14 @@ pub fn (mut p Parser) array_of_tables(mut table map[string]ast.Value) ? { key_str := key.str() unsafe { - if key_str in table.keys() { - if val := table[key_str] or { + if val := table[key_str] { + if val is []ast.Value { + arr := &(table[key_str] as []ast.Value) + arr << p.array_of_tables_contents() ? + table[key_str] = arr + } else { return error(@MOD + '.' + @STRUCT + '.' + @FN + - ' this should never happen. Key "$key_str" was checked before access') - } - { - if val is []ast.Value { - arr := &(table[key_str] as []ast.Value) - arr << p.array_of_tables_contents() ? - table[key_str] = arr - } else { - 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 { table[key_str] = p.array_of_tables_contents() ? @@ -619,20 +603,14 @@ pub fn (mut p Parser) double_array_of_tables(mut table map[string]ast.Value) ? { mut t := &(t_map as map[string]ast.Value) - if last in t.keys() { - if val := t[last] or { + if val := t[last] { + if val is []ast.Value { + arr := &(val as []ast.Value) + arr << p.array_of_tables_contents() ? + t[last] = arr + } else { return error(@MOD + '.' + @STRUCT + '.' + @FN + - ' this should never happen. Key "$last" was checked before access') - } - { - if val is []ast.Value { - arr := &(val as []ast.Value) - arr << p.array_of_tables_contents() ? - t[last] = arr - } else { - return error(@MOD + '.' + @STRUCT + '.' + @FN + - ' t[$last] is not an array. (excerpt): "...${p.excerpt()}..."') - } + ' t[$last] is not an array. (excerpt): "...${p.excerpt()}..."') } } else { t[last] = p.array_of_tables_contents() ?