Skip to content

Commit 25f62a9

Browse files
committed
edits
1 parent 9fda615 commit 25f62a9

440 files changed

Lines changed: 504 additions & 882 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/format.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ jobs:
1111
steps:
1212
- name: Checkout code
1313
uses: actions/checkout@v6
14-
with:
15-
ref: ${{ github.head_ref || github.ref_name }}
1614

1715
- name: Setup LLVM 22
1816
uses: ZhongRuoyu/setup-llvm@v0
@@ -64,6 +62,6 @@ jobs:
6462
6563
- name: Check Rust lints
6664
run: |
67-
cargo clippy --manifest-path rules/Cargo.toml --all-targets -- -D clippy::correctness
68-
cargo +nightly clippy --manifest-path rule-preprocessor/Cargo.toml --all-targets -- -D clippy::correctness
69-
cargo clippy --manifest-path libcc2rs/Cargo.toml --all-targets -- -D clippy::correctness
65+
cargo clippy --manifest-path rules/Cargo.toml --all-targets --all-features -- -Dwarnings
66+
cargo +nightly clippy --manifest-path rule-preprocessor/Cargo.toml --all-targets --all-features -- -Dwarnings
67+
cargo clippy --manifest-path libcc2rs/Cargo.toml --all-targets --all-features -- -Dwarnings

cpp2rust/converter/converter.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,9 @@ use libc::*;
4848
extern crate libcc2rs;
4949
use libcc2rs::*;
5050
use std::collections::BTreeMap;
51-
use std::rc::Rc;
52-
use std::io::{Read, Write};
53-
use std::io::Seek;
51+
use std::io::{Read, Write, Seek};
5452
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
53+
use std::rc::Rc;
5554
)");
5655
}
5756

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ void ConverterRefCount::EmitFilePreamble() {
2323
StrCat(R"(
2424
extern crate libcc2rs;
2525
use libcc2rs::*;
26-
use std::collections::BTreeMap;
2726
use std::cell::RefCell;
28-
use std::io::{Read, Write};
29-
use std::rc::{Rc, Weak};
30-
use std::io::Seek;
31-
use std::os::fd::AsFd;
27+
use std::collections::BTreeMap;
28+
use std::io::{Read, Write, Seek};
3229
use std::io::prelude::*;
30+
use std::os::fd::AsFd;
31+
use std::rc::{Rc, Weak};
3332
)");
3433
}
3534

libcc2rs/src/compat.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ extern "C" {
1313
fn platform_malloc_size(ptr: *const c_void) -> usize;
1414
}
1515

