Skip to content

Commit 9b71f63

Browse files
committed
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.
1 parent 06ecbec commit 9b71f63

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

lightning/src/ln/features.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ mod sealed {
301301
set_shutdown_any_segwit_required);
302302

303303
#[cfg(test)]
304-
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext],
304+
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext],
305305
"Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
306306
set_unknown_feature_required);
307307
}
@@ -774,6 +774,16 @@ mod tests {
774774
assert!(!init_features.supports_gossip_queries());
775775
}
776776

777+
#[test]
778+
fn convert_to_context_with_unknown_flags() {
779+
// Ensure the `from` context has fewer known feature bytes than the `to` context.
780+
assert!(InvoiceFeatures::known().flags.len() < NodeFeatures::known().flags.len());
781+
let invoice_features = InvoiceFeatures::known().set_unknown_feature_optional();
782+
assert!(invoice_features.supports_unknown_bits());
783+
let node_features: NodeFeatures = invoice_features.to_context();
784+
assert!(!node_features.supports_unknown_bits());
785+
}
786+
777787
#[test]
778788
fn set_feature_bits() {
779789
let features = InvoiceFeatures::empty()

0 commit comments

Comments
 (0)