Skip to content
This repository was archived by the owner on Feb 10, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#![feature(const_raw_ptr_to_usize_cast)]
#![feature(main)]
#![feature(const_in_array_repeat_expressions)]
#![feature(ptr_wrapping_offset_from)]
#![feature(try_reserve)]
#![feature(alloc_layout_extra)]
// Required by !Send and !Sync
Expand Down
56 changes: 28 additions & 28 deletions src/lib/xmlparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub const XML_CONTEXT_BYTES: c_int = 1024;
pub struct ExpatBufRef<'a, T = c_char>(&'a [T]);
impl<'a, T> ExpatBufRef<'a, T> {
pub fn new<'new>(start: *const T, end: *const T) -> ExpatBufRef<'new, T> {
unsafe { ExpatBufRef(std::slice::from_raw_parts(start, end.wrapping_offset_from(start) as usize)) }
unsafe { ExpatBufRef(std::slice::from_raw_parts(start, end as usize - start as usize)) }
}
pub fn new_len<'new>(start: *const T, len: usize) -> ExpatBufRef<'new, T> {
unsafe { ExpatBufRef(std::slice::from_raw_parts(start, len)) }
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<'a> From<ExpatBufRef<'a, c_char>> for ExpatBufRef<'a, c_ushort> {
pub struct ExpatBufRefMut<'a, T = c_char>(&'a mut [T]);
impl<'a, T> ExpatBufRefMut<'a, T> {
pub fn new<'new>(start: *mut T, end: *mut T) -> ExpatBufRefMut<'new, T> {
unsafe { ExpatBufRefMut(std::slice::from_raw_parts_mut(start, end.wrapping_offset_from(start) as usize)) }
unsafe { ExpatBufRefMut(std::slice::from_raw_parts_mut(start, end as usize - start as usize)) }
}
pub fn new_len<'new>(start: *mut T, len: usize) -> ExpatBufRefMut<'new, T> {
unsafe { ExpatBufRefMut(std::slice::from_raw_parts_mut(start, len)) }
Expand All @@ -177,7 +177,7 @@ impl<'a, T> ExpatBufRefMut<'a, T> {
if new_start < self.as_mut_ptr() || new_start > self.end() {
panic!("Attempted to move the start of an ExpatBufRefMut to an invalid pointer: {:?}", new_start);
}
let offset = new_start.wrapping_offset_from(self.0.as_ptr());
let offset: isize = (new_start as usize - self.0.as_ptr() as usize).try_into().unwrap();
let new_start = unsafe { self.0.as_mut_ptr().offset(offset) };
*self = ExpatBufRefMut::new(new_start, self.end());
}
Expand Down Expand Up @@ -1014,7 +1014,7 @@ fn safe_ptr_diff<T>(p: *const T, q: *const T) -> isize {
if p.is_null() || q.is_null() {
0
} else {
p.wrapping_offset_from(q)
(p as usize - (q as usize)) as isize
}
}

Expand Down Expand Up @@ -2826,9 +2826,9 @@ impl XML_ParserStruct {
self.m_buffer as *mut c_void,
&mut *self.m_buffer.offset(offset as isize) as *mut c_char
as *const c_void,
(self
.m_bufferEnd
.wrapping_offset_from(self.m_bufferPtr)
((self
.m_bufferEnd as usize
- (self.m_bufferPtr as usize)) as isize
+ keep as isize).try_into().unwrap(),
);
self.m_bufferEnd = self.m_bufferEnd.offset(-offset);
Expand Down Expand Up @@ -3066,9 +3066,9 @@ pub unsafe extern "C" fn XML_GetCurrentByteIndex(mut parser: XML_Parser) -> XML_
}
if !(*parser).m_eventPtr.is_null() {
return (*parser).m_parseEndByteIndex
- (*parser)
.m_parseEndPtr
.wrapping_offset_from((*parser).m_eventPtr) as c_long;
- ((*parser)
.m_parseEndPtr as usize
- ((*parser).m_eventPtr as usize)) as c_long;
}
if cfg!(feature = "mozilla") {
return (*parser).m_parseEndByteIndex;
Expand All @@ -3084,9 +3084,9 @@ pub unsafe extern "C" fn XML_GetCurrentByteCount(mut parser: XML_Parser) -> c_in
return 0;
}
if !(*parser).m_eventEndPtr.is_null() && !(*parser).m_eventPtr.is_null() {
return (*parser)
.m_eventEndPtr
.wrapping_offset_from((*parser).m_eventPtr) as c_int;
return ((*parser)
.m_eventEndPtr as usize
- ((*parser).m_eventPtr as usize)) as c_int;
}
0
}
Expand All @@ -3111,14 +3111,14 @@ pub unsafe extern "C" fn XML_GetInputContext(
}
if !(*parser).m_eventPtr.is_null() && !(*parser).m_buffer.is_null() {
if !offset.is_null() {
*offset = (*parser)
.m_eventPtr
.wrapping_offset_from((*parser).m_buffer) as c_int
*offset = ((*parser)
.m_eventPtr as usize
- ((*parser).m_buffer as usize )) as c_int
}
if !size.is_null() {
*size = (*parser)
.m_bufferEnd
.wrapping_offset_from((*parser).m_buffer) as c_int
*size = ((*parser)
.m_bufferEnd as usize
- ((*parser).m_buffer as usize)) as c_int
}
return (*parser).m_buffer;
}
Expand Down Expand Up @@ -3443,7 +3443,7 @@ impl XML_ParserStruct {
bufSize = (nameLen as c_ulong).wrapping_add(
round_up(tag.rawNameLength as usize, mem::size_of::<XML_Char>()) as c_ulong,
) as c_int;
if bufSize as c_long > tag.bufEnd.wrapping_offset_from(tag.buf) as c_long {
if bufSize as c_long > ((tag.bufEnd as usize - (tag.buf as usize)) as c_long) {
let mut temp = REALLOC!(tag.buf => [c_char; bufSize]);
if temp.is_null() {
return false;
Expand All @@ -3459,10 +3459,10 @@ impl XML_ParserStruct {
*/
if !tag.name.localPart.is_null() {
tag.name.localPart = (temp).offset(
tag
(tag
.name
.localPart
.wrapping_offset_from(tag.buf as *const XML_Char),
.localPart as usize
- (tag.buf as *const XML_Char as usize)).try_into().unwrap()
) as *const XML_Char
} /* XmlContentTok doesn't always set the last arg */
tag.buf = temp;
Expand Down Expand Up @@ -3868,13 +3868,13 @@ impl XML_ParserStruct {
&mut fromBuf,
&mut to_buf,
);
convLen = to_buf.as_ptr().wrapping_offset_from(tag.buf as *mut XML_Char).try_into().unwrap();
convLen = (to_buf.as_ptr() as usize - (tag.buf as *mut XML_Char as usize)).try_into().unwrap();
if fromBuf.is_empty() || convert_res == super::xmltok::XML_Convert_Result::INPUT_INCOMPLETE
{
tag.name.strLen = convLen;
break;
} else {
bufSize = (tag.bufEnd.wrapping_offset_from(tag.buf) as c_int) << 1;
bufSize = ((tag.bufEnd as usize - (tag.buf as usize)) as c_int) << 1;
let mut temp = REALLOC!(tag.buf => [c_char; bufSize]);
if temp.is_null() {
return XML_Error::NO_MEMORY;
Expand Down Expand Up @@ -7054,7 +7054,7 @@ impl XML_ParserStruct {
}
if result == XML_Error::NONE {
if text_buf.end() != next && self.m_parsingStatus.parsing == XML_Parsing::SUSPENDED {
(*entity).processed = next.wrapping_offset_from(text_buf.as_ptr()) as i32;
(*entity).processed = (next as usize - (text_buf.as_ptr() as usize)).try_into().unwrap();
self.m_processor = Some(internalEntityProcessor as Processor)
} else {
(*entity).open = false;
Expand Down Expand Up @@ -7137,7 +7137,7 @@ unsafe extern "C" fn internalEntityProcessor(
} else {
if text_buf.end() != next && (*parser).m_parsingStatus.parsing == XML_Parsing::SUSPENDED {
(*entity).processed =
next.wrapping_offset_from((*entity).textPtr as *mut c_char) as c_int;
(next as usize - ((*entity).textPtr as *mut c_char as usize)) as c_int;
return result;
} else {
(*entity).open = false;
Expand Down Expand Up @@ -7767,7 +7767,7 @@ unsafe extern "C" fn reportDefault(

let defaultRan = (*parser).m_handlers.default(
(*parser).m_dataBuf,
data_buf.as_ptr().wrapping_offset_from((*parser).m_dataBuf).try_into().unwrap(),
(data_buf.as_ptr() as usize - ((*parser).m_dataBuf as usize)).try_into().unwrap(),
);

// Previously unwrapped an Option
Expand Down
2 changes: 1 addition & 1 deletion src/lib/xmltok_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ impl<T: XmlEncodingImpl+XmlTokImpl> XmlEncoding for T {
ptr = ptr.offset(self.MINBPC())
}
_ => {
return ptr.wrapping_offset_from(start) as libc::c_long as
return (ptr as usize - (start as usize)) as libc::c_long as
libc::c_int
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/tests/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#![feature(
const_raw_ptr_to_usize_cast,
main,
ptr_wrapping_offset_from,
register_tool
)]

Expand Down Expand Up @@ -158,7 +157,7 @@ unsafe fn main_0(mut argc: c_int, mut argv: *mut *mut c_char) -> c_int {
tstart = clock();
loop {
let mut parseBufferSize: c_int =
XMLBufEnd.wrapping_offset_from(XMLBufPtr) as c_long as c_int;
(XMLBufEnd as usize - (XMLBufPtr as usize)) as c_long as c_int;
if parseBufferSize <= bufferSize {
isFinal = 1 as c_int
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/tests/runtests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
const_raw_ptr_to_usize_cast,
label_break_value,
main,
ptr_wrapping_offset_from,
register_tool
)]

Expand Down Expand Up @@ -85,6 +84,7 @@ use ::libc::{free, printf, sprintf, fprintf, strcmp, strncmp, strlen, malloc, re
use ::rexpat::lib::xmlparse::ExpatBufRef;

use std::alloc::{GlobalAlloc, Layout, System};
use std::convert::TryInto;
use std::mem::transmute;
use std::ptr;

Expand Down Expand Up @@ -1336,7 +1336,7 @@ unsafe extern "C" fn test_utf8_auto_align() {
let mut buf = ExpatBufRef::new(cases[i as usize].input, fromLim);
_INTERNAL_trim_to_complete_utf8_characters(&mut buf);
fromLim = buf.end();
actualMovementInChars = fromLim.wrapping_offset_from(fromLimInitially);
actualMovementInChars = isize::wrapping_sub(fromLim as _, fromLimInitially as _);
if actualMovementInChars != cases[i as usize].expectedMovementInChars {
let mut j: size_t = 0;
success = false_0 != 0;
Expand Down
7 changes: 3 additions & 4 deletions src/xmlwf/xmlwf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
const_raw_ptr_to_usize_cast,
label_break_value,
main,
ptr_wrapping_offset_from,
register_tool
)]

Expand Down Expand Up @@ -171,7 +170,7 @@ unsafe extern "C" fn startElement(
while !(*p).is_null() {
p = p.offset(1)
}
nAtts = (p.wrapping_offset_from(atts) as c_long >> 1) as c_int;
nAtts = ((isize::wrapping_sub(p as _, atts as _).wrapping_div(::std::mem::size_of::<*mut *const XML_Char>() as _)) as c_long >> 1) as c_int;
if nAtts > 1 {
qsort(
atts as *mut c_void,
Expand Down Expand Up @@ -236,7 +235,7 @@ unsafe extern "C" fn startElementNS(
while !(*p).is_null() {
p = p.offset(1)
}
nAtts = (p.wrapping_offset_from(atts) as c_long >> 1) as c_int;
nAtts = ((isize::wrapping_sub(p as _, atts as _).wrapping_div(::std::mem::size_of::<*mut *const XML_Char>() as _)) as c_long >> 1) as c_int;
if nAtts > 1 {
qsort(
atts as *mut c_void,
Expand Down Expand Up @@ -921,7 +920,7 @@ unsafe extern "C" fn unknownEncoding(
return 0i32;
}
cp *= 10;
cp += s.wrapping_offset_from(digits.as_ptr()) as c_int;
cp += (s as usize - (digits.as_ptr() as usize)) as c_int;
if cp >= 0x10000 {
return 0i32;
}
Expand Down