rust-keyutils icon indicating copy to clipboard operation
rust-keyutils copied to clipboard

Reduce uses of unwrap/expect

Open josephlr opened this issue 6 years ago • 1 comments

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 “?”

josephlr avatar Jun 27 '19 18:06 josephlr

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 CString from 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 usize range

mathstuf avatar May 13 '20 22:05 mathstuf