fix(parsing): Handle underflow in A6 parsing by wooffie · Pull Request #23047 · vectordotdev/vector

Fixed possible panic with integer subtract in dnsmsg-parser. More info in test section.

 ---- dns_message_parser::tests::repro_crash stdout ----

thread 'dns_message_parser::tests::repro_crash' panicked at lib/dnsmsg-parser/src/dns_message_parser.rs:266:34:
attempt to subtract with overflow
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/std/src/panicking.rs:692:5
   1: core::panicking::panic_fmt
             at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/panicking.rs:75:14
   2: core::panicking::panic_const::panic_const_sub_overflow
             at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/panicking.rs:178:21
   3: dnsmsg_parser::dns_message_parser::DnsMessageParser::parse_a6_rdata
             at ./src/dns_message_parser.rs:266:34
   4: dnsmsg_parser::dns_message_parser::DnsMessageParser::format_unknown_rdata
             at ./src/dns_message_parser.rs:500:38
   5: dnsmsg_parser::dns_message_parser::DnsMessageParser::parse_dns_record
             at ./src/dns_message_parser.rs:190:47
   6: dnsmsg_parser::dns_message_parser::DnsMessageParser::parse_dns_message_section::{{closure}}
             at ./src/dns_message_parser.rs:184:27
   7: core::iter::adapters::map::map_try_fold::{{closure}}
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/map.rs:95:28
   8: core::iter::traits::iterator::Iterator::try_fold
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2370:21
   9: <core::iter::adapters::map::Map<I,F> as core::iter::traits::iterator::Iterator>::try_fold
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/map.rs:121:9
  10: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::try_fold
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/mod.rs:191:9
  11: core::iter::traits::iterator::Iterator::try_for_each
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2431:9
  12: <core::iter::adapters::GenericShunt<I,R> as core::iter::traits::iterator::Iterator>::next
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/mod.rs:174:14
  13: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/spec_from_iter_nested.rs:25:32
  14: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/spec_from_iter.rs:34:9
  15: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3427:9
  16: core::iter::traits::iterator::Iterator::collect
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1971:9
  17: <core::result::Result<V,E> as core::iter::traits::collect::FromIterator<core::result::Result<A,E>>>::from_iter::{{closure}}
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1985:51
  18: core::iter::adapters::try_process
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/mod.rs:160:17
  19: <core::result::Result<V,E> as core::iter::traits::collect::FromIterator<core::result::Result<A,E>>>::from_iter
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1985:9
  20: core::iter::traits::iterator::Iterator::collect
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:1971:9
  21: dnsmsg_parser::dns_message_parser::DnsMessageParser::parse_dns_message_section
             at ./src/dns_message_parser.rs:182:9
  22: dnsmsg_parser::dns_message_parser::DnsMessageParser::parse_as_query_message
             at ./src/dns_message_parser.rs:119:33
  23: dnsmsg_parser::dns_message_parser::tests::repro_crash
             at ./src/dns_message_parser.rs:1609:29
  24: dnsmsg_parser::dns_message_parser::tests::repro_crash::{{closure}}
             at ./src/dns_message_parser.rs:1604:21
  25: core::ops::function::FnOnce::call_once
             at /home/wooffie/.rustup/toolchains/1.85-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
  26: core::ops::function::FnOnce::call_once
             at /rustc/4eb161250e340c8f48f66e2b929ef4a5bed7c181/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Now I just added check like in some places in this source file, maybe this condition can be modified.