From 16acb98b6ef427520313120071bedec7022bbc4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Wed, 10 Nov 2021 06:04:31 -0300 Subject: [PATCH] Early return when can't detect extension from magic numbers --- src/utils/fs.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 86f49d4..6d8856f 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -126,10 +126,16 @@ pub fn try_infer_extension(path: &Path) -> Option { } let buf = { - let mut b = [0; 270]; - // Reading errors will just make the inferring fail so its safe to ignore - let _ = std::fs::File::open(&path).map(|mut f| std::io::Read::read(&mut f, &mut b)); - b + let mut buf = [0; 270]; + + // Error cause will be ignored, so use std::fs instead of fs_err + let result = std::fs::File::open(&path).map(|mut file| file.read(&mut buf)); + + // In case of file open or read failure, could not infer a extension + if result.is_err() { + return None; + } + buf }; use crate::extension::CompressionFormat::*;