From dfc74bab06fb9e0defc794b8f0fbedcbba90257c Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Sat, 16 Jul 2022 21:05:18 -0700 Subject: [PATCH 1/2] Disallow panic in production code --- src/lib.rs | 22 +++++++++++++--------- src/tests/mod.rs | 4 ++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 247fcd2..6c9c864 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,22 +11,26 @@ // specific language governing permissions and limitations under // each license. -#![deny(warnings)] +#![deny(clippy::expect_used)] +#![deny(clippy::panic)] +#![deny(clippy::unwrap_used)] #![deny(missing_docs)] +#![deny(warnings)] #![doc = include_str!("../README.md")] mod ffi; - -#[cfg(test)] -mod tests; - mod xmp_date_time; mod xmp_error; mod xmp_file; mod xmp_meta; pub mod xmp_ns; -pub use xmp_date_time::XmpDateTime; -pub use xmp_error::{XmpError, XmpErrorType, XmpResult}; -pub use xmp_file::{OpenFileOptions, XmpFile}; -pub use xmp_meta::XmpMeta; +pub use { + xmp_date_time::XmpDateTime, + xmp_error::{XmpError, XmpErrorType, XmpResult}, + xmp_file::{OpenFileOptions, XmpFile}, + xmp_meta::XmpMeta, +}; + +#[cfg(test)] +mod tests; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index a4e5865..689862d 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -14,6 +14,10 @@ // Tests are grouped under this module so as to avoid // having the test code itself included in coverage numbers. +#![allow(clippy::expect_used)] +#![allow(clippy::panic)] +#![allow(clippy::unwrap_used)] + mod fixtures; mod xmp_date_time; mod xmp_error; From 38f997c76df5e96bd86d342bffb3f8c6e0a7a19a Mon Sep 17 00:00:00 2001 From: Eric Scouten Date: Sat, 16 Jul 2022 21:13:03 -0700 Subject: [PATCH 2/2] Remove .unwrap() calls in XmpMeta::property --- src/tests/xmp_meta.rs | 12 ++++++++++++ src/xmp_meta.rs | 15 ++++++++------- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/tests/xmp_meta.rs b/src/tests/xmp_meta.rs index bc3fc0a..194d8a2 100644 --- a/src/tests/xmp_meta.rs +++ b/src/tests/xmp_meta.rs @@ -110,6 +110,18 @@ mod property { let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap(); assert_eq!(m.property(xmp_ns::XMP, ""), None); } + + #[test] + fn invalid_namespace() { + let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap(); + assert_eq!(m.property("\0", "CreatorTool"), None); + } + + #[test] + fn invalid_prop_name() { + let m = XmpMeta::from_file(fixture_path("Purple Square.psd")).unwrap(); + assert_eq!(m.property(xmp_ns::XMP, "\0"), None); + } } mod set_property { diff --git a/src/xmp_meta.rs b/src/xmp_meta.rs index 81e6dd9..cdd2c90 100644 --- a/src/xmp_meta.rs +++ b/src/xmp_meta.rs @@ -11,13 +11,14 @@ // specific language governing permissions and limitations under // each license. -use std::{ - ffi::{CStr, CString}, - path::Path, +use { + crate::{ffi, OpenFileOptions, XmpDateTime, XmpError, XmpErrorType, XmpFile, XmpResult}, + std::{ + ffi::{CStr, CString}, + path::Path, + }, }; -use crate::{ffi, OpenFileOptions, XmpDateTime, XmpError, XmpErrorType, XmpFile, XmpResult}; - /// The `XmpMeta` struct allows access to the XMP Toolkit core services. /// /// You can create `XmpMeta` structs from metadata that you construct, @@ -124,8 +125,8 @@ impl XmpMeta { /// Any errors (for instance, empty or invalid namespace or property name) /// are ignored; the function will return `None` in such cases. pub fn property(&self, schema_ns: &str, prop_name: &str) -> Option { - let c_ns = CString::new(schema_ns).unwrap(); - let c_name = CString::new(prop_name).unwrap(); + let c_ns = CString::new(schema_ns).unwrap_or_default(); + let c_name = CString::new(prop_name).unwrap_or_default(); unsafe { let c_result = ffi::CXmpMetaGetProperty(self.m, c_ns.as_ptr(), c_name.as_ptr());