Skip to content

Commit

Permalink
Prevent infinite loop on invalid arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Jan 23, 2025
1 parent 4b7b47c commit e85f564
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 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
21 changes: 21 additions & 0 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 @@ -405,4 +408,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");
}
}
22 changes: 22 additions & 0 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 @@ -273,4 +276,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");
}
}
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 e85f564

Please sign in to comment.