Skip to content

[Foundation] Correct case of over-released data contents when specifying .none as a deallocator #6398

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
Dec 19, 2016
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
20 changes: 13 additions & 7 deletions stdlib/public/SDK/Foundation/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public final class _DataStorage {
num -= pages
}
if num > 0 {
memmove(dest, source, num)
memmove(dest, source!, num)
}
}

Expand Down Expand Up @@ -500,7 +500,7 @@ public final class _DataStorage {
}
if replacementLength != 0 {
if replacementBytes != nil {
memmove(mutableBytes! + start, replacementBytes, replacementLength)
memmove(mutableBytes! + start, replacementBytes!, replacementLength)
} else {
memset(mutableBytes! + start, 0, replacementLength)
}
Expand Down Expand Up @@ -822,7 +822,10 @@ public final class _DataStorage {
if lhs.bytes == rhs.bytes {
return true
}
return memcmp(lhs._bytes, rhs._bytes, length1) == 0
if length1 > 0 {
return memcmp(lhs._bytes!, rhs._bytes!, length1) == 0
}
return true
}
}

Expand Down Expand Up @@ -932,15 +935,15 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
/// A custom deallocator.
case custom((UnsafeMutableRawPointer, Int) -> Void)

fileprivate var _deallocator : ((UnsafeMutableRawPointer, Int) -> Void)? {
fileprivate var _deallocator : ((UnsafeMutableRawPointer, Int) -> Void) {
#if DEPLOYMENT_RUNTIME_SWIFT
switch self {
case .unmap:
return { __NSDataInvokeDeallocatorUnmap($0, $1) }
case .free:
return { __NSDataInvokeDeallocatorFree($0, $1) }
case .none:
return nil
return { _, _ in }
case .custom(let b):
return { (ptr, len) in
b(ptr, len)
Expand All @@ -955,7 +958,7 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
case .free:
return { __NSDataInvokeDeallocatorFree($0, $1) }
case .none:
return nil
return { _, _ in }
case .custom(let b):
return { (ptr, len) in
b(ptr, len)
Expand Down Expand Up @@ -1639,7 +1642,10 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
if backing1.bytes == backing2.bytes {
return true
}
return memcmp(backing1.bytes, backing2.bytes, length1) == 0
if length1 > 0 {
return memcmp(backing1.bytes!, backing2.bytes!, length1) == 0
}
return true
}
}

Expand Down
10 changes: 10 additions & 0 deletions test/stdlib/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,15 @@ class TestData : TestDataSuper {

expectTrue(deallocated)
}

func test_doubleDeallocation() {
let data = "12345679".data(using: .utf8)!
let len = data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> Int in
let slice = Data(bytesNoCopy: UnsafeMutablePointer(mutating: bytes), count: 1, deallocator: .none)
return slice.count
}
expectEqual(len, 1)
}
}

#if !FOUNDATION_XCTEST
Expand Down Expand Up @@ -941,6 +950,7 @@ DataTests.test("test_classForCoder") { TestData().test_classForCoder() }
DataTests.test("test_AnyHashableContainingData") { TestData().test_AnyHashableContainingData() }
DataTests.test("test_AnyHashableCreatedFromNSData") { TestData().test_AnyHashableCreatedFromNSData() }
DataTests.test("test_noCopyBehavior") { TestData().test_noCopyBehavior() }
DataTests.test("test_doubleDeallocation") { TestData().test_doubleDeallocation() }

// XCTest does not have a crash detection, whereas lit does
DataTests.test("bounding failure subdata") {
Expand Down