Lind-Project / safeposix-rust

Rust implementation of SafePOSIX
Apache License 2.0
12 stars 10 forks source link

Unwrapping on an Err() causes a program to panic #263

Open ve1nard opened 4 months ago

ve1nard commented 4 months ago

Description

If a file descriptor provided to get_filedescriptor() is out of the allowed range: 0 to 1024(excluded), the method returns Err(), and unwrapping on the returned Err() value later on causes a program to panic.

Cases:

Any syscall, like fcntl or ioctl, that involves file descriptors needs to access a file descriptor table using get_filedescriptor(). The pattern is always the same and involves the following line:

let checkedfd = self.get_filedescriptor(fd).unwrap();

Why this behavior?

By definition, an unwrap method for the Result type should panic when provided with an Err() result.

How is this tested?

Unit tests calling fcntl syscall with out-of-range file descriptors, e.g. negative values or values greater than or equal to 1024, failed with called Result::unwrap() on an Err.

References

https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/error-handling.html#unwrapping-explained

Anway-Agte commented 3 months ago

Should a match condition be written to handle the Err() value before unwrapping it?