Free memory when it is no more used
Memory leak happen when Android data framework updates identical DNS
information to netd.
Bug: 115595601
Test: netd_unit_test, netd_integration_test
Change-Id: I97f22c131e823a03258b09311f4ef9d63d316ffd
diff --git a/resolv/res_cache.cpp b/resolv/res_cache.cpp
index a6bc6bf..4f02bfc 100644
--- a/resolv/res_cache.cpp
+++ b/resolv/res_cache.cpp
@@ -1776,9 +1776,8 @@
.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_flags = AI_NUMERICHOST};
int rt = getaddrinfo_numeric(servers[i], sbuf, hints, &nsaddrinfo[i]);
if (rt != 0) {
- for (unsigned j = 0; j < i; j++) {
+ for (int j = 0; j < i; j++) {
freeaddrinfo(nsaddrinfo[j]);
- nsaddrinfo[j] = NULL;
}
VLOG << __func__ << ": getaddrinfo_numeric(" << servers[i]
<< ") = " << gai_strerror(rt);
@@ -1821,14 +1820,19 @@
// max_samples actually change, in practice the overhead of checking is higher than the
// cost, and overflows are unlikely
++cache_info->revision_id;
- } else if (cache_info->params.max_samples != old_max_samples) {
- // If the maximum number of samples changes, the overhead of keeping the most recent
- // samples around is not considered worth the effort, so they are cleared instead. All
- // other parameters do not affect shared state: Changing these parameters does not
- // invalidate the samples, as they only affect aggregation and the conditions under
- // which servers are considered usable.
- res_cache_clear_stats_locked(cache_info);
- ++cache_info->revision_id;
+ } else {
+ if (cache_info->params.max_samples != old_max_samples) {
+ // If the maximum number of samples changes, the overhead of keeping the most recent
+ // samples around is not considered worth the effort, so they are cleared instead.
+ // All other parameters do not affect shared state: Changing these parameters does
+ // not invalidate the samples, as they only affect aggregation and the conditions
+ // under which servers are considered usable.
+ res_cache_clear_stats_locked(cache_info);
+ ++cache_info->revision_id;
+ }
+ for (int j = 0; j < numservers; j++) {
+ freeaddrinfo(nsaddrinfo[j]);
+ }
}
// Always update the search paths, since determining whether they actually changed is