Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deny panic in production code #70

Merged
merged 3 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 4 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/tests/xmp_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions src/xmp_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<String> {
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());
Expand Down