From 0caf8804da8809e1b930600fc75ab6b636bcb2de Mon Sep 17 00:00:00 2001 From: Andreas Steinbart Date: Sun, 9 Aug 2026 08:39:12 +0000 Subject: [PATCH] refactor(decode): delete the unreachable dispatchOne header parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `dispatchOne` was a third copy of the field-header parser, private to the input stream and called from nowhere. Being dead, it had drifted: it read the fixlen word without `fixlenWordValid` (§4.6), the array count without the ARRAY_MAX ceiling (§6.2) and the array fixlen word without `arrayFixlenWordValid` (§4.8), and it folded every truncation into `error_`, i.e. it would have reported INCOMPLETE input as INVALID. Wiring it up would have re-opened exactly the hole `nestedArrayCountCeiling()` closed: `05 20` takes the `count * element_size` skip, wraps size_t to zero bytes and decodes COMPLETE a message §6.2 requires to be INVALID. Delete it, and point the one doc comment that named it (`@ref dispatchOne` on the §7.3 tag-match seam) at the two parsers that actually perform the skip, `parseTopLevel` and `dispatchLevel`. `headerParsersEnforceCeilings()` guards both halves of this. It drives the §4.6/§4.8/§6.2 violations through the callback entry point — the API a per-field dispatcher serves — and, because an unreachable parser cannot be reached by feeding bytes at all, also scans the header source and requires every `(id<<3)|type` decode site to carry the three ceiling checks. That structural half fails on the parent commit, naming sofab.hpp:4381. Closes #88. --- README.md | 6 +++ include/sofab/sofab.hpp | 83 ++------------------------------ test/CMakeLists.txt | 6 +++ test/test_roundtrip.cpp | 102 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index d04a4ee..12e4010 100644 --- a/README.md +++ b/README.md @@ -684,6 +684,12 @@ Two suites run under CTest: skipped sub-sequence, and the §2 sequence framing above (an all-default sequence omitted, a kept element frame, hold-back at full `MAX_DEPTH`, and identical bytes when a run is committed across a flush boundary). + One check in it is structural rather than byte-driven: the header decodes a + field header `(id<<3)|type` in more than one place, and a copy that omits the + §4.6/§4.8/§6.2 ceilings is a hole that feeding bytes cannot expose for as long + as it stays unreachable — so `headerParsersEnforceCeilings()` also scans + `include/sofab/sofab.hpp` itself (CMake bakes the path in) and requires every + header-decode site to carry those checks. - **`test_vectors`** — replays the shared `assets/test_vectors.json` conformance suite (copied verbatim from `corelib-c-cpp`, the authoritative source) for encode, decode, and byte-at-a-time chunked streaming. It asserts each vector's diff --git a/include/sofab/sofab.hpp b/include/sofab/sofab.hpp index 15d5369..76a5151 100644 --- a/include/sofab/sofab.hpp +++ b/include/sofab/sofab.hpp @@ -2937,10 +2937,11 @@ namespace sofab * and `blob` all share @ref Wire::Fixlen. Every typed @ref read compares * the whole tag here, so half a comparison cannot be written. * - * On a mismatch the field is left unconsumed: @ref dispatchOne then skips - * it exactly like an unknown id, which is what MESSAGE_SPEC §7.3 requires - * — this is **not** an error, and never affects the decode outcome. The - * skip is counted in @ref skipped_ as a diagnostic. + * On a mismatch the field is left unconsumed: the parser that delivered it + * — @ref parseTopLevel at the top level, @ref dispatchLevel inside a + * sequence — then skips it exactly like an unknown id, which is what + * MESSAGE_SPEC §7.3 requires — this is **not** an error, and never affects + * the decode outcome. The skip is counted in @ref skipped_ as a diagnostic. * * A read may admit *more than one* subtype — `read(std::string&)` takes a * `string` or a `blob`, since both are fixlen payloads and a std::string @@ -4355,80 +4356,6 @@ namespace sofab * @return The delivered field's @ref Fix. */ [[nodiscard]] Fix fixType() const noexcept { return fixType_; } - - private: - /** - * @brief Decode the field at the cursor, set its metadata and deliver it to @p cb. - * - * A field whose value @p cb does not @ref read is skipped automatically. - * - * @param cb Callback invoked as `(fieldId, size, count)` for the field. - */ - void dispatchOne(const std::function &cb) noexcept - { - uint64_t header; - if (!getVarint(p_, end_, header)) - { - error_ = true; - return; - } - if ((header >> 3) > ID_MAX) - { - error_ = true; - return; - } - auto fieldId = static_cast(header >> 3); - type_ = static_cast(header & 0x7); - - fixLen_ = 0; count_ = 0; - if (type_ == Wire::Fixlen) - { - uint64_t sub; - if (!getVarint(p_, end_, sub)) - { - error_ = true; - return; - } - fixLen_ = static_cast(sub >> 3); fixType_ = static_cast(sub & 0x7); - } - else if (type_ == Wire::ArrayUnsigned || type_ == Wire::ArraySigned) - { - uint64_t n; - if (!getVarint(p_, end_, n)) - { - error_ = true; - return; - } - count_ = static_cast(n); - } - else if (type_ == Wire::ArrayFixlen) - { - uint64_t n; - if (!getVarint(p_, end_, n)) - { - error_ = true; - return; - } - count_ = static_cast(n); - /* §4.8: the fixlen_word is always present, even for an empty array. */ - uint64_t sub; - if (!getVarint(p_, end_, sub)) - { - error_ = true; - return; - } - fixLen_ = static_cast(sub >> 3); fixType_ = static_cast(sub & 0x7); - } - - consumed_ = false; - const uint8_t *payload = p_; - cb(fieldId, fixLen_, count_); - if (!consumed_) - { - p_ = payload; - skipPayload(); - } - } }; /** diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b927f7c..f3ed540 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,6 +7,12 @@ add_executable(test_roundtrip test_roundtrip.cpp) # the call site and reports a mismatch, which it is not — the pair is consistent. target_compile_options(test_roundtrip PRIVATE -Wall -Wextra -Wpedantic -Werror -Wno-mismatched-new-delete) +# headerParsersEnforceCeilings() reads the header source itself: a copy of the +# field-header parser that no entry point reaches cannot be caught by feeding +# bytes, so the check that every decode site carries the §4.6/§4.8/§6.2 ceilings +# has to be structural. +target_compile_definitions(test_roundtrip PRIVATE + SOFAB_HEADER_SOURCE_PATH="${PROJECT_SOURCE_DIR}/include/sofab/sofab.hpp") target_link_libraries(test_roundtrip PRIVATE sofab_cpp) add_test(NAME test_roundtrip COMMAND test_roundtrip) diff --git a/test/test_roundtrip.cpp b/test/test_roundtrip.cpp index 537ea70..0ab2d8a 100644 --- a/test/test_roundtrip.cpp +++ b/test/test_roundtrip.cpp @@ -14,11 +14,15 @@ #include "sofab/sofab.hpp" +#include #include #include +#include #include #include #include +#include +#include #include #include #include @@ -4143,6 +4147,103 @@ static void nestedArrayCountCeiling() } } +/* --- Every field-header parser in the header enforces the same ceilings + * (CORELIB_PLAN §4.6, §4.8, §6.2). The header decodes `(id<<3)|type` in more + * than one place, and a copy that omits the ceilings is a latent hole: the + * fixlen-array skip computes `count * element_size`, which wraps size_t for a + * large enough count, so a message that must be INVALID skips as if the array + * were empty and decodes COMPLETE (see nestedArrayCountCeiling above, which is + * that exact regression on the nested copy). + * + * Two halves, because one alone cannot see the whole surface. The first drives + * the malformed headers through the CALLBACK entry point, the API a per-field + * dispatcher serves. The second is structural: a copy that is not reachable + * from any entry point cannot be caught by feeding bytes at all, so the source + * is scanned directly and every header-decode site must carry the ceiling + * checks. That is what a dead third parser, unreachable but ready to be wired + * up, trips on (issue #88). --- */ + +static void headerParsersEnforceCeilings() +{ + /* Half 1: the callback entry point rejects each ceiling violation. */ + auto viaCallback = [](const std::vector &b) { + sofab::IStreamInline in([](sofab::id, size_t, size_t) noexcept {}); + return in.feed(b.data(), b.size()).code(); + }; + + { /* id0 fixlen array, count = 2^62, fp32 elements: 2^62 x 4 bytes is exactly + * 2^64, so an unchecked skip wraps to zero bytes and swallows the array. */ + std::vector b = {0x05}; + appendVarint(b, uint64_t{1} << 62); + b.push_back((4u << 3) | 0u); + CHECK(viaCallback(b) == sofab::Error::InvalidMessage, + "header parsers: callback path rejects a fixlen-array count past ARRAY_MAX (§6.2)"); + } + { /* id0 unsigned array, count = 2^31: one past ARRAY_MAX. */ + std::vector b = {0x03}; + appendVarint(b, uint64_t{1} << 31); + CHECK(viaCallback(b) == sofab::Error::InvalidMessage, + "header parsers: callback path rejects an integer-array count past ARRAY_MAX (§6.2)"); + } + { /* id0 fixlen, reserved subtype 4 (§4.6). */ + std::vector b = {0x02, (4u << 3) | 4u}; + CHECK(viaCallback(b) == sofab::Error::InvalidMessage, + "header parsers: callback path rejects a reserved fixlen subtype (§4.6)"); + } + { /* id0 fixlen array of `string` elements: only fp32/fp64 are legal (§4.8). */ + std::vector b = {0x05, 0x01, (1u << 3) | 2u}; + CHECK(viaCallback(b) == sofab::Error::InvalidMessage, + "header parsers: callback path rejects a non-fp element in a fixlen array (§4.8)"); + } + { /* Control: a legal two-element fp32 array still decodes. */ + std::vector b = {0x05, 0x02, (4u << 3) | 0u}; + b.insert(b.end(), 8, 0x00); + size_t seen = 0; + sofab::IStreamInline in([&seen](sofab::id, size_t, size_t) noexcept { ++seen; }); + CHECK(in.feed(b.data(), b.size()).code() == sofab::Error::None && seen == 1, + "header parsers: callback path still decodes a legal fixlen array"); + } + +#ifndef SOFAB_HEADER_SOURCE_PATH + /* The path is baked in by test/CMakeLists.txt; a hand-rolled compile of this + * file (see the build line at the top) has no way to know it, so say out loud + * that the structural half did not run rather than passing quietly. */ + std::printf("SKIP: header parsers: structural check needs -DSOFAB_HEADER_SOURCE_PATH\n"); +#else + /* Half 2: no header-decode site may skip the ceilings, reachable or not. */ + std::ifstream src(SOFAB_HEADER_SOURCE_PATH, std::ios::binary); + std::string text((std::istreambuf_iterator(src)), std::istreambuf_iterator()); + CHECK(text.size() > 1024, "header parsers: the header source is readable"); + + static const char kDecodeSite[] = "static_cast(header & 0x7)"; + std::vector sites; + for (size_t at = text.find(kDecodeSite); at != std::string::npos; + at = text.find(kDecodeSite, at + 1)) + sites.push_back(at); + CHECK(!sites.empty(), "header parsers: at least one field-header decode site is found"); + + for (size_t i = 0; i < sites.size(); ++i) + { + /* The site's region runs to the next decode site (or to the end of the + * file), so a newly added parser is measured on its own checks and never + * borrows those of the one before it. */ + const size_t stop = (i + 1 < sites.size()) ? sites[i + 1] : text.size(); + const std::string region = text.substr(sites[i], stop - sites[i]); + const size_t line = 1 + static_cast( + std::count(text.begin(), text.begin() + static_cast(sites[i]), '\n')); + + for (const char *guard : {"fixlenWordValid", "arrayFixlenWordValid", "ARRAY_MAX"}) + { + char what[192]; + std::snprintf(what, sizeof what, + "header parsers: the parser at sofab.hpp:%zu checks %s", + line, guard); + CHECK(region.find(guard) != std::string::npos, what); + } + } +#endif +} + /* --- MIN_OUTPUT_BUFFER (§5.1): the smallest buffer this port accepts FOR * STREAMING, declared so a caller can size a streaming buffer from the API * instead of finding out at runtime. It binds a buffer installed together @@ -4951,6 +5052,7 @@ int main() varintWidthSweep(); trailingDefaultsStayOnTheWire(); nestedArrayCountCeiling(); + headerParsersEnforceCeilings(); minOutputBuffer(); negativeBlobSize(); encodeFormatCeilings();