Skip to content

Commit

Permalink
fix panic in get_mime_extensions_str
Browse files Browse the repository at this point in the history
get_mime_extensions_str should not panic
if the mime type does not contains '/' and return None
instead.
  • Loading branch information
ririsoft authored and abonander committed Aug 26, 2020
1 parent bea27b3 commit 6f536cc
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,10 @@ pub fn get_mime_extensions_str(mut mime_str: &str) -> Option<&'static [&'static
}

let (top, sub) = {
let split_idx = mime_str.find('/').unwrap();
let split_idx = match mime_str.find('/') {
Some(idx) => idx,
None => return None,
};
(&mime_str[..split_idx], &mime_str[split_idx + 1..])
};

Expand Down Expand Up @@ -432,7 +435,7 @@ pub fn octet_stream() -> Mime {
mod tests {
include!("mime_types.rs");

use super::{from_ext, from_path, expect_mime};
use super::{expect_mime, from_ext, from_path, get_mime_extensions_str};
#[allow(deprecated, unused_imports)]
use std::ascii::AsciiExt;

Expand Down Expand Up @@ -523,4 +526,9 @@ mod tests {
);
}
}

#[test]
fn test_get_mime_extensions_str_no_panic_if_bad_mime() {
assert_eq!(get_mime_extensions_str(""), None);
}
}

0 comments on commit 6f536cc

Please sign in to comment.