Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit a6afd20

Browse files
committed
Corrected memory leak from extra memory allocation.
We were allocating twice the memory needed to buffer the QR code string. The second allocation (strdup) was overwriting the assignment of the pointer to the first. Consequently, the first allocation was never free'd. In the end, we don't need to allocate anything ourself and can defer to Apple's Foundation to do that for us.
1 parent 5fe9f08 commit a6afd20

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

src/device-manager/cocoa/NLWeaveDeviceDescriptor.mm

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,13 @@ + (instancetype)decodeDeviceDescriptor:(NSString *)descriptorStr
3434
WDM_LOG_METHOD_SIG();
3535

3636
WEAVE_ERROR err;
37-
uint8_t * encodedBuf = NULL;
38-
uint32_t encodedLen;
3937
WeaveDeviceDescriptor deviceDesc;
40-
41-
NLWeaveDeviceDescriptor * nlDeviceDescriptor = nil;
42-
43-
encodedLen = [descriptorStr length];
44-
45-
encodedBuf = (uint8_t *) malloc(encodedLen + 1);
46-
encodedBuf = (uint8_t *) strdup([descriptorStr UTF8String]);
47-
48-
err = WeaveDeviceDescriptor::Decode(encodedBuf, encodedLen, deviceDesc);
38+
NSData *buffer = [descriptorStr dataUsingEncoding:NSUTF8StringEncoding];
39+
err = WeaveDeviceDescriptor::Decode((const uint8_t *)buffer.bytes, (uint32_t)buffer.length, deviceDesc);
4940
SuccessOrExit(err);
5041

51-
nlDeviceDescriptor = [NLWeaveDeviceDescriptor createUsing:deviceDesc];
52-
5342
exit:
54-
if (encodedBuf != NULL)
55-
free((uint8_t *) encodedBuf);
56-
57-
return nlDeviceDescriptor;
43+
return [NLWeaveDeviceDescriptor createUsing:deviceDesc];
5844
}
5945

6046
+ (NLWeaveDeviceDescriptor *)createUsing:(WeaveDeviceDescriptor)deviceDescriptor

0 commit comments

Comments
 (0)