Skip to content

Commit 701d461

Browse files
authored
Small clean-ups (including fix for UB) (tikv#616)
1 parent ac4ffa3 commit 701d461

File tree

5 files changed

+105
-87
lines changed

5 files changed

+105
-87
lines changed

src/backup.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use crate::{ffi, ffi_util::to_cpath, Error, DB};
1717

18+
use libc::{c_int, c_uchar};
1819
use std::path::Path;
1920

2021
/// Represents information of a backup including timestamp of the backup
@@ -82,7 +83,7 @@ impl BackupEngine {
8283
ffi_try!(ffi::rocksdb_backup_engine_create_new_backup_flush(
8384
self.inner,
8485
db.inner,
85-
u8::from(flush_before_backup),
86+
c_uchar::from(flush_before_backup),
8687
));
8788
Ok(())
8889
}
@@ -219,7 +220,10 @@ impl BackupEngineOptions {
219220
impl RestoreOptions {
220221
pub fn set_keep_log_files(&mut self, keep_log_files: bool) {
221222
unsafe {
222-
ffi::rocksdb_restore_options_set_keep_log_files(self.inner, i32::from(keep_log_files));
223+
ffi::rocksdb_restore_options_set_keep_log_files(
224+
self.inner,
225+
c_int::from(keep_log_files),
226+
);
223227
}
224228
}
225229
}

src/db.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
WriteBatch, WriteOptions, DEFAULT_COLUMN_FAMILY_NAME,
2727
};
2828

29-
use libc::{self, c_char, c_int, c_void, size_t};
29+
use libc::{self, c_char, c_int, c_uchar, c_void, size_t};
3030
use std::collections::BTreeMap;
3131
use std::ffi::{CStr, CString};
3232
use std::fmt;
@@ -638,22 +638,22 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
638638
error_if_log_file_exist,
639639
} => ffi_try!(ffi::rocksdb_open_for_read_only(
640640
opts.inner,
641-
cpath.as_ptr() as *const _,
642-
u8::from(error_if_log_file_exist),
641+
cpath.as_ptr(),
642+
c_uchar::from(error_if_log_file_exist),
643643
)),
644644
AccessType::ReadWrite => {
645-
ffi_try!(ffi::rocksdb_open(opts.inner, cpath.as_ptr() as *const _))
645+
ffi_try!(ffi::rocksdb_open(opts.inner, cpath.as_ptr()))
646646
}
647647
AccessType::Secondary { secondary_path } => {
648648
ffi_try!(ffi::rocksdb_open_as_secondary(
649649
opts.inner,
650-
cpath.as_ptr() as *const _,
651-
to_cpath(secondary_path)?.as_ptr() as *const _,
650+
cpath.as_ptr(),
651+
to_cpath(secondary_path)?.as_ptr(),
652652
))
653653
}
654654
AccessType::WithTTL { ttl } => ffi_try!(ffi::rocksdb_open_with_ttl(
655655
opts.inner,
656-
cpath.as_ptr() as *const _,
656+
cpath.as_ptr(),
657657
ttl.as_secs() as c_int,
658658
)),
659659
}
@@ -682,7 +682,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
682682
cfnames.as_ptr(),
683683
cfopts.as_ptr(),
684684
cfhandles.as_mut_ptr(),
685-
u8::from(error_if_log_file_exist),
685+
c_uchar::from(error_if_log_file_exist),
686686
)),
687687
AccessType::ReadWrite => ffi_try!(ffi::rocksdb_open_column_families(
688688
opts.inner,
@@ -695,23 +695,24 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
695695
AccessType::Secondary { secondary_path } => {
696696
ffi_try!(ffi::rocksdb_open_as_secondary_column_families(
697697
opts.inner,
698-
cpath.as_ptr() as *const _,
699-
to_cpath(secondary_path)?.as_ptr() as *const _,
698+
cpath.as_ptr(),
699+
to_cpath(secondary_path)?.as_ptr(),
700700
cfs_v.len() as c_int,
701701
cfnames.as_ptr(),
702702
cfopts.as_ptr(),
703703
cfhandles.as_mut_ptr(),
704704
))
705705
}
706706
AccessType::WithTTL { ttl } => {
707+
let ttls_v = vec![ttl.as_secs() as c_int; cfs_v.len()];
707708
ffi_try!(ffi::rocksdb_open_column_families_with_ttl(
708709
opts.inner,
709710
cpath.as_ptr(),
710711
cfs_v.len() as c_int,
711712
cfnames.as_ptr(),
712713
cfopts.as_ptr(),
713714
cfhandles.as_mut_ptr(),
714-
&(ttl.as_secs() as c_int) as *const _,
715+
ttls_v.as_ptr(),
715716
))
716717
}
717718
}
@@ -726,7 +727,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
726727
unsafe {
727728
let ptr = ffi_try!(ffi::rocksdb_list_column_families(
728729
opts.inner,
729-
cpath.as_ptr() as *const _,
730+
cpath.as_ptr(),
730731
&mut length,
731732
));
732733

@@ -763,7 +764,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
763764
/// the data to disk.
764765
pub fn flush_wal(&self, sync: bool) -> Result<(), Error> {
765766
unsafe {
766-
ffi_try!(ffi::rocksdb_flush_wal(self.inner, u8::from(sync)));
767+
ffi_try!(ffi::rocksdb_flush_wal(self.inner, c_uchar::from(sync)));
767768
}
768769
Ok(())
769770
}
@@ -1975,7 +1976,7 @@ impl<T: ThreadMode> DBWithThreadMode<T> {
19751976
/// Request stopping background work, if wait is true wait until it's done.
19761977
pub fn cancel_all_background_work(&self, wait: bool) {
19771978
unsafe {
1978-
ffi::rocksdb_cancel_all_background_work(self.inner, u8::from(wait));
1979+
ffi::rocksdb_cancel_all_background_work(self.inner, c_uchar::from(wait));
19791980
}
19801981
}
19811982

0 commit comments

Comments
 (0)