rust-keyutils
rust-keyutils copied to clipboard
Reduce uses of unwrap/expect
The current code uses unwrap() and expect() more than it strictly needs to. A better approach would be to have the function return an error in these cases.
Specifically, the error type could be changed to be an enum with variants for:
- IO error, when the syscall fails
- Invalid UTF8
- Invalid Null Byte
- other error conditions we’re currently just panicking on
As this would change the error type, it would be a breaking change. However, if this was done correctly, a lot of the code where we currently use unwrap could just use “?”
FWIW, we're down to 6 unwrap and 3 expect calls outside the test suite.
Unwrap calls:
- parsing description bits as uid and gid (2 calls)
- parsing permission bits from ascii hex
- assuming utf8 for description and security descriptions (2 calls)
- construction of a
CStringfrom a&str
Expect calls:
- the kernel returning a key serial outside of the i32 range
- the kernel handing us back a key serial of 0
- a size result is outside of
usizerange