From: Jeffrey Czyz Date: Wed, 14 Jul 2021 23:18:33 +0000 (-0700) Subject: Test index-out-of-bounds in Features::to_context X-Git-Tag: v0.0.100~26^2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=rust-lightning;a=commitdiff_plain;h=87233488ccfc0dad06ce712b9e594fc20e0e52f1 Test index-out-of-bounds in Features::to_context When there are fewer known `from` feature bytes than known `to` feature bytes, an index-out-of-bounds error can occur if the `from` features have unknown features set in a byte past the greatest known `from` feature byte. --- diff --git a/lightning/src/ln/features.rs b/lightning/src/ln/features.rs index a9f5a10e..5d908ee0 100644 --- a/lightning/src/ln/features.rs +++ b/lightning/src/ln/features.rs @@ -301,7 +301,7 @@ mod sealed { set_shutdown_any_segwit_required); #[cfg(test)] - define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext], + define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext], "Feature flags for an unknown feature used in testing.", set_unknown_feature_optional, set_unknown_feature_required); } @@ -774,6 +774,16 @@ mod tests { assert!(!init_features.supports_gossip_queries()); } + #[test] + fn convert_to_context_with_unknown_flags() { + // Ensure the `from` context has fewer known feature bytes than the `to` context. + assert!(InvoiceFeatures::known().byte_count() < NodeFeatures::known().byte_count()); + let invoice_features = InvoiceFeatures::known().set_unknown_feature_optional(); + assert!(invoice_features.supports_unknown_bits()); + let node_features: NodeFeatures = invoice_features.to_context(); + assert!(!node_features.supports_unknown_bits()); + } + #[test] fn set_feature_bits() { let features = InvoiceFeatures::empty()