expression

Prost: detect truncated binary input

A partial varint is malformed when it has no terminating byte. Decode returns a Result; the example checks the error without depending on library error wording. Bound incoming message size before accepting untrusted data. Run in the accompanying generated-bindings Cargo project. Expected stdout (not executed): malformed: true Common mistake: Unwrapping the decode result for arbitrary external bytes.

Formal Syntax & Structure

rust syntaxrust
T::decode(input).is_err()

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use bm_tonic_inventory::pb::GetItemRequest;    use prost::Message;    let bad = [0x08_u8, 0x80];    println!("malformed: {}", GetItemRequest::decode(&bad[..]).is_err());}
Rust Reference