Merge pull request #604 from arthurprs/busytimeout_overflow

Fix i32 overflow in Connection::busy_timeout
This commit is contained in:
gwenn 2019-12-29 17:54:10 +01:00 committed by GitHub
commit 54163a4146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
///! Busy handler (when the database is locked) ///! Busy handler (when the database is locked)
use std::convert::TryInto;
use std::mem; use std::mem;
use std::os::raw::{c_int, c_void}; use std::os::raw::{c_int, c_void};
use std::panic::catch_unwind; use std::panic::catch_unwind;
@ -21,12 +22,13 @@ impl Connection {
/// (using `busy_handler`) prior to calling this routine, that other /// (using `busy_handler`) prior to calling this routine, that other
/// busy handler is cleared. /// busy handler is cleared.
pub fn busy_timeout(&self, timeout: Duration) -> Result<()> { pub fn busy_timeout(&self, timeout: Duration) -> Result<()> {
let ms = timeout let ms: i32 = timeout
.as_secs() .as_secs()
.checked_mul(1000) .checked_mul(1000)
.and_then(|t| t.checked_add(timeout.subsec_millis().into())) .and_then(|t| t.checked_add(timeout.subsec_millis().into()))
.and_then(|t| t.try_into().ok())
.expect("too big"); .expect("too big");
self.db.borrow_mut().busy_timeout(ms as i32) self.db.borrow_mut().busy_timeout(ms)
} }
/// Register a callback to handle `SQLITE_BUSY` errors. /// Register a callback to handle `SQLITE_BUSY` errors.