Skip to content

Commit cd035e2

Browse files
authored
toml: fix invalid exception by erroring on duplicate keys in inline table (#26177)
1 parent d696233 commit cd035e2

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

vlib/toml/parser/parser.v

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -818,19 +818,24 @@ pub fn (mut p Parser) inline_table(mut tbl map[string]ast.Value) ! {
818818
p.check_immutable(left_most)!
819819
}
820820
}
821+
key_str := key.str()
822+
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'inserting @6 "${key_str}" = ${val} into ${ptr_str(t)}')
821823
unsafe {
822-
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'inserting @6 "${key}" = ${val} into ${ptr_str(t)}')
823-
t[key.str()] = val
824+
if _ := t[key_str] {
825+
return error(@MOD + '.' + @STRUCT + '.' + @FN +
826+
' key "${key_str}" is already initialized with a value. At "${p.tok.kind}" "${p.tok.lit}" in this (excerpt): "...${p.excerpt()}..."')
827+
}
828+
t[key_str] = val
824829
}
825830
} else {
826831
p.ignore_while(space_formatting)
827832
key, val := p.key_value()!
828833
key_str := key.str()
834+
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'inserting @5 "${key_str}" = ${val} into ${ptr_str(tbl)}')
829835
if _ := tbl[key_str] {
830836
return error(@MOD + '.' + @STRUCT + '.' + @FN +
831837
' key "${key_str}" is already initialized with a value. At "${p.tok.kind}" "${p.tok.lit}" in this (excerpt): "...${p.excerpt()}..."')
832838
}
833-
util.printdbg(@MOD + '.' + @STRUCT + '.' + @FN, 'inserting @5 "${key_str}" = ${val} into ${ptr_str(tbl)}')
834839
tbl[key_str] = val
835840
}
836841
previous_token_was_value = true

vlib/toml/tests/toml_lang_test.v

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const valid_exceptions = [
2424
// NOTE: entries in this list are tests of invalid TOML that should have the parser fail, but currently does not.
2525
const invalid_exceptions = [
2626
'do_not_remove',
27-
'inline-table/duplicate-key-02.toml',
2827
'string/multiline-escape-space-02.toml',
2928
'string/missing-quotes-array.toml',
3029
'table/append-with-dotted-keys-05.toml',

0 commit comments

Comments
 (0)