// Event-store record/field decoding over untrusted bytes (a torn or hostile // segment). Decoders must never crash, and whatever they accept must // re-encode to the same bytes (the encoding is canonical for what we write). #include "strawberry/store/codec.hpp" #include #include #include using namespace strawberry::store; extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { const std::string_view in(reinterpret_cast(data), size); RecordReader records(in); RecordView rec; std::string reencoded; std::size_t accepted = 0; while (records.next(rec)) { FieldReader fields(rec.payload); Field f; FieldWriter w; std::size_t field_bytes = 0; bool canonical = true; while (fields.next(f)) { const std::size_t before = w.data().size(); switch (f.type) { case WireType::varint: w.u64(f.tag, f.value); break; case WireType::fixed32: w.fixed32(f.tag, static_cast(f.value)); break; case WireType::fixed64: w.fixed64(f.tag, f.value); break; case WireType::bytes: w.bytes(f.tag, f.bytes); break; } field_bytes += w.data().size() - before; } // Varints may be non-minimal in hostile input (e.g. 0x80 0x00), so only // a fully parsed payload that is already canonical must match exactly. if (!fields.error() && field_bytes == rec.payload.size() && w.data() != rec.payload) { canonical = false; } if (!canonical) __builtin_trap(); append_record(reencoded, static_cast(rec.type), rec.payload); ++accepted; } // The accepted records are a prefix of the input (modulo varint form). if (!records.error() && accepted > 0 && reencoded.size() > in.size()) __builtin_trap(); return 0; }