@@ -50,8 +50,6 @@ use core::iter::from_fn;
5050use core:: ops:: Add ;
5151#[ cfg( not( no_global_oom_handling) ) ]
5252use core:: ops:: AddAssign ;
53- #[ cfg( not( no_global_oom_handling) ) ]
54- use core:: ops:: Bound :: { Excluded , Included , Unbounded } ;
5553use core:: ops:: { self , Range , RangeBounds } ;
5654use core:: str:: pattern:: { Pattern , Utf8Pattern } ;
5755use core:: { fmt, hash, ptr, slice} ;
@@ -2062,30 +2060,19 @@ impl String {
20622060 where
20632061 R : RangeBounds < usize > ,
20642062 {
2065- // Memory safety
2066- //
2067- // Replace_range does not have the memory safety issues of a vector Splice.
2068- // of the vector version. The data is just plain bytes.
2069-
2070- // WARNING: Inlining this variable would be unsound (#81138)
2071- let start = range. start_bound ( ) ;
2072- match start {
2073- Included ( & n) => assert ! ( self . is_char_boundary( n) ) ,
2074- Excluded ( & n) => assert ! ( self . is_char_boundary( n + 1 ) ) ,
2075- Unbounded => { }
2076- } ;
2077- // WARNING: Inlining this variable would be unsound (#81138)
2078- let end = range. end_bound ( ) ;
2079- match end {
2080- Included ( & n) => assert ! ( self . is_char_boundary( n + 1 ) ) ,
2081- Excluded ( & n) => assert ! ( self . is_char_boundary( n) ) ,
2082- Unbounded => { }
2083- } ;
2084-
2085- // Using `range` again would be unsound (#81138)
2086- // We assume the bounds reported by `range` remain the same, but
2087- // an adversarial implementation could change between calls
2088- unsafe { self . as_mut_vec ( ) } . splice ( ( start, end) , replace_with. bytes ( ) ) ;
2063+ // We avoid #81138 (nondeterministic RangeBounds impls) because we only use `range` once, here.
2064+ let checked_range = slice:: range ( range, ..self . len ( ) ) ;
2065+
2066+ assert ! (
2067+ self . is_char_boundary( checked_range. start) ,
2068+ "start of range should be a character boundary"
2069+ ) ;
2070+ assert ! (
2071+ self . is_char_boundary( checked_range. end) ,
2072+ "end of range should be a character boundary"
2073+ ) ;
2074+
2075+ unsafe { self . as_mut_vec ( ) } . splice ( checked_range, replace_with. bytes ( ) ) ;
20892076 }
20902077
20912078 /// Replaces the leftmost occurrence of a pattern with another string, in-place.
0 commit comments