Skip to content

Commit 556be9b

Browse files
Change the memcmp and bcmp return type to c_int
Fix the return type of `memcmp` and `bcmp` builtin functions on targets with a `c_int` other than `i32`. Linked issue: rust-lang/rust#144076
1 parent 5d33f9d commit 556be9b

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

compiler-builtins/src/mem/impls.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// this use. Of course this is not a guarantee that such use will work, it just means that this
1616
// crate doing wrapping pointer arithmetic with a method that must not wrap won't be the problem if
1717
// something does go wrong at runtime.
18+
use core::ffi::c_int;
1819
use core::intrinsics::likely;
1920

2021
const WORD_SIZE: usize = core::mem::size_of::<usize>();
@@ -384,13 +385,13 @@ pub unsafe fn set_bytes(mut s: *mut u8, c: u8, mut n: usize) {
384385
}
385386

386387
#[inline(always)]
387-
pub unsafe fn compare_bytes(s1: *const u8, s2: *const u8, n: usize) -> i32 {
388+
pub unsafe fn compare_bytes(s1: *const u8, s2: *const u8, n: usize) -> c_int {
388389
let mut i = 0;
389390
while i < n {
390391
let a = *s1.wrapping_add(i);
391392
let b = *s2.wrapping_add(i);
392393
if a != b {
393-
return a as i32 - b as i32;
394+
return c_int::from(a) - c_int::from(b);
394395
}
395396
i += 1;
396397
}

compiler-builtins/src/mem/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ intrinsics! {
3737
}
3838

3939
#[mem_builtin]
40-
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
40+
pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> core::ffi::c_int {
4141
impls::compare_bytes(s1, s2, n)
4242
}
4343

4444
#[mem_builtin]
45-
pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
45+
pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> core::ffi::c_int {
4646
memcmp(s1, s2, n)
4747
}
4848

0 commit comments

Comments
 (0)