aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilippe Mathieu-Daudé <philmd@redhat.com>2021-05-07 15:32:12 +0200
committerMarc-André Lureau <marcandre.lureau@redhat.com>2021-05-07 18:38:27 +0400
commit6690d55626cc9dc4b90e614dece5918d281e9e1f (patch)
treeafd68c7add0e7ef3061edcf4d018c29aac58ec15
parent4e6444e842695a6bfb00e15a8d0edfceb5c4628d (diff)
downloadslirp-6690d55626cc9dc4b90e614dece5918d281e9e1f.zip
slirp-6690d55626cc9dc4b90e614dece5918d281e9e1f.tar.gz
slirp-6690d55626cc9dc4b90e614dece5918d281e9e1f.tar.bz2
Remove alloca() call in get_dns_addr_resolv_conf()
The ALLOCA(3) man-page mentions its "use is discouraged". For now get_dns_addr_resolv_conf() is called with pointer to a in_addr/in6_addr structure, and its size. Declare a union of these structures on the stack, able to hold both of them. This allows us to remove the alloca() call, keeping the buffer on the stack. Add an assertion in the unlikely case another inet address is handled by this function. Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com> Message-Id: <20210507133212.1952121-1-philmd@redhat.com>
-rw-r--r--src/slirp.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/src/slirp.c b/src/slirp.c
index ee4ce36..5d60cb5 100644
--- a/src/slirp.c
+++ b/src/slirp.c
@@ -249,9 +249,13 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
char buff2[257];
FILE *f;
int found = 0;
- void *tmp_addr = alloca(addrlen);
+ union {
+ struct in_addr dns_addr;
+ struct in6_addr dns6_addr;
+ } tmp_addr;
unsigned if_index;
+ assert(sizeof(tmp_addr) >= addrlen);
f = fopen("/etc/resolv.conf", "r");
if (!f)
return -1;
@@ -267,13 +271,13 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
if_index = 0;
}
- if (!inet_pton(af, buff2, tmp_addr)) {
+ if (!inet_pton(af, buff2, &tmp_addr)) {
continue;
}
/* If it's the first one, set it to dns_addr */
if (!found) {
- memcpy(pdns_addr, tmp_addr, addrlen);
- memcpy(cached_addr, tmp_addr, addrlen);
+ memcpy(pdns_addr, &tmp_addr, addrlen);
+ memcpy(cached_addr, &tmp_addr, addrlen);
if (scope_id) {
*scope_id = if_index;
}
@@ -285,7 +289,7 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
break;
} else if (slirp_debug & DBG_MISC) {
char s[INET6_ADDRSTRLEN];
- const char *res = inet_ntop(af, tmp_addr, s, sizeof(s));
+ const char *res = inet_ntop(af, &tmp_addr, s, sizeof(s));
if (!res) {
res = " (string conversion error)";
}