16+
/// # Safety
17+
///
18+
/// The pointer `ptr` must be a pointer to a block of memory allocated by
19+
/// the appropriate allocator (e.g., `malloc`).
20+
// The memory must not have been deallocated.
1621
pub unsafe fn malloc_usable_size(ptr: *mut c_void) -> usize {
1722
#[cfg(target_os = "linux")]
1823
{

libcc2rs/src/dec.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ prefix_wrap_dec_impl!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
6666
prefix_nowrap_dec_impl!(f32, f64);
6767

6868
pub trait UnsafePostfixDec {
69+
/// # Safety
70+
/// This function decrements a pointer and returns the old value.
71+
/// The caller must ensure the pointer is valid and doesn't underflow.
6972
unsafe fn postfix_dec(&mut self) -> Self;
7073
}
7174

@@ -88,6 +91,9 @@ impl<T> UnsafePostfixDec for *mut T {
8891
}
8992

9093
pub trait UnsafePrefixDec {
94+
/// # Safety
95+
/// This function decrements a pointer and returns the new value.
96+
/// The caller must ensure the pointer is valid and doesn't underflow.
9197
unsafe fn prefix_dec(&mut self) -> Self;
9298
}
9399

libcc2rs/src/inc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ postfix_wrap_inc_impl!(i8, u8, i16, u16, i32, u32, i64, u64, isize, usize);
7777
postfix_nowrap_inc_impl!(f32, f64);
7878

7979
pub trait UnsafePostfixInc {
80+
/// # Safety
81+
/// This function increments a pointer and returns the old value.
82+
/// The caller must ensure the pointer is valid and doesn't overflow.
8083
unsafe fn postfix_inc(&mut self) -> Self;
8184
}
8285

@@ -99,6 +102,9 @@ impl<T> UnsafePostfixInc for *mut T {
99102
}
100103

101104
pub trait UnsafePrefixInc {
105+
/// # Safety
106+
/// This function increments a pointer and returns the new value.
107+
/// The caller must ensure the pointer is valid and doesn't overflow.
102108
unsafe fn prefix_inc(&mut self) -> Self;
103109
}
104110

libcc2rs/src/io.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,18 @@ pub fn cerr() -> Ptr<std::fs::File> {
4343
SAFE_STDERR.with(AsPointer::as_pointer)
4444
}
4545

46+
/// # Safety
47+
///
48+
/// The caller must ensure that the returned pointer is not used after the
49+
// thread finishes.
4650
pub unsafe fn cout_unsafe() -> *mut std::fs::File {
4751
UNSAFE_STDOUT.with(UnsafeCell::get)
4852
}
4953

54+
/// # Safety
55+
///
56+
/// The caller must ensure that the returned pointer is not used after the
57+
// thread finishes.
5058
pub unsafe fn cerr_unsafe() -> *mut std::fs::File {
5159
UNSAFE_STDERR.with(UnsafeCell::get)
5260
}

libcc2rs/src/rc.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,25 @@ impl<T> Ptr<T> {
272272
}
273273
}
274274

275+
#[inline]
276+
pub fn is_empty(&self) -> bool {
277+
match self.kind {
278+
PtrKind::Null => true,
279+
PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => false,
280+
PtrKind::Vec(ref weak) => weak
281+
.upgrade()
282+
.expect("ub: dangling pointer")
283+
.borrow()
284+
.is_empty(),
285+
PtrKind::StackArray(ref weak) | PtrKind::HeapArray(ref weak) => weak
286+
.upgrade()
287+
.expect("ub: dangling pointer")
288+
.borrow()
289+
.is_empty(),
290+
PtrKind::Reinterpreted(ref data) => self.offset >= data.total_byte_len(),
291+
}
292+
}
293+
275294
#[inline]
276295
pub fn offset(&self, offset: isize) -> Self {
277296
let step = self.elem_step();
@@ -660,7 +679,9 @@ impl<T> std::ops::AddAssign<u64> for Ptr<T> {
660679
#[inline]
661680
fn add_assign(&mut self, other: u64) {
662681
let step = self.elem_step();
663-
self.offset = self.offset.wrapping_add((other as usize) * step);
682+
self.offset = self
683+
.offset
684+
.wrapping_add((other as usize).wrapping_mul(step));
664685
}
665686
}
666687

@@ -670,23 +691,27 @@ impl<T> std::ops::AddAssign<i32> for Ptr<T> {
670691
let step = self.elem_step();
671692
self.offset = self
672693
.offset
673-
.wrapping_add(((other as isize) * step as isize) as usize);
694+
.wrapping_add(((other as isize).wrapping_mul(step as isize)) as usize);
674695
}
675696
}
676697

677698
impl<T> std::ops::AddAssign<u32> for Ptr<T> {
678699
#[inline]
679700
fn add_assign(&mut self, other: u32) {
680701
let step = self.elem_step();
681-
self.offset = self.offset.wrapping_add((other as usize) * step);
702+
self.offset = self
703+
.offset
704+
.wrapping_add((other as usize).wrapping_mul(step));
682705
}
683706
}
684707

685708
impl<T> std::ops::AddAssign<isize> for Ptr<T> {
686709
#[inline]
687710
fn add_assign(&mut self, other: isize) {
688711
let step = self.elem_step();
689-
self.offset = self.offset.wrapping_add((other * step as isize) as usize);
712+
self.offset = self
713+
.offset
714+
.wrapping_add((other.wrapping_mul(step as isize)) as usize);
690715
}
691716
}
692717

@@ -900,6 +925,7 @@ thread_local! {
900925
}
901926

902927
impl Ptr<u8> {
928+
#[allow(clippy::explicit_counter_loop)]
903929
pub fn memcpy(&self, src: &Self, len: usize) {
904930
let mut dst = self.clone();
905931
let mut i: usize = 0;
@@ -914,6 +940,7 @@ impl Ptr<u8> {
914940
assert_eq!(i, len, "ub: memcpy");
915941
}
916942

943+
#[allow(clippy::explicit_counter_loop)]
917944
pub fn memset(&self, value: u8, num: usize) {
918945
let mut dst = self.clone();
919946
for _ in 0..num {
@@ -922,6 +949,7 @@ impl Ptr<u8> {
922949
}
923950
}
924951

952+
#[allow(clippy::explicit_counter_loop)]
925953
pub fn memcmp(&self, other: &Self, len: usize) -> i32 {
926954
let mut a = self.clone();
927955
let mut b = other.clone();

libcc2rs/src/reinterpret.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ fn slice_read_bytes<S: ByteRepr>(slice: &[S], byte_offset: usize, buf: &mut [u8]
9999
// Only deserializes/reserializes the overlapping elements.
100100
fn slice_write_bytes<S: ByteRepr>(slice: &mut [S], byte_offset: usize, data: &[u8]) {
101101
let elem_size = std::mem::size_of::<S>();
102+
let mut elem_buf = vec![0u8; elem_size];
102103
let first_elem = byte_offset / elem_size;
103-
let last_elem = (byte_offset + data.len()).div_ceil(elem_size);
104-
for elem_idx in first_elem..last_elem {
104+
let num_elem = data.len().div_ceil(elem_size);
105+
for (elem_idx, elem) in slice.iter_mut().enumerate().skip(first_elem).take(num_elem) {
105106
let elem_byte_start = elem_idx * elem_size;
106-
let mut elem_buf = vec![0u8; elem_size];
107-
slice[elem_idx].to_bytes(&mut elem_buf);
107+
elem.to_bytes(&mut elem_buf);
108108
let overlap_start = byte_offset.max(elem_byte_start) - elem_byte_start;
109109
let overlap_end =
110110
(byte_offset + data.len()).min(elem_byte_start + elem_size) - elem_byte_start;
111111
let data_start = byte_offset.max(elem_byte_start) - byte_offset;
112112
elem_buf[overlap_start..overlap_end]
113113
.copy_from_slice(&data[data_start..data_start + (overlap_end - overlap_start)]);
114-
slice[elem_idx] = S::from_bytes(&elem_buf);
114+
*elem = S::from_bytes(&elem_buf);
115115
}
116116
}
117117

libcc2rs/src/va_args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
14
use std::ffi::c_void;
25

36
use crate::rc::AnyPtr;

0 commit comments

Comments
 (0)