From 0214b99079e4354b129b2707ba741a01fa2e774c Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Mon, 24 Aug 2026 20:00:53 +0530 Subject: [PATCH 1/2] fix: reject unconsumed trailing input in query parser (#305) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit parse_edn returned as soon as it parsed one complete top-level value, without checking whether tokens remained, so `[1 2 3] garbage ) ] } 12345` silently parsed as `[1 2 3]` with everything after it dropped — no error, no indication anything was ignored. parse_edn now errors if tokens remain once the top-level value is parsed. Separately, parse_query and parse_rule accepted and silently ignored any arguments after their vector, the same defect (transact ...) and (retract ...) already closed for #303 — so `(query [:find ...] :max-results 5)` parsed as the query alone, discarding the trailing clause instead of rejecting the malformed call. Both now reject extra trailing arguments with the same wording transact/retract already use. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Nsh3ZnmoK6PTaUbp4HcqHY --- CHANGELOG.md | 2 ++ src/query/datalog/parser.rs | 67 ++++++++++++++++++++++++++++++++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c07f5fc22..6f15290600 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,6 +105,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **Keywords may contain `?`**: `:person/alive?` now lexes as a single keyword. Previously `?` terminated the keyword, so `[:e :alive? true]` lexed as `:alive` followed by a stray `?` symbol and was rejected as a four-element fact — reporting `Optional 4th element of a fact must be a map`, which named the value rather than the keyword. `?` is a constituent character in EDN keywords and predicate-style names (`:artist/dead?`) are idiomatic. Query variables are unaffected: a `?` not preceded by `:` still begins a symbol. `tests/grammar/grammar.pest` updated to match. +- **The parser no longer silently discards trailing input after a complete form** (#305). `parse_edn` returned successfully as soon as it parsed one complete top-level value, without checking whether any tokens remained — so `[1 2 3] garbage ) ] } 12345` parsed as `[1 2 3]` with everything after it dropped, no error, no indication anything was ignored. `parse_edn` now errors if tokens remain once the top-level value is parsed. Separately, `(query [...])` and `(rule [...])` accepted and silently ignored any arguments after their vector, the same defect `(transact ...)`/`(retract ...)` already closed for #303 — so `(query [:find ...] :max-results 5)` parsed as the query alone, discarding `:max-results 5` (the correct place for that clause is *inside* the vector: `[:find ... :max-results 5]`) rather than rejecting the malformed call. Both now reject extra trailing arguments with the same "unexpected trailing argument(s)" wording `transact`/`retract` already use. + ### Performance - **`BrowserDb` no longer allocates a redundant LRU page cache** (#275): `open_in_memory()`, `open()`, and `import_graph()` now pass page cache capacity `0` to `PersistentFactStorage::new` instead of `256`. The backing `BrowserBufferBackend` is already a `HashMap`-resident, fully-loaded page store, so every `PageCache` hit was a second RAM-to-RAM copy plus LRU bookkeeping for no benefit. diff --git a/src/query/datalog/parser.rs b/src/query/datalog/parser.rs index 4b2a0aefc3..7637d984bb 100644 --- a/src/query/datalog/parser.rs +++ b/src/query/datalog/parser.rs @@ -499,7 +499,14 @@ impl Parser { pub fn parse_edn(input: &str) -> Result { let tokens = tokenize(input)?; let mut parser = Parser::new(tokens); - parser.parse_value() + let value = parser.parse_value()?; + if let Some(token) = parser.peek() { + return Err(format!( + "unexpected trailing input after a complete form: {:?}", + token + )); + } + Ok(value) } /// Parse a Datalog command from EDN @@ -735,6 +742,13 @@ fn parse_query(elements: &[EdnValue]) -> Result { return Err("Query requires a map argument".to_string()); } + if elements.len() > 1 { + return Err(format!( + "query takes (query [...]); found {} unexpected trailing argument(s)", + elements.len() - 1 + )); + } + // SAFETY: is_empty() check above guarantees index 0 exists #[allow(clippy::indexing_slicing)] let query_vector = elements[0] @@ -1836,6 +1850,13 @@ fn parse_rule(elements: &[EdnValue]) -> Result { return Err("Rule must have a body".to_string()); } + if elements.len() > 1 { + return Err(format!( + "rule takes (rule [...]); found {} unexpected trailing argument(s)", + elements.len() - 1 + )); + } + // Parse the rule body (single vector containing head + body patterns) // SAFETY: is_empty() check above guarantees index 0 exists #[allow(clippy::indexing_slicing)] @@ -2606,6 +2627,50 @@ mod tests { ); } + #[test] + fn test_parse_edn_rejects_trailing_tokens_after_complete_form() { + // A syntactically complete top-level form followed by garbage tokens + // must be a hard error, not silently truncated parsing (#305). + let result = parse_edn("[1 2 3] garbage-trailing-tokens ) ] } 12345"); + assert!( + result.is_err(), + "trailing tokens after a complete form must be rejected" + ); + } + + #[test] + fn test_parse_datalog_command_rejects_trailing_tokens_after_complete_form() { + let result = parse_datalog_command( + r#"(query [:find ?e :where [?e :entity-type :type/commit]]) garbage-trailing-tokens ) ] } 12345"#, + ); + assert!( + result.is_err(), + "trailing tokens after a complete command must be rejected" + ); + } + + #[test] + fn test_parse_query_rejects_extra_argument_after_vector() { + // :max-results must be a keyword INSIDE the query vector; putting it + // after the vector as a sibling argument must not be silently dropped (#305). + let result = + parse_datalog_command(r#"(query [:find ?e :where [?e :a :b]] :max-results 5)"#); + assert!( + result.is_err(), + "extra trailing argument to query must be rejected, not silently ignored" + ); + } + + #[test] + fn test_parse_rule_rejects_extra_argument_after_vector() { + let result = + parse_datalog_command(r#"(rule [(reachable ?a ?b) [?a :edge ?b]] :unexpected-extra)"#); + assert!( + result.is_err(), + "extra trailing argument to rule must be rejected, not silently ignored" + ); + } + #[test] fn test_parse_limits_order_independent() { // :max-derived-facts before :find — order should not matter From 9c8b7ac46bd889feb8e4795e86704bfc4753b05b Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Mon, 24 Aug 2026 20:07:04 +0530 Subject: [PATCH 2/2] test: assert on specific error message in #305 regression tests Per code review: the 4 new trailing-input tests only checked is_err(), which can't distinguish the intended rejection from an unrelated one. Assert on the error message content instead. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Nsh3ZnmoK6PTaUbp4HcqHY --- src/query/datalog/parser.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/query/datalog/parser.rs b/src/query/datalog/parser.rs index 7637d984bb..d577549772 100644 --- a/src/query/datalog/parser.rs +++ b/src/query/datalog/parser.rs @@ -2636,6 +2636,12 @@ mod tests { result.is_err(), "trailing tokens after a complete form must be rejected" ); + let msg = result.unwrap_err(); + assert!( + msg.contains("unexpected trailing input"), + "wrong error: {}", + msg + ); } #[test] @@ -2647,6 +2653,12 @@ mod tests { result.is_err(), "trailing tokens after a complete command must be rejected" ); + let msg = result.unwrap_err(); + assert!( + msg.contains("unexpected trailing input"), + "wrong error: {}", + msg + ); } #[test] @@ -2659,6 +2671,12 @@ mod tests { result.is_err(), "extra trailing argument to query must be rejected, not silently ignored" ); + let msg = result.unwrap_err(); + assert!( + msg.contains("unexpected trailing argument"), + "wrong error: {}", + msg + ); } #[test] @@ -2669,6 +2687,12 @@ mod tests { result.is_err(), "extra trailing argument to rule must be rejected, not silently ignored" ); + let msg = result.unwrap_err(); + assert!( + msg.contains("unexpected trailing argument"), + "wrong error: {}", + msg + ); } #[test]