From 017c53345748e9c72b9a223c51092864af5ec047 Mon Sep 17 00:00:00 2001 From: Ian Partridge Date: Mon, 27 Nov 2017 10:29:58 +0000 Subject: [PATCH 1/2] Fix warning: overlapping access to 'self' > Overlapping accesses to 'self', but modification requires exclusive access; consider copying to a local variable --- Foundation/NSCalendar.swift | 6 ++++-- TestFoundation/TestNSData.swift | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Foundation/NSCalendar.swift b/Foundation/NSCalendar.swift index e3b427f19e..5b952e1a2a 100644 --- a/Foundation/NSCalendar.swift +++ b/Foundation/NSCalendar.swift @@ -583,8 +583,9 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding { var at: CFAbsoluteTime = date.timeIntervalSinceReferenceDate let res: Bool = withUnsafeMutablePointer(to: &at) { t in + let copy = vector return vector.withUnsafeMutableBufferPointer { (vectorBuffer: inout UnsafeMutableBufferPointer) in - return _CFCalendarAddComponentsV(_cfObject, t, CFOptionFlags(opts.rawValue), compDesc, vectorBuffer.baseAddress!, Int32(vector.count)) + return _CFCalendarAddComponentsV(_cfObject, t, CFOptionFlags(opts.rawValue), compDesc, vectorBuffer.baseAddress!, Int32(copy.count)) } } @@ -603,8 +604,9 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding { return intArrayBuffer.baseAddress!.advanced(by: idx) } + let copy = vector return vector.withUnsafeMutableBufferPointer { (vecBuffer: inout UnsafeMutableBufferPointer>) in - return _CFCalendarGetComponentDifferenceV(_cfObject, startingDate.timeIntervalSinceReferenceDate, resultDate.timeIntervalSinceReferenceDate, CFOptionFlags(opts.rawValue), compDesc, vecBuffer.baseAddress!, Int32(vector.count)) + return _CFCalendarGetComponentDifferenceV(_cfObject, startingDate.timeIntervalSinceReferenceDate, resultDate.timeIntervalSinceReferenceDate, CFOptionFlags(opts.rawValue), compDesc, vecBuffer.baseAddress!, Int32(copy.count)) } } if res { diff --git a/TestFoundation/TestNSData.swift b/TestFoundation/TestNSData.swift index 221daa35e8..c348732254 100644 --- a/TestFoundation/TestNSData.swift +++ b/TestFoundation/TestNSData.swift @@ -924,13 +924,14 @@ extension TestNSData { XCTAssertEqual(mutatingHello.count, helloLength * 2, "The length should have changed") // Get the underlying data for hello2 + let copy = mutatingHello mutatingHello.withUnsafeMutableBytes { (bytes : UnsafeMutablePointer) in XCTAssertEqual(bytes.pointee, 0x68, "First byte should be 0x68") // Mutate it bytes.pointee = 0x67 XCTAssertEqual(bytes.pointee, 0x67, "First byte should be 0x67") - XCTAssertEqual(mutatingHello[0], 0x67, "First byte accessed via other method should still be 0x67") + XCTAssertEqual(copy[0], 0x67, "First byte accessed via other method should still be 0x67") // Verify that the first data is still correct XCTAssertEqual(hello[0], 0x68, "The first byte should still be 0x68") From 5418b99d9898cec89287b206ca7ed2d17e5fe095 Mon Sep 17 00:00:00 2001 From: Ian Partridge Date: Mon, 27 Nov 2017 10:53:15 +0000 Subject: [PATCH 2/2] Address review feedback --- Foundation/NSCalendar.swift | 8 ++++---- TestFoundation/TestNSData.swift | 9 ++++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/Foundation/NSCalendar.swift b/Foundation/NSCalendar.swift index 5b952e1a2a..b14ce2778d 100644 --- a/Foundation/NSCalendar.swift +++ b/Foundation/NSCalendar.swift @@ -583,9 +583,9 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding { var at: CFAbsoluteTime = date.timeIntervalSinceReferenceDate let res: Bool = withUnsafeMutablePointer(to: &at) { t in - let copy = vector + let count = Int32(vector.count) return vector.withUnsafeMutableBufferPointer { (vectorBuffer: inout UnsafeMutableBufferPointer) in - return _CFCalendarAddComponentsV(_cfObject, t, CFOptionFlags(opts.rawValue), compDesc, vectorBuffer.baseAddress!, Int32(copy.count)) + return _CFCalendarAddComponentsV(_cfObject, t, CFOptionFlags(opts.rawValue), compDesc, vectorBuffer.baseAddress!, count) } } @@ -604,9 +604,9 @@ open class NSCalendar : NSObject, NSCopying, NSSecureCoding { return intArrayBuffer.baseAddress!.advanced(by: idx) } - let copy = vector + let count = Int32(vector.count) return vector.withUnsafeMutableBufferPointer { (vecBuffer: inout UnsafeMutableBufferPointer>) in - return _CFCalendarGetComponentDifferenceV(_cfObject, startingDate.timeIntervalSinceReferenceDate, resultDate.timeIntervalSinceReferenceDate, CFOptionFlags(opts.rawValue), compDesc, vecBuffer.baseAddress!, Int32(copy.count)) + return _CFCalendarGetComponentDifferenceV(_cfObject, startingDate.timeIntervalSinceReferenceDate, resultDate.timeIntervalSinceReferenceDate, CFOptionFlags(opts.rawValue), compDesc, vecBuffer.baseAddress!, count) } } if res { diff --git a/TestFoundation/TestNSData.swift b/TestFoundation/TestNSData.swift index c348732254..510ff85d4a 100644 --- a/TestFoundation/TestNSData.swift +++ b/TestFoundation/TestNSData.swift @@ -924,18 +924,17 @@ extension TestNSData { XCTAssertEqual(mutatingHello.count, helloLength * 2, "The length should have changed") // Get the underlying data for hello2 - let copy = mutatingHello mutatingHello.withUnsafeMutableBytes { (bytes : UnsafeMutablePointer) in XCTAssertEqual(bytes.pointee, 0x68, "First byte should be 0x68") // Mutate it bytes.pointee = 0x67 XCTAssertEqual(bytes.pointee, 0x67, "First byte should be 0x67") - XCTAssertEqual(copy[0], 0x67, "First byte accessed via other method should still be 0x67") - - // Verify that the first data is still correct - XCTAssertEqual(hello[0], 0x68, "The first byte should still be 0x68") } + XCTAssertEqual(mutatingHello[0], 0x67, "First byte accessed via other method should still be 0x67") + + // Verify that the first data is still correct + XCTAssertEqual(hello[0], 0x68, "The first byte should still be 0x68") }