Skip to content

Fix crash due to index-out-of-bounds in Feature context-translation #1002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lightning/src/ln/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,14 @@ impl<T: sealed::Context> Features<T> {
/// Converts `Features<T>` to `Features<C>`. Only known `T` features relevant to context `C` are
/// included in the result.
fn to_context_internal<C: sealed::Context>(&self) -> Features<C> {
let byte_count = C::KNOWN_FEATURE_MASK.len();
let from_byte_count = T::KNOWN_FEATURE_MASK.len();
let to_byte_count = C::KNOWN_FEATURE_MASK.len();
let mut flags = Vec::new();
for (i, byte) in self.flags.iter().enumerate() {
if i < byte_count {
let known_source_features = T::KNOWN_FEATURE_MASK[i];
let known_target_features = C::KNOWN_FEATURE_MASK[i];
flags.push(byte & known_source_features & known_target_features);
if i < from_byte_count && i < to_byte_count {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test case?

Copy link
Contributor Author

@valentinewallace valentinewallace Jul 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can later but won't have time this week. Asked Matt and he's fine with me PR'ing a test separately later, lmk what you think.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No prob. I don't mind doing it if you want to get this merged in the meantime.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be awesome! 🙏

let from_known_features = T::KNOWN_FEATURE_MASK[i];
let to_known_features = C::KNOWN_FEATURE_MASK[i];
flags.push(byte & from_known_features & to_known_features);
}
}
Features::<C> { flags, mark: PhantomData, }
Expand Down