Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
83 changes: 5 additions & 78 deletions include/sofab/sofab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<void(sofab::id, size_t, size_t)> &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<sofab::id>(header >> 3);
type_ = static_cast<Wire>(header & 0x7);

fixLen_ = 0; count_ = 0;
if (type_ == Wire::Fixlen)
{
uint64_t sub;
if (!getVarint(p_, end_, sub))
{
error_ = true;
return;
}
fixLen_ = static_cast<size_t>(sub >> 3); fixType_ = static_cast<Fix>(sub & 0x7);
}
else if (type_ == Wire::ArrayUnsigned || type_ == Wire::ArraySigned)
{
uint64_t n;
if (!getVarint(p_, end_, n))
{
error_ = true;
return;
}
count_ = static_cast<size_t>(n);
}
else if (type_ == Wire::ArrayFixlen)
{
uint64_t n;
if (!getVarint(p_, end_, n))
{
error_ = true;
return;
}
count_ = static_cast<size_t>(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<size_t>(sub >> 3); fixType_ = static_cast<Fix>(sub & 0x7);
}

consumed_ = false;
const uint8_t *payload = p_;
cb(fieldId, fixLen_, count_);
if (!consumed_)
{
p_ = payload;
skipPayload();
}
}
};

/**
Expand Down
6 changes: 6 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
102 changes: 102 additions & 0 deletions test/test_roundtrip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

#include "sofab/sofab.hpp"

#include <algorithm>
#include <array>
#include <cfloat>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iterator>
#include <new>
#include <span>
#include <string>
Expand Down Expand Up @@ -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<uint8_t> &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<uint8_t> 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<uint8_t> 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<uint8_t> 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<uint8_t> 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<uint8_t> 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<char>(src)), std::istreambuf_iterator<char>());
CHECK(text.size() > 1024, "header parsers: the header source is readable");

static const char kDecodeSite[] = "static_cast<Wire>(header & 0x7)";
std::vector<size_t> 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<size_t>(
std::count(text.begin(), text.begin() + static_cast<std::ptrdiff_t>(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
Expand Down Expand Up @@ -4951,6 +5052,7 @@ int main()
varintWidthSweep();
trailingDefaultsStayOnTheWire();
nestedArrayCountCeiling();
headerParsersEnforceCeilings();
minOutputBuffer();
negativeBlobSize();
encodeFormatCeilings();
Expand Down