{"record":{"id":"6fd622d9953aeca9","repo":"nautechsystems/nautilus_trader","slug":"options-chain-buffer-capacity-overflow","errorCode":null,"errorMessage":"options chain buffer capacity overflow","messagePattern":"options chain buffer capacity overflow","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/tardis/src/csv/stream.rs","lineNumber":57,"sourceCode":"        parse_options_chain_record_as_quote, parse_quote_record, parse_trade_record,\n        record::{\n            TardisBookUpdateRecord, TardisOptionsChainRecord, TardisOrderBookSnapshot5Record,\n            TardisOrderBookSnapshot25Record, TardisQuoteRecord, TardisTradeRecord,\n        },\n    },\n};\n\nconst MAX_STREAM_CHUNK_SIZE: usize = 1_000_000;\n\nfn validate_stream_chunk_size(chunk_size: usize) -> anyhow::Result<()> {\n    check_in_range_inclusive_usize(chunk_size, 1, MAX_STREAM_CHUNK_SIZE, stringify!(chunk_size))?;\n    Ok(())\n}\n\nfn options_chain_buffer_capacity(chunk_size: usize) -> anyhow::Result<usize> {\n    chunk_size\n        .checked_mul(2)\n        .ok_or_else(|| anyhow::anyhow!(\"options chain buffer capacity overflow\"))\n}\n\n////////////////////////////////////////////////////////////////////////////////\n// OrderBookDelta Streaming\n////////////////////////////////////////////////////////////////////////////////\n\n/// Streaming iterator over CSV records that yields chunks of parsed data.\nstruct DeltaStreamIterator {\n    reader: Reader<Box<dyn std::io::Read>>,\n    record: StringRecord,\n    buffer: Vec<OrderBookDelta>,\n    chunk_size: usize,\n    instrument_id: Option<InstrumentId>,\n    price_precision: u8,\n    size_precision: u8,\n    last_ts_init: Option<UnixNanos>,\n    last_is_snapshot: bool,\n    limit: Option<usize>,","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/tardis/src/csv/stream.rs#L39-L75","documentation":"The options-chain streaming iterator allocates an internal buffer of `chunk_size * 2` records. On platforms where `usize` is small (or with an absurdly large `chunk_size`), the multiplication can overflow; rather than wrap silently, `options_chain_buffer_capacity` uses `checked_mul` and fails with this error.","triggerScenarios":"Calling the options-chain stream constructor (`new`) with a `chunk_size` so large that `chunk_size * 2` exceeds `usize::MAX` (practically only on 16-bit `usize` or with adversarial/huge values on 32-bit).","commonSituations":"Programmatic/derived chunk sizes (e.g. `usize::MAX` or a computed value from config) rather than realistic record counts; running on an unusual embedded target.","solutions":["Pass a realistic `chunk_size` (e.g. thousands of records, well within `usize::MAX / 2`).","Clamp or validate the configured chunk size before constructing the stream.","Check where the chunk size is computed; an overflowing upstream calculation is likely the root cause."],"exampleFix":"// before\nlet stream = OptionsChainStream::new(usize::MAX, ...);\n// after\nlet chunk_size = requested_chunk_size.min(1_000_000);\nlet stream = OptionsChainStream::new(chunk_size, ...);","handlingStrategy":"validation","validationCode":"def validate_chunk_size(chunk_size: int) -> int:\n    max_safe = (1 << 63) // 2  # conservative usize/2 bound\n    if not (0 < chunk_size < max_safe):\n        raise ValueError(f\"chunk_size must be in (0, {max_safe}), got {chunk_size}\")\n    return chunk_size","typeGuard":"def is_safe_chunk_size(n: int) -> bool:\n    return isinstance(n, int) and 0 < n < (1 << 62)","tryCatchPattern":null,"preventionTips":["Clamp configured chunk sizes to a sane maximum before constructing streams","Never pass raw usize::MAX or unvalidated computed values as chunk_size"],"tags":["rust","overflow","streaming","memory"],"backgroundTag":"value-out-of-range","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}