Repository: toml-rs/toml
Pull request #416 (2023-01-03 03:18:37)

fix(parser): Detect more duplicate tables


Patch:
diff --git a/Cargo.lock b/Cargo.lock
index 1b71936c..81c611ce 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -17,6 +17,12 @@ version = "0.1.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
 
+[[package]]
+name = "anyhow"
+version = "1.0.65"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602"
+
 [[package]]
 name = "arbitrary"
 version = "0.2.0"
@@ -124,8 +130,8 @@ checksum = "86447ad904c7fb335a790c9d7fe3d0d971dc523b8ccd1561a520de9a85302750"
 dependencies = [
  "atty",
  "bitflags",
- "clap_derive 3.2.18",
- "clap_lex 0.2.4",
+ "clap_derive",
+ "clap_lex",
  "indexmap",
  "once_cell",
  "strsim",
@@ -133,21 +139,6 @@ dependencies = [
  "textwrap",
 ]
 
-[[package]]
-name = "clap"
-version = "4.0.32"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a7db700bc935f9e43e88d00b0850dae18a63773cfbec6d8e070fccf7fef89a39"
-dependencies = [
- "bitflags",
- "clap_derive 4.0.21",
- "clap_lex 0.3.0",
- "is-terminal",
- "once_cell",
- "strsim",
- "termcolor",
-]
-
 [[package]]
 name = "clap_derive"
 version = "3.2.18"
@@ -161,19 +152,6 @@ dependencies = [
  "syn",
 ]
 
-[[package]]
-name = "clap_derive"
-version = "4.0.21"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0177313f9f02afc995627906bbd8967e2be069f5261954222dac78290c2b9014"
-dependencies = [
- "heck",
- "proc-macro-error",
- "proc-macro2",
- "quote",
- "syn",
-]
-
 [[package]]
 name = "clap_lex"
 version = "0.2.4"
@@ -183,15 +161,6 @@ dependencies = [
  "os_str_bytes",
 ]
 
-[[package]]
-name = "clap_lex"
-version = "0.3.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8"
-dependencies = [
- "os_str_bytes",
-]
-
 [[package]]
 name = "concolor"
 version = "0.0.11"
@@ -219,7 +188,7 @@ dependencies = [
  "atty",
  "cast",
  "ciborium",
- "clap 3.2.22",
+ "clap",
  "criterion-plot",
  "itertools",
  "lazy_static",
@@ -323,6 +292,12 @@ version = "1.0.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
 
+[[package]]
+name = "glob"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
+
 [[package]]
 name = "globset"
 version = "0.4.9"
@@ -392,21 +367,26 @@ dependencies = [
 
 [[package]]
 name = "include_dir"
-version = "0.7.3"
+version = "0.6.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e"
+checksum = "24b56e147e6187d61e9d0f039f10e070d0c0a887e24fe0bb9ca3f29bfde62cab"
 dependencies = [
- "include_dir_macros",
+ "glob",
+ "include_dir_impl",
+ "proc-macro-hack",
 ]
 
 [[package]]
-name = "include_dir_macros"
-version = "0.7.3"
+name = "include_dir_impl"
+version = "0.6.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f"
+checksum = "0a0c890c85da4bab7bce4204c707396bbd3c6c8a681716a51c8814cfc2b682df"
 dependencies = [
+ "anyhow",
+ "proc-macro-hack",
  "proc-macro2",
  "quote",
+ "syn",
 ]
 
 [[package]]
@@ -501,18 +481,7 @@ version = "0.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "79529479c298f5af41375b0c1a77ef670d450b4c9cd7949d2b43af08121b20ec"
 dependencies = [
- "clap 3.2.22",
- "termcolor",
- "threadpool",
-]
-
-[[package]]
-name = "libtest-mimic"
-version = "0.6.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "d7b603516767d1ab23d0de09d023e62966c3322f7148297c35cf3d97aa8b37fa"
-dependencies = [
- "clap 4.0.32",
+ "clap",
  "termcolor",
  "threadpool",
 ]
@@ -661,6 +630,12 @@ dependencies = [
  "version_check",
 ]
 
+[[package]]
+name = "proc-macro-hack"
+version = "0.5.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
+
 [[package]]
 name = "proc-macro2"
 version = "1.0.47"
@@ -757,18 +732,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
 
 [[package]]
 name = "serde"
-version = "1.0.152"
+version = "1.0.149"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb"
+checksum = "256b9932320c590e707b94576e3cc1f7c9024d0ee6612dfbcf1cb106cbe8e055"
 dependencies = [
  "serde_derive",
 ]
 
 [[package]]
 name = "serde_derive"
-version = "1.0.152"
+version = "1.0.149"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "af487d118eecd09402d70a5d72551860e788df87b464af30e5ea6a38c75c541e"
+checksum = "b4eae9b04cbffdfd550eb462ed33bc6a1b68c935127d008b27444d08380f94e4"
 dependencies = [
  "proc-macro2",
  "quote",
@@ -777,9 +752,9 @@ dependencies = [
 
 [[package]]
 name = "serde_json"
-version = "1.0.91"
+version = "1.0.89"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883"
+checksum = "020ff22c755c2ed3f8cf162dbb41a7268d934702f3ed3631656ea597e08fc3db"
 dependencies = [
  "itoa",
  "ryu",
@@ -800,7 +775,7 @@ checksum = "efbd7b250c7243273b5aec4ca366fced84ad716d110bb7baae4814678952ebde"
 dependencies = [
  "concolor",
  "ignore",
- "libtest-mimic 0.5.2",
+ "libtest-mimic",
  "normalize-line-endings",
  "similar",
  "snapbox-macros",
@@ -892,9 +867,9 @@ dependencies = [
 
 [[package]]
 name = "toml"
-version = "0.5.10"
+version = "0.5.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1333c76748e868a4d9d1017b5ab53171dfd095f70c712fdb4653a406547f598f"
+checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
 dependencies = [
  "serde",
 ]
@@ -912,9 +887,9 @@ dependencies = [
 
 [[package]]
 name = "toml-test-data"
-version = "1.1.1"
+version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "727e33b7f232a93b6a2c4b87ff41be5b98e0385383dbaf89d95af78c17dd33a5"
+checksum = "264626d07f1602e14dbba415b9a2145023c46e13c4477bbc9cf7dd15727b9d83"
 dependencies = [
  "include_dir",
 ]
@@ -926,7 +901,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9dd8f1792ea1c5f58d0ff04297bfea7df05dc0d7924e7db7c72360058def550e"
 dependencies = [
  "ignore",
- "libtest-mimic 0.5.2",
+ "libtest-mimic",
  "toml-test",
  "toml-test-data",
 ]
@@ -946,12 +921,12 @@ dependencies = [
  "indexmap",
  "itertools",
  "kstring",
- "libtest-mimic 0.6.0",
+ "libtest-mimic",
  "nom8",
  "serde",
  "serde_json",
  "snapbox",
- "toml 0.5.10",
+ "toml 0.5.9 (registry+https://github.com/rust-lang/crates.io-index)",
  "toml-test-data",
  "toml-test-harness",
  "toml_datetime",
diff --git a/crates/toml/Cargo.toml b/crates/toml/Cargo.toml
index 166b3b2f..c87bcf74 100644
--- a/crates/toml/Cargo.toml
+++ b/crates/toml/Cargo.toml
@@ -49,5 +49,5 @@ indexmap = { version = "1.9.1", optional = true }
 toml_datetime = { version = "0.5.0", path = "../toml_datetime", features = ["serde"] }
 
 [dev-dependencies]
-serde_derive = "1.0.152"
-serde_json = "1.0.91"
+serde_derive = "1.0.149"
+serde_json = "1.0.89"
diff --git a/crates/toml_edit/Cargo.toml b/crates/toml_edit/Cargo.toml
index f8e71ec2..dc78114e 100644
--- a/crates/toml_edit/Cargo.toml
+++ b/crates/toml_edit/Cargo.toml
@@ -55,13 +55,13 @@ kstring = { version = "2.0.0", features = ["max_inline"], optional = true }
 toml_datetime = { version = "0.5.0", path = "../toml_datetime" }
 
 [dev-dependencies]
-serde_json = "1.0.91"
+serde_json = "1.0.89"
 toml-test-harness = "0.4.2"
-toml-test-data = "1.1.1"
-libtest-mimic = "0.6.0"
+toml-test-data = "1.1.0"
+libtest-mimic = "0.5.2"
 snapbox = { version = "0.4.3", features = ["harness"] }
 criterion = "0.4.0"
-toml = "0.5.10"
+toml = "0.5.9"
 
 [[test]]
 name = "decoder_compliance"
diff --git a/crates/toml_edit/src/parser/state.rs b/crates/toml_edit/src/parser/state.rs
index def0e151..2a286456 100644
--- a/crates/toml_edit/src/parser/state.rs
+++ b/crates/toml_edit/src/parser/state.rs
@@ -202,6 +202,13 @@ impl ParseState {
                     table = last_child;
                 }
                 Item::Table(ref mut sweet_child_of_mine) => {
+                    // "Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed"
+                    if dotted && !sweet_child_of_mine.is_implicit() {
+                        return Err(CustomError::DuplicateKey {
+                            key: key.get().into(),
+                            table: None,
+                        });
+                    }
                     table = sweet_child_of_mine;
                 }
                 _ => unreachable!(),
diff --git a/crates/toml_edit_fuzz/parse_document.rs b/crates/toml_edit_fuzz/parse_document.rs
index 7cd13ddc..ca9265ac 100644
--- a/crates/toml_edit_fuzz/parse_document.rs
+++ b/crates/toml_edit_fuzz/parse_document.rs
@@ -8,12 +8,7 @@ libfuzzer_sys::fuzz_target!(|data| {
         if let Ok(doc) = doc {
             let toml = doc.to_string();
             let doc = toml.parse::<Document>();
-            assert!(
-                doc.is_ok(),
-                "Failed to parse `doc.to_string()`: {}\n```\n{}\n```",
-                doc.unwrap_err(),
-                toml
-            );
+            assert!(doc.is_ok());
             let doc = doc.unwrap();
             assert_eq!(doc.to_string(), toml);
         }


Test patch:
diff --git a/crates/test-suite/Cargo.toml b/crates/test-suite/Cargo.toml
index 2fda776f..de66b49c 100644
--- a/crates/test-suite/Cargo.toml
+++ b/crates/test-suite/Cargo.toml
@@ -13,9 +13,9 @@ harness = false
 [dev-dependencies]
 criterion = "0.4.0"
 toml = { path = "../toml" }
-serde = { version = "1.0.152", features = ["derive"] }
-serde_derive = "1.0.152"
-serde_json = "1.0.91"
+serde = { version = "1.0.149", features = ["derive"] }
+serde_derive = "1.0.149"
+serde_json = "1.0.89"
 toml-test-harness = "0.4.2"
 
 [[test]]
diff --git a/crates/toml_edit/tests/decoder_compliance.rs b/crates/toml_edit/tests/decoder_compliance.rs
index 8d0f4f54..d33b53a4 100644
--- a/crates/toml_edit/tests/decoder_compliance.rs
+++ b/crates/toml_edit/tests/decoder_compliance.rs
@@ -3,11 +3,6 @@ mod decoder;
 fn main() {
     let decoder = decoder::Decoder;
     let mut harness = toml_test_harness::DecoderHarness::new(decoder);
-    harness
-        .ignore([
-            "invalid/control/comment-cr.toml",
-            "invalid/table/append-with-dotted-keys-2.toml",
-        ])
-        .unwrap();
+    harness.ignore(["invalid/control/comment-cr.toml"]).unwrap();
     harness.test();
 }
diff --git a/crates/toml_edit/tests/fixtures/invalid/table/append-with-dotted-keys-1.stderr b/crates/toml_edit/tests/fixtures/invalid/table/append-with-dotted-keys-1.stderr
index 643f941f..0ccb245a 100644
--- a/crates/toml_edit/tests/fixtures/invalid/table/append-with-dotted-keys-1.stderr
+++ b/crates/toml_edit/tests/fixtures/invalid/table/append-with-dotted-keys-1.stderr
@@ -2,4 +2,4 @@ TOML parse error at line 17, column 3
    |
 17 |   b.c.t = "Using dotted keys to add to [a.b.c] after explicitly defining it above is not allowed"
    |   ^
-Duplicate key `t`
+Duplicate key `c`
diff --git a/crates/toml_edit/tests/fixtures/invalid/table/append-with-dotted-keys-2.stderr b/crates/toml_edit/tests/fixtures/invalid/table/append-with-dotted-keys-2.stderr
index e69de29b..358930b2 100644
--- a/crates/toml_edit/tests/fixtures/invalid/table/append-with-dotted-keys-2.stderr
+++ b/crates/toml_edit/tests/fixtures/invalid/table/append-with-dotted-keys-2.stderr
@@ -0,0 +1,5 @@
+TOML parse error at line 8, column 3
+  |
+8 |   b.c.d.k.t = "Using dotted keys to add to [a.b.c.d] after explicitly defining it above is not allowed"
+  |   ^
+Duplicate key `d`