Skip to content

[SR-6361] Fix Data.withUnsafeMutableBytes() for slices with length < range.lowerBound #1314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2017
Merged
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
8 changes: 4 additions & 4 deletions Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,20 @@ public final class _DataStorage {
switch _backing {
case .swift: fallthrough
case .mutable:
return try apply(UnsafeMutableRawBufferPointer(start: _bytes!.advanced(by:range.lowerBound - _offset), count: Swift.min(range.count, _length - range.lowerBound)))
return try apply(UnsafeMutableRawBufferPointer(start: _bytes!.advanced(by:range.lowerBound - _offset), count: Swift.min(range.count, _length)))
case .customMutableReference(let d):
let len = d.length
return try apply(UnsafeMutableRawBufferPointer(start: d.mutableBytes.advanced(by:range.lowerBound - _offset), count: Swift.min(range.count, len - range.lowerBound)))
return try apply(UnsafeMutableRawBufferPointer(start: d.mutableBytes.advanced(by:range.lowerBound - _offset), count: Swift.min(range.count, len)))
case .immutable(let d):
let data = d.mutableCopy() as! NSMutableData
_backing = .mutable(data)
_bytes = data.mutableBytes
return try apply(UnsafeMutableRawBufferPointer(start: _bytes!.advanced(by:range.lowerBound - _offset), count: Swift.min(range.count, _length - range.lowerBound)))
return try apply(UnsafeMutableRawBufferPointer(start: _bytes!.advanced(by:range.lowerBound - _offset), count: Swift.min(range.count, _length)))
case .customReference(let d):
let data = d.mutableCopy() as! NSMutableData
_backing = .customMutableReference(data)
let len = data.length
return try apply(UnsafeMutableRawBufferPointer(start: data.mutableBytes.advanced(by:range.lowerBound - _offset), count: Swift.min(range.count, len - range.lowerBound)))
return try apply(UnsafeMutableRawBufferPointer(start: data.mutableBytes.advanced(by:range.lowerBound - _offset), count: Swift.min(range.count, len)))
}
}

Expand Down
50 changes: 50 additions & 0 deletions TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ class TestNSData: XCTestCase {
// ("test_sliceEnumeration", test_sliceEnumeration),
("test_sliceInsertion", test_sliceInsertion),
("test_sliceDeletion", test_sliceDeletion),
("test_validateMutation_slice_withUnsafeMutableBytes_lengthLessThanLowerBound", test_validateMutation_slice_withUnsafeMutableBytes_lengthLessThanLowerBound),
("test_validateMutation_slice_immutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound", test_validateMutation_slice_immutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound),
("test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound", test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound),
("test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound", test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound),
("test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound",
test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound),
]
}

Expand Down Expand Up @@ -3979,5 +3985,49 @@ extension TestNSData {
XCTAssertEqual(mutableSliceData.startIndex, 2)
XCTAssertEqual(mutableSliceData.endIndex, sliceData.endIndex - numberOfElementsToDelete)
}

func test_validateMutation_slice_withUnsafeMutableBytes_lengthLessThanLowerBound() {
var data = Data(bytes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
ptr.advanced(by: 1).pointee = 0xFF
}
XCTAssertEqual(data, Data(bytes: [4, 0xFF]))
}

func test_validateMutation_slice_immutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() {
var data = Data(referencing: NSData(bytes: "hello world", length: 11))[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
ptr.advanced(by: 1).pointee = 0xFF
}
XCTAssertEqual(data[data.startIndex.advanced(by: 1)], 0xFF)
}

func test_validateMutation_slice_mutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() {
var base = Data(referencing: NSData(bytes: "hello world", length: 11))
base.append(contentsOf: [1, 2, 3, 4, 5, 6])
var data = base[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
ptr.advanced(by: 1).pointee = 0xFF
}
XCTAssertEqual(data[data.startIndex.advanced(by: 1)], 0xFF)
}

func test_validateMutation_slice_customBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() {
var data = Data(referencing: AllOnesImmutableData(length: 10))[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
ptr.advanced(by: 1).pointee = 0xFF
}
XCTAssertEqual(data[data.startIndex.advanced(by: 1)], 0xFF)
}

func test_validateMutation_slice_customMutableBacking_withUnsafeMutableBytes_lengthLessThanLowerBound() {
var base = Data(referencing: AllOnesData(length: 1))
base.count = 10
var data = base[4..<6]
data.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) in
ptr.advanced(by: 1).pointee = 0xFF
}
XCTAssertEqual(data[data.startIndex.advanced(by: 1)], 0xFF)
}
}