Skip to content

Commit

Permalink
Merge pull request #253 from p2pderivatives/fix/prevent-infinit-loop
Browse files Browse the repository at this point in the history
Fix/prevent infinit loop
  • Loading branch information
Tibo-lg authored Jan 23, 2025
2 parents 4b7b47c + 879d9d0 commit 902a8fd
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
12 changes: 12 additions & 0 deletions dlc-trie/src/digit_decomposition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,18 @@ fn remove_tail(v: &mut Vec<usize>, to_remove: usize) {
}

/// Returns the set of decomposed prefixes that cover the range [start, end].
///
/// # Panics
///
/// Panics if `start` is greater than `end`.
pub fn group_by_ignoring_digits(
start: usize,
end: usize,
base: usize,
nb_digits: usize,
) -> Vec<Vec<usize>> {
assert!(start <= end);

let mut ds = decompose_value(start, base, nb_digits);
let mut de = decompose_value(end, base, nb_digits);

Expand Down Expand Up @@ -629,6 +635,12 @@ mod tests {
}
}

#[test]
#[should_panic]
fn group_by_ignoring_digits_start_greater_than_end_panics() {
super::group_by_ignoring_digits(11, 10, 2, 4);
}

#[test]
fn get_max_ranges_test() {
for test_case in get_max_range_test_cases() {
Expand Down
26 changes: 22 additions & 4 deletions dlc-trie/src/multi_oracle_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ impl<'a> DlcTrie<'a, MultiOracleTrieIter<'a>> for MultiOracleTrie {
let mut trie_infos = Vec::new();
let oracle_numeric_infos = &self.oracle_numeric_infos;
for (cet_index, outcome) in outcomes.iter().enumerate() {
if outcome.count == 0 {
return Err(Error::InvalidArgument);
}
let groups = group_by_ignoring_digits(
outcome.start,
outcome.start + outcome.count - 1,
Expand Down Expand Up @@ -296,10 +299,7 @@ impl Iterator for MultiOracleTrieIter<'_> {
let res = match &self.cur_res {
None => {
if let Some(extra_cover_trie_iterator) = &mut self.extra_cover_trie_iterator {
let res = match extra_cover_trie_iterator.next() {
None => return None,
Some(res) => res,
};
let res = extra_cover_trie_iterator.next()?;
let (indexes, paths) = res.path.iter().fold(
(Vec::new(), Vec::new()),
|(mut indexes, mut paths), x| {
Expand Down Expand Up @@ -405,4 +405,22 @@ mod tests {
])
.expect("Could not retrieve path with extra len.");
}

#[test]
fn test_invalid_range_payout() {
let range_payouts = vec![RangePayout {
start: 0,
count: 0,
payout: Payout {
offer: Amount::ZERO,
accept: Amount::from_sat(200000000),
},
}];

let oracle_numeric_infos = get_variable_oracle_numeric_infos(&[13, 12], 2);
let mut multi_oracle_trie = MultiOracleTrie::new(&oracle_numeric_infos, 2).unwrap();
multi_oracle_trie
.generate(0, &range_payouts)
.expect_err("Should fail when given a range payout with a count of 0");
}
}
27 changes: 23 additions & 4 deletions dlc-trie/src/multi_oracle_trie_with_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ impl<'a> DlcTrie<'a, MultiOracleTrieWithDiffIter<'a>> for MultiOracleTrieWithDif
let mut trie_infos = Vec::new();

for (cet_index, outcome) in outcomes.iter().enumerate() {
if outcome.count == 0 {
return Err(Error::InvalidArgument);
}
let groups = group_by_ignoring_digits(
outcome.start,
outcome.start + outcome.count - 1,
Expand Down Expand Up @@ -145,10 +148,7 @@ impl Iterator for MultiOracleTrieWithDiffIter<'_> {
type Item = TrieIterInfo;

fn next(&mut self) -> Option<Self::Item> {
let res = match self.multi_trie_iterator.next() {
None => return None,
Some(res) => res,
};
let res = self.multi_trie_iterator.next()?;
let (indexes, paths) =
res.path
.iter()
Expand Down Expand Up @@ -273,4 +273,23 @@ mod tests {
&iter_res.paths
);
}

#[test]
fn test_invalid_range_payout() {
let range_payouts = vec![RangePayout {
start: 0,
count: 0,
payout: Payout {
offer: Amount::ZERO,
accept: Amount::from_sat(200000000),
},
}];

let oracle_numeric_infos = get_variable_oracle_numeric_infos(&[13, 12], 2);
let mut multi_oracle_trie =
MultiOracleTrieWithDiff::new(&oracle_numeric_infos, 2, 1, 2).unwrap();
multi_oracle_trie
.generate(0, &range_payouts)
.expect_err("Should fail when given a range payout with a count of 0");
}
}
7 changes: 1 addition & 6 deletions dlc-trie/src/multi_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,7 @@ impl<'a, T> Iterator for MultiTrieIterator<'a, T> {
}
}

let res = self.node_stack.pop();

let ((cur_trie_index, parent_path), mut cur_iter) = match res {
None => return None,
Some(cur) => cur,
};
let ((cur_trie_index, parent_path), mut cur_iter) = self.node_stack.pop()?;

match cur_iter.next() {
None => self.next(),
Expand Down
5 changes: 1 addition & 4 deletions sample/src/hex_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ pub fn hex_str(value: &[u8]) -> String {
}

pub fn to_compressed_pubkey(hex: &str) -> Option<PublicKey> {
let data = match to_vec(&hex[0..33 * 2]) {
Some(bytes) => bytes,
None => return None,
};
let data = to_vec(&hex[0..33 * 2])?;
match PublicKey::from_slice(&data) {
Ok(pk) => Some(pk),
Err(_) => None,
Expand Down

0 comments on commit 902a8fd

Please sign in to comment.