Skip to content

Commit

Permalink
Do not pass negative arguments to get_available().
Browse files Browse the repository at this point in the history
  • Loading branch information
clydegerber committed Nov 2, 2023
1 parent b1ba504 commit 0ac4b80
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions rust_icu_uloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ impl ULoc {

/// Implements `uloc_getAvailable`.
pub fn get_available(n: i32) -> Result<ULoc, common::Error> {
if n >= Self::count_available() {
panic!("n is greater than or equal to the value returned from count_available()");
if (0 > n) || (n >= Self::count_available()) {
panic!("n is negative or greater than or equal to the value returned from count_available()");
}
let label = unsafe { ffi::CStr::from_ptr(versioned_function!(uloc_getAvailable)(n)).to_str().unwrap() };
ULoc::try_from(label)
Expand Down Expand Up @@ -1295,8 +1295,14 @@ mod tests {
}

#[test]
#[should_panic(expected = "n is greater than or equal to the value returned from count_available()")]
fn test_get_available_error() {
#[should_panic(expected = "n is negative or greater than or equal to the value returned from count_available()")]
fn test_get_available_negative() {
let _ = ULoc::get_available(-1);
}

#[test]
#[should_panic(expected = "n is negative or greater than or equal to the value returned from count_available()")]
fn test_get_available_overrun() {
let index = ULoc::count_available();
let _ = ULoc::get_available(index);
}
Expand Down

0 comments on commit 0ac4b80

Please sign in to comment.