Fix off-by-one error in res_cache.c
Change-Id: I58264902c123b3bc0d392d17837aa537ca0a3ca2
diff --git a/libc/dns/include/resolv_stats.h b/libc/dns/include/resolv_stats.h
index 2aab958..aaf6bd8 100644
--- a/libc/dns/include/resolv_stats.h
+++ b/libc/dns/include/resolv_stats.h
@@ -67,7 +67,7 @@
/* Returns an array of bools indicating which servers are considered good */
extern void
-_res_stats_get_usable_servers(const struct __res_params* params, struct __res_stats stats[MAXNS],
- int nscount, bool valid_servers[MAXNS]);
+_res_stats_get_usable_servers(const struct __res_params* params, struct __res_stats stats[],
+ int nscount, bool valid_servers[]);
#endif // _RES_STATS_H
diff --git a/libc/dns/resolv/res_cache.c b/libc/dns/resolv/res_cache.c
index 526c91b..15f9aa4 100644
--- a/libc/dns/resolv/res_cache.c
+++ b/libc/dns/resolv/res_cache.c
@@ -1228,15 +1228,18 @@
PendingReqInfo pending_requests;
} Cache;
+// The nameservers[], nsaddrinfo[] and nsstats[] are containing MAXNS + 1 elements, because the
+// number of nameservers is not known and the code relies on the n+1-st entry to be null to
+// recognize the end.
struct resolv_cache_info {
unsigned netid;
Cache* cache;
struct resolv_cache_info* next;
- char* nameservers[MAXNS +1];
+ char* nameservers[MAXNS + 1];
struct addrinfo* nsaddrinfo[MAXNS + 1];
int revision_id; // # times the nameservers have been replaced
struct __res_params params;
- struct __res_stats nsstats[MAXNS];
+ struct __res_stats nsstats[MAXNS + 1];
char defdname[256];
int dnsrch_offset[MAXDNSRCH+1]; // offsets into defdname
};
diff --git a/libc/dns/resolv/res_send.c b/libc/dns/resolv/res_send.c
index bfc6e1a..9ceeeb7 100644
--- a/libc/dns/resolv/res_send.c
+++ b/libc/dns/resolv/res_send.c
@@ -488,10 +488,10 @@
* Send request, RETRY times, or until successful.
*/
for (try = 0; try < statp->retry; try++) {
- struct __res_stats stats[MAXNS];
+ struct __res_stats stats[MAXNS + 1];
struct __res_params params;
int revision_id = _resolv_cache_get_resolver_stats(statp->netid, ¶ms, stats);
- bool usable_servers[MAXNS];
+ bool usable_servers[MAXNS + 1];
_res_stats_get_usable_servers(¶ms, stats, statp->nscount, usable_servers);
for (ns = 0; ns < statp->nscount; ns++) {
diff --git a/libc/dns/resolv/res_stats.c b/libc/dns/resolv/res_stats.c
index a3d6f36..b6f5ecb 100644
--- a/libc/dns/resolv/res_stats.c
+++ b/libc/dns/resolv/res_stats.c
@@ -164,8 +164,8 @@
}
void
-_res_stats_get_usable_servers(const struct __res_params* params, struct __res_stats stats[MAXNS],
- int nscount, bool usable_servers[MAXNS]) {
+_res_stats_get_usable_servers(const struct __res_params* params, struct __res_stats stats[],
+ int nscount, bool usable_servers[]) {
unsigned usable_servers_found = 0;
for (int ns = 0; ns < nscount; ns++) {
bool usable = _res_stats_usable_server(params, &stats[ns]);