Skip to content

Commit

Permalink
fix memory leak in macos ipv6/dns helper (#2030)
Browse files Browse the repository at this point in the history
Co-authored-by: Grant Limberg <[email protected]>
  • Loading branch information
laduke and glimberg authored Jul 6, 2023
1 parent 1ce08a6 commit 6540107
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions osdep/MacDNSHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,16 @@ static void printKeys (const void* key, const void* value, void* context) {


CFStringRef key = CFStringCreateWithCString(NULL, buf, kCFStringEncodingUTF8);
CFStringRef* cfaddrs = new CFStringRef[1];
CFStringRef* cfprefixes = new CFStringRef[1];
CFStringRef* cfdestaddrs = new CFStringRef[1];
CFStringRef* cfflags = new CFStringRef[1];

CFStringRef cfaddr = CFStringCreateWithCString(NULL, llStr, kCFStringEncodingUTF8);
CFStringRef cfprefixes = CFStringCreateWithCString(NULL, "64", kCFStringEncodingUTF8);
CFStringRef cfdestaddrs = CFStringCreateWithCString(NULL, "::ffff:ffff:ffff:ffff:0:0", kCFStringEncodingUTF8);
CFStringRef cfflags = CFStringCreateWithCString(NULL, "0", kCFStringEncodingUTF8);

cfaddrs[0] = CFStringCreateWithCString(NULL, llStr, kCFStringEncodingUTF8);
cfprefixes[0] = CFStringCreateWithCString(NULL, "64", kCFStringEncodingUTF8);
cfdestaddrs[0] = CFStringCreateWithCString(NULL, "::ffff:ffff:ffff:ffff:0:0", kCFStringEncodingUTF8);
cfflags[0] = CFStringCreateWithCString(NULL, "0", kCFStringEncodingUTF8);

CFArrayRef addrArray = CFArrayCreate(NULL, (const void**)cfaddrs, 1, &kCFTypeArrayCallBacks);
CFArrayRef prefixArray = CFArrayCreate(NULL, (const void**)cfprefixes, 1, &kCFTypeArrayCallBacks);
CFArrayRef destArray = CFArrayCreate(NULL, (const void**)cfdestaddrs, 1, &kCFTypeArrayCallBacks);
CFArrayRef flagsArray = CFArrayCreate(NULL, (const void**)cfflags, 1, &kCFTypeArrayCallBacks);
CFArrayRef addrArray = CFArrayCreate(NULL, (const void**)&cfaddr, 1, &kCFTypeArrayCallBacks);
CFArrayRef prefixArray = CFArrayCreate(NULL, (const void**)&cfprefixes, 1, &kCFTypeArrayCallBacks);
CFArrayRef destArray = CFArrayCreate(NULL, (const void**)&cfdestaddrs, 1, &kCFTypeArrayCallBacks);
CFArrayRef flagsArray = CFArrayCreate(NULL, (const void**)&cfflags, 1, &kCFTypeArrayCallBacks);
CFStringRef cfdev = CFStringCreateWithCString(NULL, dev, kCFStringEncodingUTF8);

const int SIZE = 5;
Expand Down Expand Up @@ -182,23 +177,30 @@ static void printKeys (const void* key, const void* value, void* context) {
fprintf(stderr, "Error writing IPv6 configuration\n");
}
}
if (oldAddrs != NULL) {
CFRelease(oldAddrs);
}

CFRelease(cfaddr);
CFRelease(cfprefixes);
CFRelease(cfdestaddrs);
CFRelease(cfflags);

CFRelease(addrArray);
CFRelease(prefixArray);
CFRelease(destArray);
CFRelease(flagsArray);
CFRelease(cfdev);

CFRelease(list);
CFRelease(dict);

CFRelease(ds);
CFRelease(key);

delete[] cfaddrs;
delete[] cfprefixes;
delete[] cfdestaddrs;
delete[] cfflags;
// for (unsigned int i = 0; i < SIZE; ++i) {
// values[i] = NULL;
// }

CFRelease(list);
CFRelease(dict);

return ret;
}
Expand Down

0 comments on commit 6540107

Please sign in to comment.