@@ -64,6 +64,7 @@ pub use ::libc::INT_MAX;
6464use libc:: { c_char, c_int, c_long, c_uint, c_ulong, c_ushort, c_void, size_t, ptrdiff_t, memcpy, memcmp, memmove, memset} ;
6565use num_traits:: { ToPrimitive , FromPrimitive } ;
6666
67+ use std:: collections:: TryReserveError ;
6768use fallible_collections:: FallibleBox ;
6869
6970use std:: alloc:: { self , Layout } ;
@@ -224,6 +225,12 @@ impl<'a, T> ops::DerefMut for ExpatBufRefMut<'a, T> {
224225 }
225226}
226227
228+ impl < ' a , T > From < & ' a mut [ T ] > for ExpatBufRefMut < ' a , T > {
229+ fn from ( s : & ' a mut [ T ] ) -> ExpatBufRefMut < ' a , T > {
230+ ExpatBufRefMut ( s)
231+ }
232+ }
233+
227234/// Create a null-terminated XML_Char array from ASCII_ literals
228235macro_rules! XML_STR {
229236 [ $( $char: ident) ,* $( , ) * ] => {
@@ -778,8 +785,8 @@ pub struct XML_ParserStruct<'scf> {
778785 m_parseEndByteIndex : usize ,
779786 // Index in m_buffer after last character that has been parsed
780787 m_parseEndIdx : usize ,
781- pub m_dataBuf : * mut XML_Char , // Box<[XML_Char; INIT_DATA_BUF_SIZE]>
782- pub m_dataBufEnd : * mut XML_Char ,
788+ // Temporary scratch buffer
789+ m_dataBuf : Box < [ XML_Char ; INIT_DATA_BUF_SIZE as usize ] > ,
783790
784791 // Handlers should be trait, with native C callback instance
785792 m_handlers : CXmlHandlers < ' scf > ,
@@ -1455,8 +1462,9 @@ pub unsafe extern "C" fn XML_ParserCreate_MM(
14551462}
14561463
14571464impl < ' scf > XML_ParserStruct < ' scf > {
1458- fn new ( use_namespaces : bool ) -> Self {
1459- Self {
1465+ fn new ( use_namespaces : bool ) -> Result < Self , TryReserveError > {
1466+ let m_dataBuf = Box :: try_new ( [ 0 ; INIT_DATA_BUF_SIZE as usize ] ) ?;
1467+ Ok ( Self {
14601468 m_userData : ptr:: null_mut ( ) ,
14611469 m_buffer : Vec :: new ( ) ,
14621470 // index of first character to be parsed
@@ -1465,8 +1473,7 @@ impl<'scf> XML_ParserStruct<'scf> {
14651473 m_bufferEnd : 0 ,
14661474 m_parseEndByteIndex : 0 ,
14671475 m_parseEndIdx : 0 ,
1468- m_dataBuf : ptr:: null_mut ( ) , // Box<[XML_Char; INIT_DATA_BUF_SIZE]>
1469- m_dataBufEnd : ptr:: null_mut ( ) ,
1476+ m_dataBuf,
14701477
14711478 m_handlers : Default :: default ( ) ,
14721479
@@ -1539,7 +1546,7 @@ impl<'scf> XML_ParserStruct<'scf> {
15391546
15401547 #[ cfg( feature = "mozilla" ) ]
15411548 m_mismatch : ptr:: null ( ) ,
1542- }
1549+ } )
15431550 }
15441551
15451552 unsafe fn create (
@@ -1548,9 +1555,8 @@ impl<'scf> XML_ParserStruct<'scf> {
15481555 mut dtd : * mut DTD ,
15491556 ) -> XML_Parser {
15501557 let use_namespaces = !nameSep. is_null ( ) ;
1551- let mut parser = XML_ParserStruct :: new ( use_namespaces) ;
15521558
1553- let mut parser = match Box :: try_new ( parser ) {
1559+ let mut parser = match XML_ParserStruct :: new ( use_namespaces ) . and_then ( Box :: try_new) {
15541560 Ok ( p) => p,
15551561 Err ( _) => return ptr:: null_mut ( ) ,
15561562 } ;
@@ -1996,7 +2002,6 @@ impl<'scf> Drop for XML_ParserStruct<'scf> {
19962002 dtdDestroy ( self . m_dtd ) ;
19972003 }
19982004 FREE ! ( self . m_groupConnector) ;
1999- FREE ! ( self . m_dataBuf) ;
20002005 if self . m_unknownEncodingRelease . is_some ( ) {
20012006 self . m_unknownEncodingRelease
20022007 . expect ( "non-null function pointer" ) ( self . m_unknownEncodingData ) ;
@@ -3510,11 +3515,11 @@ unsafe extern "C" fn externalEntityContentProcessor(
35103515}
35113516
35123517impl < ' scf > XML_ParserStruct < ' scf > {
3513- unsafe fn doContent (
3514- & mut self ,
3518+ unsafe fn doContent < ' a , ' b : ' a > (
3519+ & ' b mut self ,
35153520 startTagLevel : c_int ,
35163521 enc_type : EncodingType ,
3517- mut buf : ExpatBufRef ,
3522+ mut buf : ExpatBufRef < ' a > ,
35183523 nextPtr : * mut * const c_char ,
35193524 haveMore : XML_Bool ,
35203525 ) -> XML_Error {
@@ -4022,14 +4027,12 @@ impl<'scf> XML_ParserStruct<'scf> {
40224027 }
40234028 if self . m_handlers . hasCharacterData ( ) {
40244029 if MUST_CONVERT ! ( enc, buf. as_ptr( ) ) {
4025- let mut dataPtr = ExpatBufRefMut :: new (
4026- self . m_dataBuf as * mut ICHAR ,
4027- self . m_dataBufEnd as * mut ICHAR ,
4028- ) ;
4030+ let dataStart = self . m_dataBuf . as_ptr ( ) ;
4031+ let mut dataPtr = ( & mut self . m_dataBuf [ ..] ) . into ( ) ;
40294032 XmlConvert ! ( enc, & mut buf, & mut dataPtr) ;
40304033 self . m_handlers . characterData (
40314034 & ExpatBufRef :: new (
4032- self . m_dataBuf ,
4035+ dataStart ,
40334036 dataPtr. as_ptr ( ) ,
40344037 ) ,
40354038 ) ;
@@ -4059,18 +4062,19 @@ impl<'scf> XML_ParserStruct<'scf> {
40594062 if MUST_CONVERT ! ( enc, buf. as_ptr( ) ) {
40604063 loop {
40614064 let mut from_buf = buf. with_end ( next) ;
4062- let mut to_buf = ExpatBufRefMut :: new (
4063- self . m_dataBuf as * mut ICHAR ,
4064- self . m_dataBufEnd as * mut ICHAR ,
4065- ) ;
4065+ let dataStart = self . m_dataBuf . as_ptr ( ) ;
4066+ let mut to_buf = ( & mut self . m_dataBuf [ ..] ) . into ( ) ;
40664067 let convert_res_0: super :: xmltok:: XML_Convert_Result = XmlConvert ! (
40674068 enc,
40684069 & mut from_buf,
40694070 & mut to_buf,
40704071 ) ;
40714072 buf = buf. with_start ( from_buf. as_ptr ( ) ) ;
40724073 * eventEndPP = buf. as_ptr ( ) ;
4073- let data_buf = ExpatBufRef :: new ( self . m_dataBuf , to_buf. as_ptr ( ) ) ;
4074+ let data_buf = ExpatBufRef :: new (
4075+ dataStart,
4076+ to_buf. as_ptr ( ) ,
4077+ ) ;
40744078 handlers. characterData ( & data_buf) ;
40754079 if convert_res_0 == super :: xmltok:: XML_Convert_Result :: COMPLETED
40764080 || convert_res_0 == super :: xmltok:: XML_Convert_Result :: INPUT_INCOMPLETE
@@ -4926,10 +4930,7 @@ unsafe extern "C" fn doCdataSection(
49264930 if MUST_CONVERT ! ( enc, buf. as_ptr( ) ) {
49274931 loop {
49284932 let mut from_buf = buf. with_end ( next) ;
4929- let mut to_buf = ExpatBufRefMut :: new (
4930- ( * parser) . m_dataBuf as * mut ICHAR ,
4931- ( * parser) . m_dataBufEnd as * mut ICHAR ,
4932- ) ;
4933+ let mut to_buf = ( & mut ( * parser) . m_dataBuf [ ..] ) . into ( ) ;
49334934 let convert_res: super :: xmltok:: XML_Convert_Result = XmlConvert ! (
49344935 enc,
49354936 & mut from_buf,
@@ -4939,7 +4940,7 @@ unsafe extern "C" fn doCdataSection(
49394940 * eventEndPP = next;
49404941 handlers. characterData (
49414942 & ExpatBufRef :: new (
4942- ( * parser) . m_dataBuf ,
4943+ ( * parser) . m_dataBuf . as_ptr ( ) ,
49434944 to_buf. as_ptr ( ) ,
49444945 ) ,
49454946 ) ;
@@ -7724,16 +7725,13 @@ unsafe extern "C" fn reportDefault(
77247725 eventEndPP = & mut ( * parser) . m_eventEndPtr
77257726 }
77267727 loop {
7727- let mut data_buf = ExpatBufRefMut :: new (
7728- ( * parser) . m_dataBuf as * mut ICHAR ,
7729- ( * parser) . m_dataBufEnd as * mut ICHAR ,
7730- ) ;
7728+ let mut data_buf = ( & mut ( * parser) . m_dataBuf [ ..] ) . into ( ) ;
77317729 convert_res = XmlConvert ! ( enc, & mut buf, & mut data_buf) ;
77327730 * eventEndPP = buf. as_ptr ( ) ;
77337731
77347732 let defaultRan = ( * parser) . m_handlers . default (
7735- ( * parser) . m_dataBuf ,
7736- data_buf. as_ptr ( ) . wrapping_offset_from ( ( * parser) . m_dataBuf ) . try_into ( ) . unwrap ( ) ,
7733+ ( * parser) . m_dataBuf . as_ptr ( ) ,
7734+ data_buf. as_ptr ( ) . wrapping_offset_from ( ( * parser) . m_dataBuf . as_ptr ( ) ) . try_into ( ) . unwrap ( ) ,
77377735 ) ;
77387736
77397737 // Previously unwrapped an Option
0 commit comments