Merge branch 'ptr-offset-by-literal-lint' into 'master'

add ptr_offset_by_literal clippy lint and set to deny

See merge request redox-os/relibc!1527
This commit is contained in:
Jeremy Soller
2026-07-06 06:04:43 -06:00
15 changed files with 51 additions and 50 deletions
+1
View File
@@ -36,6 +36,7 @@ mut_from_ref = "deny"
precedence = "deny"
ptr_as_ptr = "warn" # TODO review occurrences
ptr_cast_constness = "warn" # TODO review occurrences
ptr_offset_by_literal = "deny"
ref_as_ptr = "warn" # TODO review occurrences
upper_case_acronyms = "allow" # TODO review occurrences
zero_ptr = "deny" # must allow on public constants due to cbindgen issue
+8 -8
View File
@@ -61,7 +61,7 @@ unsafe fn tokenize(mut pattern: *const u8, flags: c_int) -> Tree {
leading = false;
let c = unsafe { *pattern };
pattern = unsafe { pattern.offset(1) };
pattern = unsafe { pattern.add(1) };
match (c == b'*', need_collapsing) {
(true, true) => continue,
@@ -76,7 +76,7 @@ unsafe fn tokenize(mut pattern: *const u8, flags: c_int) -> Tree {
// Trailing backslash. Maybe error here?
break;
}
pattern = unsafe { pattern.offset(1) };
pattern = unsafe { pattern.add(1) };
leading = is_leading(flags, c);
(Token::Char(c), ONCE)
}
@@ -85,7 +85,7 @@ unsafe fn tokenize(mut pattern: *const u8, flags: c_int) -> Tree {
b'[' => {
let mut list: Vec<Collation> = Vec::new();
let invert = if unsafe { *pattern == b'!' } {
pattern = unsafe { pattern.offset(1) };
pattern = unsafe { pattern.add(1) };
true
} else {
false
@@ -96,12 +96,12 @@ unsafe fn tokenize(mut pattern: *const u8, flags: c_int) -> Tree {
if c == 0 {
break;
}
pattern = unsafe { pattern.offset(1) };
pattern = unsafe { pattern.add(1) };
match c {
b']' => break,
b'\\' => {
c = unsafe { *pattern };
pattern = unsafe { pattern.offset(1) };
pattern = unsafe { pattern.add(1) };
if c == 0 {
// Trailing backslash. Maybe error?
break;
@@ -109,9 +109,9 @@ unsafe fn tokenize(mut pattern: *const u8, flags: c_int) -> Tree {
}
_ => (),
}
if unsafe { *pattern == b'-' && *pattern.offset(1) != 0 } {
let end = unsafe { *pattern.offset(1) };
pattern = unsafe { pattern.offset(2) };
if unsafe { *pattern == b'-' && *pattern.add(1) != 0 } {
let end = unsafe { *pattern.add(1) };
pattern = unsafe { pattern.add(2) };
for c in c..=end {
if can_push(was_leading, flags, c) {
list.push(Collation::Char(c));
+6 -6
View File
@@ -63,7 +63,7 @@ pub unsafe extern "C" fn getopt_long(
if unsafe {
current_arg.is_null()
|| *current_arg != ByteLiteral::cast_cchar(b'-')
|| *current_arg.offset(1) == 0
|| *current_arg.add(1) == 0
} {
-1
} else if unsafe { string::strcmp(current_arg, c"--".as_ptr()) == 0 } {
@@ -73,10 +73,10 @@ pub unsafe extern "C" fn getopt_long(
-1
} else {
// remove the '-'
let current_arg = unsafe { current_arg.offset(1) };
let current_arg = unsafe { current_arg.add(1) };
if unsafe { *current_arg == ByteLiteral::cast_cchar(b'-') } && !longopts.is_null() {
let current_arg = unsafe { current_arg.offset(1) };
let current_arg = unsafe { current_arg.add(1) };
// is a long option
for i in 0.. {
let opt = unsafe { &*longopts.offset(i) };
@@ -156,7 +156,7 @@ unsafe fn parse_arg(
optstring: *const c_char,
) -> c_int {
let update_current_opt = || unsafe {
CURRENT_OPT = current_arg.offset(1);
CURRENT_OPT = current_arg.add(1);
if *CURRENT_OPT == 0 {
optind += 1;
}
@@ -178,7 +178,7 @@ unsafe fn parse_arg(
}
Some(GetoptOption::OptArg) => unsafe {
CURRENT_OPT = c"".as_ptr().cast_mut();
if *current_arg.offset(1) == 0 {
if *current_arg.add(1) == 0 {
optind += 2;
if optind > argc {
CURRENT_OPT = ptr::null_mut();
@@ -200,7 +200,7 @@ unsafe fn parse_arg(
c_int::from(*current_arg)
}
} else {
optarg = current_arg.offset(1);
optarg = current_arg.add(1);
optind += 1;
c_int::from(*current_arg)
+3 -3
View File
@@ -269,7 +269,7 @@ pub unsafe extern "C" fn gethostbyaddr(
unsafe { sethostent(HOST_STAYOPEN) };
return p;
}
cp = unsafe { cp.offset(1) };
cp = unsafe { cp.add(1) };
}
}
@@ -376,7 +376,7 @@ pub unsafe extern "C" fn gethostbyname(name: *const c_char) -> *mut hostent {
unsafe { sethostent(HOST_STAYOPEN) };
return p;
}
cp = unsafe { cp.offset(1) };
cp = unsafe { cp.add(1) };
}
}
@@ -542,7 +542,7 @@ pub unsafe extern "C" fn getprotobyname(name: *const c_char) -> *mut protoent {
unsafe { setprotoent(PROTO_STAYOPEN) };
return p;
}
cp = unsafe { cp.offset(1) };
cp = unsafe { cp.add(1) };
}
}
unsafe { setprotoent(PROTO_STAYOPEN) };
+1 -1
View File
@@ -478,7 +478,7 @@ pub unsafe extern "C" fn fgets(
let unget_read_size = cmp::min(left, stream.unget.len());
for _ in 0..unget_read_size {
unsafe { *out = stream.unget.pop().unwrap() as c_char };
out = unsafe { out.offset(1) };
out = unsafe { out.add(1) };
}
left -= unget_read_size;
}
+1 -1
View File
@@ -471,7 +471,7 @@ pub unsafe fn inner_scanf<T: Kind>(
{
if let Some(ref mut ptr) = ptr {
unsafe { **ptr = character as c_char };
*ptr = unsafe { ptr.offset(1) };
*ptr = unsafe { ptr.add(1) };
data_stored = true;
}
// Decrease the width, and read a new character unless the width is 0
+8 -8
View File
@@ -372,7 +372,7 @@ pub unsafe extern "C" fn exit(status: c_int) -> ! {
let mut f = core::ptr::from_ref(unsafe { &__fini_array_end });
#[allow(clippy::op_ref)]
while f > &raw const __fini_array_start {
f = unsafe { f.offset(-1) };
f = unsafe { f.sub(1) };
(unsafe { *f })();
}
@@ -538,7 +538,7 @@ pub unsafe extern "C" fn initstate(seed: c_uint, state: *mut c_char, size: size_
_ => 63,
};
random_state.x_ptr = unsafe { (state.cast::<[u8; 4]>()).offset(1) };
random_state.x_ptr = unsafe { (state.cast::<[u8; 4]>()).add(1) };
unsafe { random_state.seed(seed) };
unsafe { random_state.save() };
@@ -1378,7 +1378,7 @@ pub unsafe fn detect_base(s: *const c_char) -> Option<(c_int, isize)> {
match first {
0 => None,
b'0' => {
let second = unsafe { *s.offset(1) } as u8;
let second = unsafe { *s.add(1) } as u8;
if second == b'X' || second == b'x' {
Some((16, 2))
} else if (b'0'..=b'7').contains(&second) {
@@ -1394,7 +1394,7 @@ pub unsafe fn detect_base(s: *const c_char) -> Option<(c_int, isize)> {
pub unsafe fn convert_octal(s: *const c_char) -> Option<(c_ulong, isize, bool)> {
if unsafe { *s } != 0 && unsafe { *s } == ByteLiteral::cast_cchar(b'0') {
if let Some((val, idx, overflow)) = unsafe { convert_integer(s.offset(1), 8) } {
if let Some((val, idx, overflow)) = unsafe { convert_integer(s.add(1), 8) } {
Some((val, idx + 1, overflow))
} else {
// in case the prefix is not actually a prefix
@@ -1407,11 +1407,11 @@ pub unsafe fn convert_octal(s: *const c_char) -> Option<(c_ulong, isize, bool)>
pub unsafe fn convert_hex(s: *const c_char) -> Option<(c_ulong, isize, bool)> {
if (unsafe { *s } != 0 && unsafe { *s } == ByteLiteral::cast_cchar(b'0'))
&& (unsafe { *s.offset(1) } != 0
&& (unsafe { *s.offset(1) } == ByteLiteral::cast_cchar(b'x')
|| unsafe { *s.offset(1) } == ByteLiteral::cast_cchar(b'X')))
&& (unsafe { *s.add(1) } != 0
&& (unsafe { *s.add(1) } == ByteLiteral::cast_cchar(b'x')
|| unsafe { *s.add(1) } == ByteLiteral::cast_cchar(b'X')))
{
unsafe { convert_integer(s.offset(2), 16) }
unsafe { convert_integer(s.add(2), 16) }
.map(|(val, idx, overflow)| (val, idx + 2, overflow))
} else {
unsafe { convert_integer(s, 16) }
+3 -3
View File
@@ -44,13 +44,13 @@ impl State {
let stash_value: u32 =
(u32::from(self.n) << 16) | (u32::from(self.i) << 8) | u32::from(self.j);
unsafe { *self.x_ptr.offset(-1) = stash_value.to_ne_bytes() };
unsafe { self.x_ptr.offset(-1) }
unsafe { *self.x_ptr.sub(1) = stash_value.to_ne_bytes() };
unsafe { self.x_ptr.sub(1) }
}
pub unsafe fn load(&mut self, state_ptr: *mut [u8; 4]) {
let stash_value = u32::from_ne_bytes(unsafe { *state_ptr });
self.x_ptr = unsafe { state_ptr.offset(1) };
self.x_ptr = unsafe { state_ptr.add(1) };
/* This calculation of n does not have a bit mask in the musl
* original, in principle resulting in a u16, but obtaining a value
+7 -7
View File
@@ -82,8 +82,8 @@ pub unsafe extern "C" fn memcmp(s1: *const c_void, s2: *const c_void, n: size_t)
}
unreachable!()
}
a = unsafe { a.offset(1) };
b = unsafe { b.offset(1) };
a = unsafe { a.add(1) };
b = unsafe { b.add(1) };
}
let mut a = a.cast::<u8>();
@@ -92,8 +92,8 @@ pub unsafe extern "C" fn memcmp(s1: *const c_void, s2: *const c_void, n: size_t)
if unsafe { *a } != unsafe { *b } {
return c_int::from(unsafe { *a }) - c_int::from(unsafe { *b });
}
a = unsafe { a.offset(1) };
b = unsafe { b.offset(1) };
a = unsafe { a.add(1) };
b = unsafe { b.add(1) };
}
0
}
@@ -354,7 +354,7 @@ pub unsafe fn inner_strspn(s1: *const c_char, s2: *const c_char, cmp: bool) -> s
while unsafe { *s2 } != 0 {
set.insert(unsafe { *s2 } as usize);
s2 = unsafe { s2.offset(1) };
s2 = unsafe { s2.add(1) };
}
let mut i = 0;
@@ -363,7 +363,7 @@ pub unsafe fn inner_strspn(s1: *const c_char, s2: *const c_char, cmp: bool) -> s
break;
}
i += 1;
s1 = unsafe { s1.offset(1) };
s1 = unsafe { s1.add(1) };
}
i
}
@@ -638,7 +638,7 @@ unsafe fn inner_strstr(
i += 1;
}
haystack = unsafe { haystack.offset(1) };
haystack = unsafe { haystack.add(1) };
}
ptr::null_mut()
}
+4 -4
View File
@@ -127,12 +127,12 @@ pub unsafe extern "C" fn utimes(path: *const c_char, times: *const timeval) -> c
} else {
Some([
timespec {
tv_sec: unsafe { (*times.offset(0)).tv_sec },
tv_nsec: c_long::from(unsafe { (*times.offset(0)).tv_usec }) * 1000,
tv_sec: unsafe { (*times).tv_sec },
tv_nsec: c_long::from(unsafe { (*times).tv_usec }) * 1000,
},
timespec {
tv_sec: unsafe { (*times.offset(1)).tv_sec },
tv_nsec: c_long::from(unsafe { (*times.offset(1)).tv_usec }) * 1000,
tv_sec: unsafe { (*times.add(1)).tv_sec },
tv_nsec: c_long::from(unsafe { (*times.add(1)).tv_usec }) * 1000,
},
])
};
+4 -4
View File
@@ -89,17 +89,17 @@ pub unsafe fn strftime<W: WriteByte>(w: &mut W, format: *const c_char, t: *const
// If the character isn't '%', just copy it out.
if unsafe { *format } as u8 != b'%' {
w!(byte unsafe { *format } as u8);
format = unsafe { format.offset(1) };
format = unsafe { format.add(1) };
continue;
}
// Skip '%'
format = unsafe { format.offset(1) };
format = unsafe { format.add(1) };
// POSIX says '%E' and '%O' can modify numeric formats for locales,
// but we ignore them in this minimal "C" locale approach.
if unsafe { *format } as u8 == b'E' || unsafe { *format } as u8 == b'O' {
format = unsafe { format.offset(1) };
format = unsafe { format.add(1) };
}
match unsafe { *format } as u8 {
@@ -270,7 +270,7 @@ pub unsafe fn strftime<W: WriteByte>(w: &mut W, format: *const c_char, t: *const
}
// Move past the format specifier
format = unsafe { format.offset(1) };
format = unsafe { format.add(1) };
}
true
}
+1 -1
View File
@@ -604,7 +604,7 @@ pub unsafe extern "C" fn gethostname(mut name: *mut c_char, mut len: size_t) ->
break;
}
name = unsafe { name.offset(1) };
name = unsafe { name.add(1) };
}
0
}
+1 -1
View File
@@ -650,7 +650,7 @@ pub unsafe extern "C" fn wcsnlen(mut s: *const wchar_t, maxlen: size_t) -> size_
}
len += 1;
s = unsafe { s.offset(1) };
s = unsafe { s.add(1) };
}
len
+1 -1
View File
@@ -230,7 +230,7 @@ impl Read for UnsafeStringReader {
}
*inner = *self.0;
self.0 = self.0.offset(1);
self.0 = self.0.add(1);
}
Ok(buf.len())
}
+2 -2
View File
@@ -257,7 +257,7 @@ pub unsafe extern "C" fn relibc_start_v1(
#[allow(clippy::op_ref)]
while f < &raw const __preinit_array_end {
(unsafe { *f })();
f = unsafe { f.offset(1) };
f = unsafe { f.add(1) };
}
}
@@ -267,7 +267,7 @@ pub unsafe extern "C" fn relibc_start_v1(
#[allow(clippy::op_ref)]
while f < &raw const __init_array_end {
(unsafe { *f })();
f = unsafe { f.offset(1) };
f = unsafe { f.add(1) };
}
}