diff --git a/Cargo.toml b/Cargo.toml index 038a9dbd4f..fc654db56f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/header/fnmatch/mod.rs b/src/header/fnmatch/mod.rs index 3deb1e8027..4d267d00a7 100644 --- a/src/header/fnmatch/mod.rs +++ b/src/header/fnmatch/mod.rs @@ -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 = 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)); diff --git a/src/header/getopt/mod.rs b/src/header/getopt/mod.rs index ecc8074b65..c91d4004fb 100644 --- a/src/header/getopt/mod.rs +++ b/src/header/getopt/mod.rs @@ -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) diff --git a/src/header/netdb/mod.rs b/src/header/netdb/mod.rs index e5fb4cb521..603db63bb4 100644 --- a/src/header/netdb/mod.rs +++ b/src/header/netdb/mod.rs @@ -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) }; diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index cdac484372..308d3d9159 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -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; } diff --git a/src/header/stdio/scanf.rs b/src/header/stdio/scanf.rs index 720f9d4fcf..f80cc506d7 100644 --- a/src/header/stdio/scanf.rs +++ b/src/header/stdio/scanf.rs @@ -471,7 +471,7 @@ pub unsafe fn inner_scanf( { 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 diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 7a29dae934..b61d066e39 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -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) } diff --git a/src/header/stdlib/random.rs b/src/header/stdlib/random.rs index 86aec6f3db..e81487a26b 100644 --- a/src/header/stdlib/random.rs +++ b/src/header/stdlib/random.rs @@ -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 diff --git a/src/header/string/mod.rs b/src/header/string/mod.rs index c5a2efa9e1..7316a978a0 100644 --- a/src/header/string/mod.rs +++ b/src/header/string/mod.rs @@ -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::(); @@ -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() } diff --git a/src/header/sys_time/mod.rs b/src/header/sys_time/mod.rs index b65a5c0ea7..a7b028e8b7 100644 --- a/src/header/sys_time/mod.rs +++ b/src/header/sys_time/mod.rs @@ -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, }, ]) }; diff --git a/src/header/time/strftime.rs b/src/header/time/strftime.rs index 90ba34af0f..0b8e6fe48c 100644 --- a/src/header/time/strftime.rs +++ b/src/header/time/strftime.rs @@ -89,17 +89,17 @@ pub unsafe fn strftime(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: &mut W, format: *const c_char, t: *const } // Move past the format specifier - format = unsafe { format.offset(1) }; + format = unsafe { format.add(1) }; } true } diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs index cc717d4278..55a755a751 100644 --- a/src/header/unistd/mod.rs +++ b/src/header/unistd/mod.rs @@ -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 } diff --git a/src/header/wchar/mod.rs b/src/header/wchar/mod.rs index a23ba14e2f..ffab6830c7 100644 --- a/src/header/wchar/mod.rs +++ b/src/header/wchar/mod.rs @@ -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 diff --git a/src/platform/mod.rs b/src/platform/mod.rs index 35190a808c..60d6993d08 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -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()) } diff --git a/src/start.rs b/src/start.rs index d6761fbdb9..41e0754881 100644 --- a/src/start.rs +++ b/src/start.rs @@ -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) }; } }