Skip to content

YAML/Parser: rfl::yaml::Reader silently accepts duplicate mapping keys and swallows non-scalar keys, forcing a YAML::EventHandler pre-pass #160

Not planned

Description

@clockblocker

Part of #138
Popularity: 1/7 corpus consumers (moqx)

Related issues

Evidence in openmoq/moqx (60bab7f13925acbaf578670223e660d71e891547)

moqx had to build a dedicated 137-line YAML::EventHandler pre-pass (rejectDuplicateKeys) that parses every YAML configuration file a second time before handing the raw text to rfl::yaml::read:

  • src/config/loader/YamlDuplicateKeys.h#L13-L19:
    /// Rejects a YAML document that repeats a key within one mapping, or uses a
    /// collection as a mapping key.
    /// yaml-cpp keeps every entry of a repeated key; the reflect step then silently
    /// drops or keeps the extras.
    /// Throws std::runtime_error naming the key and line.
    /// Throws YAML::Exception on malformed YAML.
    void rejectDuplicateKeys(const std::string& content);
  • src/config/YamlDuplicateKeys.cpp#L23-L111: implements DuplicateKeyChecker : public YAML::EventHandler (event-stream based because walking the YAML::Node graph expands self-referencing/nested aliases exponentially), tracks per-mapping key sets, normalizes null keys (~/null) to "null" to match reflect-cpp's reader (// The reflect step reads a null key as "null".), resolves scalar anchors/aliases, and rejects sequence/map/collection-alias keys (// The reflect step reads every key as a string, so a collection key never names a field.).
  • src/config/Loader.cpp#L59-L68:
    try {
      rejectDuplicateKeys(content);
    } catch (const YAML::Exception&) {
      // Malformed YAML: the reflect step below reports it.
    } catch (const std::runtime_error& e) {
      throw std::runtime_error("Failed to parse config file '" + path + "': " + e.what());
    }
    
    auto result = strict ? rfl::yaml::read<ParsedConfig, rfl::NoExtraFields>(content)
                         : rfl::yaml::read<ParsedConfig>(content);
  • test/config/LoaderTest.cpp#L542-L650: 9 unit tests covering duplicate listener/service/nested keys, ~ vs null key collisions, scalar alias duplicates, self-referencing/fan-out anchors, and collection keys.

Root Cause in reflect-cpp

In include/rfl/yaml/Reader.hpp (read_object):

template <class ObjectReader>
std::optional<Error> read_object(const ObjectReader& _object_reader,
                                 const InputObjectType& _obj) const noexcept {
  for (const auto& p : _obj.node_) {
    try {
      const auto k = p.first.as<std::string>();
      _object_reader.read(std::string_view(k), InputVarType(p.second));
    } catch (std::exception& e) {
      continue;
    }
  }
  return std::nullopt;
}
  1. yaml-cpp preserves duplicate entries in a YAML::Node mapping; read_object feeds each entry blindly into _object_reader.read(...), silently overwriting/ignoring duplicates even when rfl::NoExtraFields is active.
  2. When a mapping key is a sequence or map (non-scalar), p.first.as<std::string>() throws YAML::BadConversion, and catch (std::exception& e) { continue; } silently drops the entire entry instead of returning an rfl::Error.

Suggested Direction / Fix

  • In rfl::yaml::Reader::read_object, return an rfl::Error instead of silently continue-ing when p.first is not a scalar / cannot convert to std::string (e.g., check !p.first.IsScalar() or propagate the conversion error).
  • Support duplicate-key rejection (either as a processor like rfl::NoDuplicateKeys or as part of strict/YAML reader validation, using YAML::EventHandler or per-mapping key tracking) so YAML config consumers don't have to parse documents twice with custom yaml-cpp event handlers.

Activity

  1. added
    known-downstream-workarounds
    on Nov 29, 2026
  2. added
    wontfix
    on Nov 29, 2026
  3. clockblocker commented on Nov 29, 2026

    @clockblocker
    OwnerAuthor

    Triage decision: closing the rfl::NoDuplicateKeys core-parser feature scope as wontfix (not planned) for the upstream adoption track.

    Rationale (NoDuplicateKeys): Prototyped in fork PR #128, which scored 55/100 (below the >= 65 cutline) due to deep core parser hooks, a 77-line hand-rolled FNV-1a hash table in json/Reader.hpp, an unapproved Reader-vs-ObjectReader contract, and toml++ mismatches. PR #128 remains parked on discuss/ssot (see Draft PR #191 parked table).

    Important exception — NOT wontfix: The separate, tiny (~5 LOC) bug where rfl::yaml::Reader::read_object silently swallows non-scalar mapping keys (catch (std::exception&) { continue; }) is not wontfix — it is a genuine parser correctness bug and is queued to be fixed alongside #161 in upstream-queue (Draft PR #191).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    known-downstream-workaroundswontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions