From 6f536cc758b5d76c893dc18c1bdf7ff76735afcc Mon Sep 17 00:00:00 2001 From: Ririsoft Date: Thu, 11 Jun 2020 18:26:49 +0200 Subject: [PATCH] fix panic in get_mime_extensions_str get_mime_extensions_str should not panic if the mime type does not contains '/' and return None instead. --- src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a40c4ec..75909fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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..]) }; @@ -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; @@ -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); + } }