Skip to content

Fix memory corruption with NSMutableData(length:) #383

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
Jul 8, 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
6 changes: 2 additions & 4 deletions Foundation/NSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,7 @@ extension NSMutableData {
}

public convenience init?(length: Int) {
let memory = malloc(length)
self.init(bytes: memory, length: length, copy: false) { buffer, amount in
free(buffer)
}
self.init(bytes: nil, length: 0)
self.length = length
}
}
9 changes: 8 additions & 1 deletion TestFoundation/TestNSData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class TestNSData: XCTestCase {
("test_initializeWithBase64EncodedStringGetsDecodedData", test_initializeWithBase64EncodedStringGetsDecodedData),
("test_base64DecodeWithPadding1", test_base64DecodeWithPadding1),
("test_base64DecodeWithPadding2", test_base64DecodeWithPadding2),
("test_rangeOfData",test_rangeOfData)
("test_rangeOfData",test_rangeOfData),
("test_initMutableDataWithLength", test_initMutableDataWithLength)
]
}

Expand Down Expand Up @@ -338,4 +339,10 @@ class TestNSData: XCTestCase {

}

func test_initMutableDataWithLength() {
let mData = NSMutableData(length: 30)
XCTAssertNotNil(mData)
XCTAssertEqual(mData!.length, 30)
}

}