Skip to content

Commit 121726a

Browse files
authored
Merge pull request #232 from muzarski/unsafe-op-in-unsafe-fn
cargo: enable unsafe-op-in-unsafe-fn compiler lint
2 parents 283fd5f + ece2ee4 commit 121726a

22 files changed

+566
-463
lines changed

scylla-rust-wrapper/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ opt-level = 3
5454
codegen-units = 1
5555
debug = 2
5656
strip = "none"
57+
58+
[lints.rust]
59+
unsafe-op-in-unsafe-fn = "warn"

scylla-rust-wrapper/src/argconv.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ use std::ptr::NonNull;
77
use std::sync::{Arc, Weak};
88

99
pub unsafe fn ptr_to_cstr(ptr: *const c_char) -> Option<&'static str> {
10-
CStr::from_ptr(ptr).to_str().ok()
10+
unsafe { CStr::from_ptr(ptr) }.to_str().ok()
1111
}
1212

1313
pub unsafe fn ptr_to_cstr_n(ptr: *const c_char, size: size_t) -> Option<&'static str> {
1414
if ptr.is_null() {
1515
return None;
1616
}
17-
std::str::from_utf8(std::slice::from_raw_parts(ptr as *const u8, size as usize)).ok()
17+
std::str::from_utf8(unsafe { std::slice::from_raw_parts(ptr as *const u8, size as usize) }).ok()
1818
}
1919

2020
pub unsafe fn arr_to_cstr<const N: usize>(arr: &[c_char]) -> Option<&'static str> {
2121
let null_char = '\0' as c_char;
2222
let end_index = arr[..N].iter().position(|c| c == &null_char).unwrap_or(N);
23-
ptr_to_cstr_n(arr.as_ptr(), end_index as size_t)
23+
unsafe { ptr_to_cstr_n(arr.as_ptr(), end_index as size_t) }
2424
}
2525

