|
| 1 | +# Code review notes and task list |
| 2 | + |
| 3 | +From a review pass on 2026-07-18 (leak hunting under Linux LeakSanitizer, |
| 4 | +UB probes under AddressSanitizer, plus a read-through of ownership and |
| 5 | +header structure). Items fixed during the pass are marked **[fixed]**; |
| 6 | +the rest are queued for follow-up work, roughly in priority order. |
| 7 | +Suggested workflow: pick an item, reproduce with the command given, |
| 8 | +fix, and run `ctest --test-dir build` plus the item's repro. |
| 9 | + |
| 10 | +## P1 — crashes / correctness |
| 11 | + |
| 12 | +1. **ChainingMapper double-delete (intermittent crash).** |
| 13 | + `lib/ChainingMapper.hpp` `map()`: `delete op` after `BGPSimplifier` |
| 14 | + walks it. The rewriter (`QueryWalker::mapQuery`) can produce trees |
| 15 | + sharing subexpressions, so the tree-delete double-frees. Crashes |
| 16 | + `test_QueryMap bsbm/pieces/transformQuery` on some heap layouts |
| 17 | + (deterministic under `lldb` because ASLR is off; hidden under ASan |
| 18 | + and MallocStackLogging). |
| 19 | + *Suggested approach*: make `QueryWalker`'s sharing explicit — either |
| 20 | + clone at the sharing points (SWObjectDuplicator) or count references |
| 21 | + in the rewrite products. Verify with repeated |
| 22 | + `lldb -b -o run -o quit build/test_QueryMap`. |
| 23 | + |
| 24 | +2. **Eager-filter semantic-action over-fire on unused candidates.** |
| 25 | + `lib/ShExSchema.cpp` `satisfiesShape` step 6 validates every |
| 26 | + (triple, constraint) candidate; a candidate that validates but is |
| 27 | + not chosen by the accepted matching still ran nested shapes' step-10 |
| 28 | + semantic actions. `SemActHandler::mark`/`rollback` scopes bindings |
| 29 | + to *successful* subtrees, but successful-unused candidates still |
| 30 | + leak bindings into e.g. ShExMap. Faithful fix: dispatch semantic |
| 31 | + actions from the accepted solution only (re-walk the winning |
| 32 | + matching, re-validating value expressions with the handler enabled |
| 33 | + and pre-filter dispatch disabled). |
| 34 | + |
| 35 | +3. **SPARQL `md5`/`sha*` builtins unimplemented** (8 known |
| 36 | + `test_SPARQL11` failures). `TTerm::FUNC_md5` etc. exist but no |
| 37 | + evaluator binds them. A small digest implementation (or vendored |
| 38 | + single-file SHA/MD5) in `lib/SWObjects.cpp`'s BuiltIn section would |
| 39 | + turn the suite fully green. |
| 40 | + |
| 41 | +4. **`function_library` CSV `<foo>`-as-IRI** (1 known `test_SPARQL` |
| 42 | + failure). `lib/ResultSet.cpp` csv regex has the IRI branch disabled |
| 43 | + (`// disabled`) since ~2013 while the 2012 test expects it. Decide: |
| 44 | + re-enable for `text/csv` *input* (keeping SPARQL 1.1 CSV *results* |
| 45 | + conformance in `test_SPARQL11` csv tests) or retire the test. |
| 46 | + |
| 47 | +## P2 — leaks / ownership |
| 48 | + |
| 49 | +5. **[fixed]** `restoreFilter` leaked the `FilterExpressions` carrier |
| 50 | + per FILTER clause (LSan: 104 × 24 B via `SPARQLParser.ypp` + |
| 51 | + hangers-on). Now deleted after its expressions move into the |
| 52 | + `Filter` op. |
| 53 | + |
| 54 | +6. **[fixed]** `StarVarSet::project` leaked a fresh `TTermExpression` |
| 55 | + per projected variable (LSan: 100 × 64 B) because it wrapped them |
| 56 | + in a deliberately non-owning alias. Now uses owning |
| 57 | + `ExpressionAlias`. |
| 58 | + |
| 59 | +7. **Bison error-path leaks.** None of the grammars declare |
| 60 | + `%destructor`, so semantic values on the stack leak when a parse |
| 61 | + fails. LSan-confirmed for ShExC: after the fixes above, `test_ShEx` |
| 62 | + under LSan reports exactly this — ~18.5 KB / 222 allocations, all |
| 63 | + through `ShExDriver::parse` from the negativeSyntax suites (AST |
| 64 | + fragments from actions around `ShExCParser.ypp:696/835/985/1009` |
| 65 | + plus scanner token strings from `ShExCScanner.lpp:155-165`); the |
| 66 | + success paths (`test_DAWG`, `bin/sparql` ShExMap run) are clean. |
| 67 | + Adding `%destructor` per union member is mechanical but touchy: |
| 68 | + *do not delete* AtomFactory-interned values (`p_URI`, `p_TTerm`, |
| 69 | + `p_Literal` — the factory owns them), guard `p_SE`/`p_SEs` |
| 70 | + elements against the `driver.emptyShape` sentinel, and remember |
| 71 | + `p_SEs`/`p_TEs`/vector types own their elements. Do |
| 72 | + ShExCParser.ypp first as the template, verify with the docker LSan |
| 73 | + run, then replicate to the other grammars. |
| 74 | + |
| 75 | +8. **`YaccDriver` error-path state.** `SPARQLDriver::curFilter`, |
| 76 | + `curBGP` etc. leak if a throw unwinds mid-parse. Consider owning |
| 77 | + these as `std::unique_ptr` in the drivers (release() at the |
| 78 | + handoff points). |
| 79 | + |
| 80 | +9. **`getRDFLiteral` numeric branches ignore `p_LANGTAG`.** |
| 81 | + `lib/SWObjects.cpp`: the integer/decimal/double/boolean early |
| 82 | + returns drop a passed langtag without deleting it (invalid input, |
| 83 | + but a leak if it happens). Delete or reject explicitly. |
| 84 | + |
| 85 | +## P3 — RAII / API modernization |
| 86 | + |
| 87 | +10. **`ProductionVector` vs `NoDelProductionVector`.** The owning / |
| 88 | + non-owning split is by class choice with manual delete loops in |
| 89 | + destructors. A cleaner shape: one |
| 90 | + `template <typename T, bool Owning>` (or move owning containers to |
| 91 | + `std::vector<std::unique_ptr<const T>>`) so ownership is visible in |
| 92 | + the type. Big mechanical change; start with new code only |
| 93 | + (`ShEx::*` vectors) and leave the SPARQL AST for a dedicated pass. |
| 94 | + Note the TTerm/"POS" objects themselves are fine: AtomFactory owns |
| 95 | + them (interning maps freed in `~AtomFactory`), and their private |
| 96 | + constructors/destructors enforce it. |
| 97 | + |
| 98 | +11. **`NamespaceMap` ownership flag.** `ParserDriver::freeNamespaces` |
| 99 | + bool + raw pointer is a hand-rolled maybe-owner; a |
| 100 | + `std::unique_ptr<NamespaceMap>` plus a non-owning observer pointer |
| 101 | + expresses it directly. |
| 102 | + |
| 103 | +12. **test harness caches** (`tests/test_ShEx.cpp` SchemaCache / |
| 104 | + DataCache) use raw `new`/manual dtor loops — safe today, but |
| 105 | + `std::map<std::string, std::unique_ptr<Entry>>` removes the |
| 106 | + footgun. |
| 107 | + |
| 108 | +## P4 — templating / duplication |
| 109 | + |
| 110 | +13. **`dynamic_cast` dispatch in the ShEx validator.** |
| 111 | + `lib/ShExSchema.cpp` re-implements type dispatch with |
| 112 | + `dynamic_cast` chains in ~8 places (RefWalk, SorbeExpr, predicate |
| 113 | + collection, materializer in ShExMap.cpp) although |
| 114 | + `ShapeExprVisitor`/`TripleExprVisitor` exist. Converting the |
| 115 | + walkers to visitors removes the chains and the risk of missing a |
| 116 | + case when the AST grows. |
| 117 | + |
| 118 | +14. **Grammar action boilerplate.** The `_Q..._E_Star/Plus` vector |
| 119 | + accumulation actions repeat ~20 times per grammar. A tiny driver |
| 120 | + template (`appendTo(vector*, elt)` as in ShExJison's helpers) |
| 121 | + would halve the .ypp action volume. Cosmetic; do when touching a |
| 122 | + grammar anyway. |
| 123 | + |
| 124 | +15. **Interval/cardinality arithmetic** in `ShExSchema.cpp` and the |
| 125 | + `REPEAT_RANGE` handling in the scanner both re-implement |
| 126 | + min/max-with-unbounded logic; unify on `ShEx::Cardinality`. |
| 127 | + |
| 128 | +## P5 — build / header layout |
| 129 | + |
| 130 | +16. **Header-only is fine here — with one exception.** With ~10 |
| 131 | + library TUs, ~25 generated parser TUs, and one-TU test/binary |
| 132 | + targets, keeping SimpleServer/SQLizer/WEBserver as headers is |
| 133 | + reasonable (each has 1–2 consumers; splitting buys little). |
| 134 | + The exception is `SWObjects.hpp` → `Logging.hpp` → Boost.Log/ |
| 135 | + Phoenix/Thread in *every* TU. **[fixed]** for build time via |
| 136 | + `target_precompile_headers` (compile CPU ~111s → ~81s for the |
| 137 | + library). The structural fix, if wanted later: put a minimal |
| 138 | + logging facade in SWObjects.hpp and move the Boost.Log machinery |
| 139 | + behind a .cpp; that also unblocks non-Boost.Log consumers. |
| 140 | + |
| 141 | +17. **[fixed]** `SWObjects_STAND_ALONE` macro-conditional definitions |
| 142 | + in Logging.hpp broke under PCH (macro state isn't seen by a |
| 143 | + precompiled header). Replaced with an explicit |
| 144 | + `w3c_sw_DEFINE_LOGGER_GLOBALS` invoked from SWObjects.cpp. |
| 145 | + |
| 146 | +## P6 — performance |
| 147 | + |
| 148 | +18. **[fixed]** ShEx SORBE forms were rebuilt on every shape |
| 149 | + evaluation; now memoized per triple expression on the `Validator`. |
| 150 | + |
| 151 | +19. **[fixed]** Added a sound `(node, label)` result memo to the |
| 152 | + validator (used only without semantic-action handlers, and only |
| 153 | + for results that took no cyclic assumption). |
| 154 | + |
| 155 | +20. **ShEx neighborhood extraction scans all triples** |
| 156 | + (`satisfiesShape` step 2 iterates the whole graph per focus node). |
| 157 | + `BasicGraphPattern` has SP/PO/OS indexes; using them makes |
| 158 | + validation linear in the neighborhood instead of the graph. |
| 159 | + |
| 160 | +21. **`Matchings` is an unpruned cartesian product.** Fine for |
| 161 | + shexTest-sized neighborhoods; pathological EXTRA/multi-candidate |
| 162 | + data can explode. jena's `FeasibleMatchingsIterator` prunes with |
| 163 | + per-constraint interval bounds — port it if real workloads hit |
| 164 | + this. |
| 165 | + |
| 166 | +22. **`GraphIndex::getObject`** in tests/test_ShEx.cpp is O(triples) |
| 167 | + per lookup over the manifest graph (~17k triples × ~1700 entries). |
| 168 | + Harness-only; index it if registration time ever matters. |
0 commit comments