-
Notifications
You must be signed in to change notification settings - Fork 341
Fix risky unwrap(), expect(), and casting #1113
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
Changes from all commits
895005c
f69a4b5
7e6ef5a
3c3e186
3dceea4
c5d7c8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,7 +36,8 @@ impl SubscriberStateTable { | |
| } | ||
|
|
||
| pub fn read_data(&self, timeout: Duration, interrupt_on_signal: bool) -> Result<SelectResult> { | ||
| let timeout_ms = timeout.as_millis().try_into().unwrap(); | ||
| let timeout_ms: u32 = timeout.as_millis().try_into() | ||
| .map_err(|_| Exception::new("Invalid timeout value"))?; | ||
| let res = unsafe { | ||
| swss_try!(p_res => { | ||
| SWSSSubscriberStateTable_readData(self.ptr, timeout_ms, interrupt_on_signal as u8, p_res) | ||
|
|
@@ -50,7 +51,10 @@ impl SubscriberStateTable { | |
| // as long as the DbConnector does. | ||
| unsafe { | ||
| let fd = swss_try!(p_fd => SWSSSubscriberStateTable_getFd(self.ptr, p_fd))?; | ||
| let fd = BorrowedFd::borrow_raw(fd.try_into().unwrap()); | ||
| if fd == -1 { | ||
| return Err(Exception::new("Invalid file descriptor: -1")); | ||
| } | ||
| let fd = BorrowedFd::borrow_raw(fd); | ||
| Ok(fd) | ||
| } | ||
| } | ||
|
|
@@ -70,7 +74,11 @@ impl SubscriberStateTable { | |
|
|
||
| impl Drop for SubscriberStateTable { | ||
| fn drop(&mut self) { | ||
| unsafe { swss_try!(SWSSSubscriberStateTable_free(self.ptr)).expect("Dropping SubscriberStateTable") }; | ||
| unsafe { | ||
| if let Err(e) = swss_try!(SWSSSubscriberStateTable_free(self.ptr)) { | ||
| eprintln!("Error dropping SubscriberStateTable: {}", e); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does it mean for a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may be possible that a programmer mistake of throwing in dtor in this class or any base class or any member class. Crashing a critical daemon is not good for online service, so use eprintln as defensive programming programming.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running after an exception is thrown in dtor is not good either; the runtime state of the program is no longer known or consistent.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I plan to fix exception is thrown in dtor issue in another future PR.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The future PR is #1115 |
||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
borrow_raw document says "The resource pointed to by fd must remain open for the duration of the returned BorrowedFd, and it must not have the value -1.". We should check the value is not negative. #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checked for -1, not for negative. Since BorrowedFd and hiredis cares only about -1.