2626
pub fn str_to_arr<const N: usize>(s: &str) -> [c_char; N] {
@@ -41,15 +41,17 @@ pub fn str_to_arr<const N: usize>(s: &str) -> [c_char; N] {
4141
}
4242

4343
pub unsafe fn write_str_to_c(s: &str, c_str: *mut *const c_char, c_strlen: *mut size_t) {
44-
*c_str = s.as_ptr() as *const c_char;
45-
*c_strlen = s.len() as u64;
44+
unsafe {
45+
*c_str = s.as_ptr() as *const c_char;
46+
*c_strlen = s.len() as u64;
47+
}
4648
}
4749

4850
pub unsafe fn strlen(ptr: *const c_char) -> size_t {
4951
if ptr.is_null() {
5052
return 0;
5153
}
52-
libc::strlen(ptr) as size_t
54+
unsafe { libc::strlen(ptr) as size_t }
5355
}
5456

5557
#[cfg(test)]

scylla-rust-wrapper/src/binding.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ macro_rules! make_name_binder {
8080
// For some reason detected as unused, which is not true
8181
#[allow(unused_imports)]
8282
use crate::value::CassCqlValue::*;
83-
let name = ptr_to_cstr(name).unwrap();
83+
let name = unsafe { ptr_to_cstr(name) }.unwrap();
8484
match ($e)($($arg), *) {
8585
Ok(v) => $consume_v(BoxFFI::as_mut_ref(this).unwrap(), name, v),
8686
Err(e) => e,
@@ -102,7 +102,7 @@ macro_rules! make_name_n_binder {
102102
// For some reason detected as unused, which is not true
103103
#[allow(unused_imports)]
104104
use crate::value::CassCqlValue::*;
105-
let name = ptr_to_cstr_n(name, name_length).unwrap();
105+
let name = unsafe { ptr_to_cstr_n(name, name_length) }.unwrap();
106106
match ($e)($($arg), *) {
107107
Ok(v) => $consume_v(BoxFFI::as_mut_ref(this).unwrap(), name, v),
108108
Err(e) => e,
@@ -217,7 +217,7 @@ macro_rules! invoke_binder_maker_macro_with_type {
217217
$this,
218218
$consume_v,
219219
$fn,
220-
|v| Ok(Some(Text(ptr_to_cstr(v).unwrap().to_string()))),
220+
|v| Ok(Some(Text(unsafe { ptr_to_cstr(v) }.unwrap().to_string()))),
221221
[v @ *const std::os::raw::c_char]
222222
);
223223
};
@@ -226,7 +226,7 @@ macro_rules! invoke_binder_maker_macro_with_type {
226226
$this,
227227
$consume_v,
228228
$fn,
229-
|v, n| Ok(Some(Text(ptr_to_cstr_n(v, n).unwrap().to_string()))),
229+
|v, n| Ok(Some(Text(unsafe { ptr_to_cstr_n(v, n) }.unwrap().to_string()))),
230230
[v @ *const std::os::raw::c_char, n @ size_t]
231231
);
232232
};
@@ -236,7 +236,7 @@ macro_rules! invoke_binder_maker_macro_with_type {
236236
$consume_v,
237237
$fn,
238238
|v, v_size| {
239-
let v_vec = std::slice::from_raw_parts(v, v_size as usize).to_vec();
239+
let v_vec = unsafe { std::slice::from_raw_parts(v, v_size as usize) }.to_vec();
240240
Ok(Some(Blob(v_vec)))
241241
},
242242
[v @ *const cass_byte_t, v_size @ size_t]
@@ -291,7 +291,7 @@ macro_rules! invoke_binder_maker_macro_with_type {
291291
$fn,
292292
|v, v_size, scale| {
293293
use scylla::value::CqlDecimal;
294-
let varint = std::slice::from_raw_parts(v, v_size as usize);
294+
let varint = unsafe { std::slice::from_raw_parts(v, v_size as usize) };
295295
Ok(Some(Decimal(CqlDecimal::from_signed_be_bytes_slice_and_exponent(varint, scale))))
296296
},
297297
[v @ *const cass_byte_t, v_size @ size_t, scale @ cass_int32_t]

scylla-rust-wrapper/src/cass_types.rs

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ unsafe impl Sync for CassDataType {}
249249

250250
impl CassDataType {
251251
pub unsafe fn get_unchecked(&self) -> &CassDataTypeInner {
252-
&*self.0.get()
252+
unsafe { &*self.0.get() }
253253
}
254254

255255
#[allow(clippy::mut_from_ref)]
256256
pub unsafe fn get_mut_unchecked(&self) -> &mut CassDataTypeInner {
257-
&mut *self.0.get()
257+
unsafe { &mut *self.0.get() }
258258
}
259259

260260
pub const fn new(inner: CassDataTypeInner) -> CassDataType {
@@ -479,7 +479,9 @@ pub unsafe extern "C" fn cass_data_type_new_from_existing(
479479
data_type: CassBorrowedSharedPtr<CassDataType, CConst>,
480480
) -> CassOwnedSharedPtr<CassDataType, CMut> {
481481
let data_type = ArcFFI::as_ref(data_type).unwrap();
482-
ArcFFI::into_ptr(CassDataType::new_arced(data_type.get_unchecked().clone()))
482+
ArcFFI::into_ptr(CassDataType::new_arced(
483+
unsafe { data_type.get_unchecked() }.clone(),
484+
))
483485
}
484486

485487
#[no_mangle]
@@ -510,15 +512,15 @@ pub unsafe extern "C" fn cass_data_type_type(
510512
data_type: CassBorrowedSharedPtr<CassDataType, CConst>,
511513
) -> CassValueType {
512514
let data_type = ArcFFI::as_ref(data_type).unwrap();
513-
data_type.get_unchecked().get_value_type()
515+
unsafe { data_type.get_unchecked() }.get_value_type()
514516
}
515517

516518
#[no_mangle]
517519
pub unsafe extern "C" fn cass_data_type_is_frozen(
518520
data_type: CassBorrowedSharedPtr<CassDataType, CConst>,
519521
) -> cass_bool_t {
520522
let data_type = ArcFFI::as_ref(data_type).unwrap();
521-
let is_frozen = match data_type.get_unchecked() {
523+
let is_frozen = match unsafe { data_type.get_unchecked() } {
522524
CassDataTypeInner::UDT(udt) => udt.frozen,
523525
CassDataTypeInner::List { frozen, .. } => *frozen,
524526
CassDataTypeInner::Set { frozen, .. } => *frozen,
@@ -536,9 +538,9 @@ pub unsafe extern "C" fn cass_data_type_type_name(
536538
type_name_length: *mut size_t,
537539
) -> CassError {
538540
let data_type = ArcFFI::as_ref(data_type).unwrap();
539-
match data_type.get_unchecked() {
541+
match unsafe { data_type.get_unchecked() } {
540542
CassDataTypeInner::UDT(UDTDataType { name, .. }) => {
541-
write_str_to_c(name, type_name, type_name_length);
543+
unsafe { write_str_to_c(name, type_name, type_name_length) };
542544
CassError::CASS_OK
543545
}
544546
_ => CassError::CASS_ERROR_LIB_INVALID_VALUE_TYPE,
@@ -550,7 +552,7 @@ pub unsafe extern "C" fn cass_data_type_set_type_name(
550552
data_type: CassBorrowedSharedPtr<CassDataType, CMut>,
551553
type_name: *const c_char,
552554
) -> CassError {
553-
cass_data_type_set_type_name_n(data_type, type_name, strlen(type_name))
555+
unsafe { cass_data_type_set_type_name_n(data_type, type_name, strlen(type_name)) }
554556
}
555557

556558
#[no_mangle]
@@ -560,11 +562,11 @@ pub unsafe extern "C" fn cass_data_type_set_type_name_n(
560562
type_name_length: size_t,
561563
) -> CassError {
562564
let data_type = ArcFFI::as_ref(data_type_raw).unwrap();
563-
let type_name_string = ptr_to_cstr_n(type_name, type_name_length)
565+
let type_name_string = unsafe { ptr_to_cstr_n(type_name, type_name_length) }
564566
.unwrap()
565567
.to_string();
566568

567-
match data_type.get_mut_unchecked() {
569+
match unsafe { data_type.get_mut_unchecked() } {
568570
CassDataTypeInner::UDT(udt_data_type) => {
569571
udt_data_type.name = type_name_string;
570572
CassError::CASS_OK
@@ -580,9 +582,9 @@ pub unsafe extern "C" fn cass_data_type_keyspace(
580582
keyspace_length: *mut size_t,
581583
) -> CassError {
582584
let data_type = ArcFFI::as_ref(data_type).unwrap();
583-
match data_type.get_unchecked() {
585+
match unsafe { data_type.get_unchecked() } {
584586
CassDataTypeInner::UDT(UDTDataType { name, .. }) => {
585-
write_str_to_c(name, keyspace, keyspace_length);
587+
unsafe { write_str_to_c(name, keyspace, keyspace_length) };
586588
CassError::CASS_OK
587589
}
588590
_ => CassError::CASS_ERROR_LIB_INVALID_VALUE_TYPE,
@@ -594,7 +596,7 @@ pub unsafe extern "C" fn cass_data_type_set_keyspace(
594596
data_type: CassBorrowedSharedPtr<CassDataType, CMut>,
595597
keyspace: *const c_char,
596598
) -> CassError {
597-
cass_data_type_set_keyspace_n(data_type, keyspace, strlen(keyspace))
599+
unsafe { cass_data_type_set_keyspace_n(data_type, keyspace, strlen(keyspace)) }
598600
}
599601

600602
#[no_mangle]
@@ -604,11 +606,11 @@ pub unsafe extern "C" fn cass_data_type_set_keyspace_n(
604606
keyspace_length: size_t,
605607
) -> CassError {
606608
let data_type = ArcFFI::as_ref(data_type).unwrap();
607-
let keyspace_string = ptr_to_cstr_n(keyspace, keyspace_length)
609+
let keyspace_string = unsafe { ptr_to_cstr_n(keyspace, keyspace_length) }
608610
.unwrap()
609611
.to_string();
610612

611-
match data_type.get_mut_unchecked() {
613+
match unsafe { data_type.get_mut_unchecked() } {
612614
CassDataTypeInner::UDT(udt_data_type) => {
613615
udt_data_type.keyspace = keyspace_string;
614616
CassError::CASS_OK
@@ -624,9 +626,9 @@ pub unsafe extern "C" fn cass_data_type_class_name(
624626
class_name_length: *mut size_t,
625627
) -> CassError {
626628
let data_type = ArcFFI::as_ref(data_type).unwrap();
627-
match data_type.get_unchecked() {
629+
match unsafe { data_type.get_unchecked() } {
628630
CassDataTypeInner::Custom(name) => {
629-
write_str_to_c(name, class_name, class_name_length);
631+
unsafe { write_str_to_c(name, class_name, class_name_length) };
630632
CassError::CASS_OK
631633
}
632634
_ => CassError::CASS_ERROR_LIB_INVALID_VALUE_TYPE,
@@ -638,7 +640,7 @@ pub unsafe extern "C" fn cass_data_type_set_class_name(
638640
data_type: CassBorrowedSharedPtr<CassDataType, CMut>,
639641
class_name: *const ::std::os::raw::c_char,
640642
) -> CassError {
641-
cass_data_type_set_class_name_n(data_type, class_name, strlen(class_name))
643+
unsafe { cass_data_type_set_class_name_n(data_type, class_name, strlen(class_name)) }
642644
}
643645

644646
#[no_mangle]
@@ -648,10 +650,10 @@ pub unsafe extern "C" fn cass_data_type_set_class_name_n(
648650
class_name_length: size_t,
649651
) -> CassError {
650652
let data_type = ArcFFI::as_ref(data_type).unwrap();
651-
let class_string = ptr_to_cstr_n(class_name, class_name_length)
653+
let class_string = unsafe { ptr_to_cstr_n(class_name, class_name_length) }
652654
.unwrap()
653655
.to_string();
654-
match data_type.get_mut_unchecked() {
656+
match unsafe { data_type.get_mut_unchecked() } {
655657
CassDataTypeInner::Custom(name) => {
656658
*name = class_string;
657659
CassError::CASS_OK
@@ -665,7 +667,7 @@ pub unsafe extern "C" fn cass_data_type_sub_type_count(
665667
data_type: CassBorrowedSharedPtr<CassDataType, CConst>,
666668
) -> size_t {
667669
let data_type = ArcFFI::as_ref(data_type).unwrap();
668-
match data_type.get_unchecked() {
670+
match unsafe { data_type.get_unchecked() } {
669671
CassDataTypeInner::Value(..) => 0,
670672
CassDataTypeInner::UDT(udt_data_type) => udt_data_type.field_types.len() as size_t,
671673
CassDataTypeInner::List { typ, .. } | CassDataTypeInner::Set { typ, .. } => {
@@ -685,7 +687,7 @@ pub unsafe extern "C" fn cass_data_type_sub_type_count(
685687
pub unsafe extern "C" fn cass_data_sub_type_count(
686688
data_type: CassBorrowedSharedPtr<CassDataType, CConst>,
687689
) -> size_t {
688-
cass_data_type_sub_type_count(data_type)
690+
unsafe { cass_data_type_sub_type_count(data_type) }
689691
}
690692

691693
#[no_mangle]
@@ -695,7 +697,7 @@ pub unsafe extern "C" fn cass_data_type_sub_data_type(
695697
) -> CassBorrowedSharedPtr<CassDataType, CConst> {
696698
let data_type = ArcFFI::as_ref(data_type).unwrap();
697699
let sub_type: Option<&Arc<CassDataType>> =
698-
data_type.get_unchecked().get_sub_data_type(index as usize);
700+
unsafe { data_type.get_unchecked() }.get_sub_data_type(index as usize);
699701

700702
match sub_type {
701703
None => ArcFFI::null(),
@@ -709,7 +711,7 @@ pub unsafe extern "C" fn cass_data_type_sub_data_type_by_name(
709711
data_type: CassBorrowedSharedPtr<CassDataType, CConst>,
710712
name: *const ::std::os::raw::c_char,
711713
) -> CassBorrowedSharedPtr<CassDataType, CConst> {
712-
cass_data_type_sub_data_type_by_name_n(data_type, name, strlen(name))
714+
unsafe { cass_data_type_sub_data_type_by_name_n(data_type, name, strlen(name)) }
713715
}
714716

715717
#[no_mangle]
@@ -719,8 +721,8 @@ pub unsafe extern "C" fn cass_data_type_sub_data_type_by_name_n(
719721
name_length: size_t,
720722
) -> CassBorrowedSharedPtr<CassDataType, CConst> {
721723
let data_type = ArcFFI::as_ref(data_type).unwrap();
722-
let name_str = ptr_to_cstr_n(name, name_length).unwrap();
723-
match data_type.get_unchecked() {
724+
let name_str = unsafe { ptr_to_cstr_n(name, name_length) }.unwrap();
725+
match unsafe { data_type.get_unchecked() } {
724726
CassDataTypeInner::UDT(udt) => match udt.get_field_by_name(name_str) {
725727
None => ArcFFI::null(),
726728
Some(t) => ArcFFI::as_ptr(t),
@@ -737,11 +739,11 @@ pub unsafe extern "C" fn cass_data_type_sub_type_name(
737739
name_length: *mut size_t,
738740
) -> CassError {
739741
let data_type = ArcFFI::as_ref(data_type).unwrap();
740-
match data_type.get_unchecked() {
742+
match unsafe { data_type.get_unchecked() } {
741743
CassDataTypeInner::UDT(udt) => match udt.field_types.get(index as usize) {
742744
None => CassError::CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS,
743745
Some((field_name, _)) => {
744-
write_str_to_c(field_name, name, name_length);
746+
unsafe { write_str_to_c(field_name, name, name_length) };
745747
CassError::CASS_OK
746748
}
747749
},
@@ -755,8 +757,7 @@ pub unsafe extern "C" fn cass_data_type_add_sub_type(
755757
sub_data_type: CassBorrowedSharedPtr<CassDataType, CConst>,
756758
) -> CassError {
757759
let data_type = ArcFFI::as_ref(data_type).unwrap();
758-
match data_type
759-
.get_mut_unchecked()
760+
match unsafe { data_type.get_mut_unchecked() }
760761
.add_sub_data_type(ArcFFI::cloned_from_ptr(sub_data_type).unwrap())
761762
{
762763
Ok(()) => CassError::CASS_OK,
@@ -770,7 +771,7 @@ pub unsafe extern "C" fn cass_data_type_add_sub_type_by_name(
770771
name: *const c_char,
771772
sub_data_type: CassBorrowedSharedPtr<CassDataType, CConst>,
772773
) -> CassError {
773-
cass_data_type_add_sub_type_by_name_n(data_type, name, strlen(name), sub_data_type)
774+
unsafe { cass_data_type_add_sub_type_by_name_n(data_type, name, strlen(name), sub_data_type) }
774775
}
775776

776777
#[no_mangle]
@@ -780,11 +781,13 @@ pub unsafe extern "C" fn cass_data_type_add_sub_type_by_name_n(
780781
name_length: size_t,
781782
sub_data_type_raw: CassBorrowedSharedPtr<CassDataType, CConst>,
782783
) -> CassError {
783-
let name_string = ptr_to_cstr_n(name, name_length).unwrap().to_string();
784+
let name_string = unsafe { ptr_to_cstr_n(name, name_length) }
785+
.unwrap()
786+
.to_string();
784787
let sub_data_type = ArcFFI::cloned_from_ptr(sub_data_type_raw).unwrap();
785788

786789
let data_type = ArcFFI::as_ref(data_type_raw).unwrap();
787-
match data_type.get_mut_unchecked() {
790+
match unsafe { data_type.get_mut_unchecked() } {
788791
CassDataTypeInner::UDT(udt_data_type) => {
789792
// The Cpp Driver does not check whether field_types size
790793
// exceeded field_count.
@@ -801,7 +804,7 @@ pub unsafe extern "C" fn cass_data_type_add_sub_value_type(
801804
sub_value_type: CassValueType,
802805
) -> CassError {
803806
let sub_data_type = CassDataType::new_arced(CassDataTypeInner::Value(sub_value_type));
804-
cass_data_type_add_sub_type(data_type, ArcFFI::as_ptr(&sub_data_type))
807+
unsafe { cass_data_type_add_sub_type(data_type, ArcFFI::as_ptr(&sub_data_type)) }
805808
}
806809

807810
#[no_mangle]
@@ -811,7 +814,7 @@ pub unsafe extern "C" fn cass_data_type_add_sub_value_type_by_name(
811814
sub_value_type: CassValueType,
812815
) -> CassError {
813816
let sub_data_type = CassDataType::new_arced(CassDataTypeInner::Value(sub_value_type));
814-
cass_data_type_add_sub_type_by_name(data_type, name, ArcFFI::as_ptr(&sub_data_type))
817+
unsafe { cass_data_type_add_sub_type_by_name(data_type, name, ArcFFI::as_ptr(&sub_data_type)) }
815818
}
816819

817820
#[no_mangle]
@@ -822,12 +825,14 @@ pub unsafe extern "C" fn cass_data_type_add_sub_value_type_by_name_n(
822825
sub_value_type: CassValueType,
823826
) -> CassError {
824827
let sub_data_type = CassDataType::new_arced(CassDataTypeInner::Value(sub_value_type));
825-
cass_data_type_add_sub_type_by_name_n(
826-
data_type,
827-
name,
828-
name_length,
829-
ArcFFI::as_ptr(&sub_data_type),
830-
)
828+
unsafe {
829+
cass_data_type_add_sub_type_by_name_n(
830+
data_type,
831+
name,
832+
name_length,
833+
ArcFFI::as_ptr(&sub_data_type),
834+
)
835+
}
831836
}
832837

833838
impl TryFrom<CassConsistency> for Consistency {

0 commit comments

Comments
 (0)