Merge pull request #659 from adityasingh2400/fix-unsafe-buffer-trim

[InternalCollectionsUtilities] Fix _trim returning the wrong buffer region
This commit is contained in:
Karoy Lorentey authored and GitHub committed 2026-05-26 17:00:10 -07:00
commit cef9727051
3 files changed
+119 -5

No files matched your search

@@ -146,7 +146,7 @@ extension UnsafeBufferPointer where Element: ~Copyable {
guard cut > 0 else { return .init(start: nil, count: 0) }
let oldStart = baseAddress.unsafelyUnwrapped
self = Self(start: oldStart + cut, count: count - cut)
return Self(start: baseAddress, count: cut)
return Self(start: oldStart, count: cut)
}
@_alwaysEmitIntoClient
@@ -154,7 +154,9 @@ extension UnsafeBufferPointer where Element: ~Copyable {
precondition(maxLength >= 0, "Cannot have a suffix of negative length")
let cut = Swift.min(maxLength, count)
guard cut > 0 else { return .init(start: nil, count: 0) }
self = .init(start: baseAddress, count: count &- cut)
return Self(start: baseAddress.unsafelyUnwrapped + (count &- cut), count: cut)
let oldStart = baseAddress.unsafelyUnwrapped
let newCount = count &- cut
self = .init(start: oldStart, count: newCount)
return Self(start: oldStart + newCount, count: cut)
}
}
@@ -167,8 +167,10 @@ extension UnsafeMutableBufferPointer where Element: ~Copyable {
precondition(maxLength >= 0, "Cannot have a suffix of negative length")
let cut = Swift.min(maxLength, count)
guard cut > 0 else { return .init(start: nil, count: 0) }
self = .init(start: baseAddress, count: count &- cut)
return Self(start: baseAddress.unsafelyUnwrapped + (count &- cut), count: cut)
let oldStart = baseAddress.unsafelyUnwrapped
let newCount = count &- cut
self = .init(start: oldStart, count: newCount)
return Self(start: oldStart + newCount, count: cut)
}
}
@@ -0,0 +1,110 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//
import XCTest
#if COLLECTIONS_SINGLE_MODULE
@_spi(Testing) import Collections
#else
import _CollectionsTestSupport
import InternalCollectionsUtilities
#endif
/// Tests for the `_trim(first:)` and `_trim(last:)` helpers on the unsafe
/// buffer pointer types in `InternalCollectionsUtilities`. These are used by
/// the deque consumption/segment machinery, so a wrong result here corrupts
/// the spans that get handed out to clients.
final class UnsafeBufferTrimTests: CollectionTestCase {
func test_mutableBuffer_trimFirst() {
let values = [10, 20, 30, 40, 50]
values.withUnsafeBufferPointer { source in
let storage = UnsafeMutableBufferPointer<Int>.allocate(capacity: source.count)
defer { storage.deallocate() }
_ = storage.initialize(fromContentsOf: source)
defer { storage.deinitialize() }
var rest = storage
let head = rest._trim(first: 2)
expectEqual(Array(head), [10, 20])
expectEqual(Array(rest), [30, 40, 50])
// Trimming more than the count yields everything.
var all = storage
let everything = all._trim(first: 99)
expectEqual(Array(everything), [10, 20, 30, 40, 50])
expectEqual(all.count, 0)
}
}
func test_mutableBuffer_trimLast() {
let values = [10, 20, 30, 40, 50]
values.withUnsafeBufferPointer { source in
let storage = UnsafeMutableBufferPointer<Int>.allocate(capacity: source.count)
defer { storage.deallocate() }
_ = storage.initialize(fromContentsOf: source)
defer { storage.deinitialize() }
var rest = storage
let tail = rest._trim(last: 2)
expectEqual(Array(tail), [40, 50])
expectEqual(Array(rest), [10, 20, 30])
var all = storage
let everything = all._trim(last: 99)
expectEqual(Array(everything), [10, 20, 30, 40, 50])
expectEqual(all.count, 0)
}
}
func test_immutableBuffer_trimFirst() {
let values = [10, 20, 30, 40, 50]
values.withUnsafeBufferPointer { source in
var rest = source
let head = rest._trim(first: 2)
expectEqual(Array(head), [10, 20])
expectEqual(Array(rest), [30, 40, 50])
var all = source
let everything = all._trim(first: 99)
expectEqual(Array(everything), [10, 20, 30, 40, 50])
expectEqual(all.count, 0)
}
}
func test_immutableBuffer_trimLast() {
let values = [10, 20, 30, 40, 50]
values.withUnsafeBufferPointer { source in
var rest = source
let tail = rest._trim(last: 2)
expectEqual(Array(tail), [40, 50])
expectEqual(Array(rest), [10, 20, 30])
var all = source
let everything = all._trim(last: 99)
expectEqual(Array(everything), [10, 20, 30, 40, 50])
expectEqual(all.count, 0)
}
}
func test_trim_zeroIsNoOp() {
let values = [1, 2, 3]
values.withUnsafeBufferPointer { source in
var a = source
expectEqual(a._trim(first: 0).count, 0)
expectEqual(Array(a), [1, 2, 3])
var b = source
expectEqual(b._trim(last: 0).count, 0)
expectEqual(Array(b), [1, 2, 3])
}
}
}