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

Commit c6cfa85

Browse files
authored
Fix memory leaks in GetIpAddress() and CurlEnv. (#159)
Found with: --config=asan -c opt
1 parent 6e049b2 commit c6cfa85

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

opencensus/exporters/trace/zipkin/internal/zipkin_exporter.cc

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ void SerializeJson(const ::opencensus::trace::exporter::SpanData& span,
160160
writer->EndObject();
161161
}
162162

163-
// JSON encoding
164163
std::string EncodeJson(
165164
const std::vector<::opencensus::trace::exporter::SpanData>& spans,
166165
const ZipkinExporterOptions::Service& service) {
@@ -175,12 +174,9 @@ std::string EncodeJson(
175174
return buffer.GetString();
176175
}
177176

178-
std::string GetIpAddress(ZipkinExporterOptions::AddressFamily af_type) {
179-
struct ifaddrs* if_address_list;
180-
struct ifaddrs* if_address;
181-
182-
getifaddrs(&if_address_list);
183-
for (if_address = if_address_list; if_address != nullptr;
177+
std::string GetIpAddressHelper(ZipkinExporterOptions::AddressFamily af_type,
178+
ifaddrs* ifaddr_list) {
179+
for (ifaddrs* if_address = ifaddr_list; if_address != nullptr;
184180
if_address = if_address->ifa_next) {
185181
if (if_address->ifa_addr == nullptr) {
186182
continue;
@@ -218,12 +214,29 @@ std::string GetIpAddress(ZipkinExporterOptions::AddressFamily af_type) {
218214
return ipv6_loopback;
219215
}
220216

217+
std::string GetIpAddress(ZipkinExporterOptions::AddressFamily af_type) {
218+
ifaddrs* ifaddr;
219+
getifaddrs(&ifaddr);
220+
std::string out = GetIpAddressHelper(af_type, ifaddr);
221+
freeifaddrs(ifaddr);
222+
return out;
223+
}
224+
221225
class CurlEnv {
222226
public:
227+
static CurlEnv* Get();
228+
229+
private:
223230
CurlEnv() { curl_global_init(CURL_GLOBAL_DEFAULT); }
224231
~CurlEnv() { curl_global_cleanup(); }
225232
};
226233

234+
// static
235+
CurlEnv* CurlEnv::Get() {
236+
static CurlEnv* g_curl_env = new CurlEnv;
237+
return g_curl_env;
238+
}
239+
227240
CURLcode CurlSendMessage(const uint8_t* data,
228241
const ZipkinExporterOptions& options, size_t size,
229242
const struct curl_slist* headers, CURL* curl,
@@ -313,8 +326,6 @@ CURLcode CurlSendMessage(const uint8_t* data,
313326
return res;
314327
}
315328

316-
} // namespace
317-
318329
class ZipkinExportHandler
319330
: public ::opencensus::trace::exporter::SpanExporter::Handler {
320331
public:
@@ -338,7 +349,7 @@ void ZipkinExportHandler::SendMessage(const std::string& msg,
338349
struct curl_slist* headers = nullptr;
339350

340351
if (!curl) {
341-
// Failed to create curl handle.
352+
std::cerr << "Failed to create curl handle.\n";
342353
return;
343354
}
344355

@@ -348,7 +359,7 @@ void ZipkinExportHandler::SendMessage(const std::string& msg,
348359
CURLcode res = CurlSendMessage(reinterpret_cast<const uint8_t*>(msg.data()),
349360
options_, size, headers, curl, err_msg);
350361
if (res != CURLE_OK) {
351-
std::cerr << "curl error: " << curl_easy_strerror(res);
362+
std::cerr << "curl error: " << curl_easy_strerror(res) << "\n";
352363
}
353364

354365
curl_slist_free_all(headers);
@@ -363,9 +374,11 @@ void ZipkinExportHandler::Export(
363374
}
364375
}
365376

377+
} // namespace
378+
366379
void ZipkinExporter::Register(const ZipkinExporterOptions& options) {
367380
// Initialize libcurl. This MUST only be done once per process.
368-
static CurlEnv* curl_lib ABSL_ATTRIBUTE_UNUSED = new CurlEnv();
381+
CurlEnv::Get();
369382

370383
// Create new exporter.
371384
ZipkinExportHandler* handler = new ZipkinExportHandler(options);

0 commit comments

Comments
 (0)