From d439847ba67f84096775f3fa019361a640101c08 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 29 Oct 2021 09:17:56 +0200 Subject: [PATCH 1/2] Fix NSData initWithBytesNoCopy:length:deallocator: The previous implementation simply swizzled `NSData` into `NSDataWithDeallocatorBlock`, and forgot to actually assign `bytes` and `length`. --- Source/NSData.m | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/NSData.m b/Source/NSData.m index a6973ebf62..c5bf2017ff 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -3548,6 +3548,8 @@ - (instancetype) initWithBytesNoCopy: (void*)buf } GSClassSwizzle(self, dataBlock); + bytes = buf; + length = len; ASSIGN(deallocator, (id)deallocBlock); return self; } From 9684c9fda1006cb4cc9bff680a7d6bb8d4beb62c Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 29 Oct 2021 10:17:39 +0200 Subject: [PATCH 2/2] Add test to ensure that NSData assigns `bytes` and `length` Specifically on initWithBytesNoCopy:length:deallocator: --- Tests/base/NSData/general.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tests/base/NSData/general.m b/Tests/base/NSData/general.m index 8780d1f59e..54b24963a0 100644 --- a/Tests/base/NSData/general.m +++ b/Tests/base/NSData/general.m @@ -89,6 +89,8 @@ int main() deallocator: ^(void* bytes, NSUInteger length) { called++; }]; + PASS([immutable length] == 4, "Length set"); + PASS([immutable bytes] == stackBuf, "Bytes set"); PASS_RUNS([immutable release]; immutable = nil;, "No free() error with custom deallocator"); PASS(called == 1, "Deallocator block called"); @@ -102,6 +104,8 @@ int main() called++; } ]; + PASS([mutable length] == 4, "Length set"); + PASS([mutable bytes] == buf, "Bytes set"); PASS_RUNS([mutable release]; mutable = nil;, "No free() error with custom deallocator on mutable data"); PASS(called == 2, "Deallocator block called on -dealloc of mutable data");