File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -498,6 +498,17 @@ extension Unsafe${Mutable}BufferPointer {
498
498
/// - Parameter slice: The buffer slice to rebase.
499
499
@inlinable // unsafe-performance
500
500
public init( rebasing slice: Slice< UnsafeBufferPointer< Element>>) {
501
+ // NOTE: `Slice` does not guarantee that its start/end indices are valid
502
+ // in `base` -- it merely ensures that `startIndex <= endIndex`.
503
+ // We need manually check that we aren't given an invalid slice,
504
+ // or the resulting collection would allow access that was
505
+ // out-of-bounds with respect to the original base buffer.
506
+ // We only do this in debug builds to prevent a measurable performance
507
+ // degradation wrt passing around pointers not wrapped in a BufferPointer
508
+ // construct.
509
+ _debugPrecondition (
510
+ slice. startIndex >= 0 && slice. endIndex <= slice. base. count,
511
+ " Invalid slice " )
501
512
let base = slice. base. baseAddress? . advanced ( by: slice. startIndex)
502
513
let count = slice. endIndex &- slice. startIndex
503
514
self . init ( start: base, count: count)
Original file line number Diff line number Diff line change @@ -511,6 +511,17 @@ extension Unsafe${Mutable}RawBufferPointer {
511
511
/// - Parameter slice: The raw buffer slice to rebase.
512
512
@inlinable
513
513
public init ( rebasing slice: Slice < UnsafeRawBufferPointer > ) {
514
+ // NOTE: `Slice` does not guarantee that its start/end indices are valid
515
+ // in `base` -- it merely ensures that `startIndex <= endIndex`.
516
+ // We need manually check that we aren't given an invalid slice,
517
+ // or the resulting collection would allow access that was
518
+ // out-of-bounds with respect to the original base buffer.
519
+ // We only do this in debug builds to prevent a measurable performance
520
+ // degradation wrt passing around pointers not wrapped in a BufferPointer
521
+ // construct.
522
+ _debugPrecondition (
523
+ slice. startIndex >= 0 && slice. endIndex <= slice. base. count,
524
+ " Invalid slice " )
514
525
let base = slice. base. baseAddress? . advanced ( by: slice. startIndex)
515
526
let count = slice. endIndex &- slice. startIndex
516
527
self . init ( start: base, count: count)
You can’t perform that action at this time.
0 commit comments