Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 067e9847 authored by waynema's avatar waynema
Browse files

Use unique_ptr in dns_gethtbyaddr()

Use make_unique instead of malloc so we don't check for memory
allocation failures.

Test: atest resolv_integration_test
Change-Id: I43243b82b4998ba3674750417cd6b3c82083c8f9
parent 0cac8c6d
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -1144,7 +1144,7 @@ void DnsProxyListener::GetHostByAddrHandler::doDns64ReverseLookup(struct hostent
        queryLimiter.finish(uid);
        queryLimiter.finish(uid);
        if (*hpp) {
        if (*hpp) {
            // Replace IPv4 address with original queried IPv6 address in place. The space has
            // Replace IPv4 address with original queried IPv6 address in place. The space has
            // reserved by _dns_gethtbyaddr() and netbsd_gethostent_r() in
            // reserved by dns_gethtbyaddr() and netbsd_gethostent_r() in
            // system/netd/resolv/gethnamaddr.cpp.
            // system/netd/resolv/gethnamaddr.cpp.
            // Note that android_gethostbyaddrfornetcontext returns only one entry in result.
            // Note that android_gethostbyaddrfornetcontext returns only one entry in result.
            memcpy((*hpp)->h_addr_list[0], &v6addr, sizeof(v6addr));
            memcpy((*hpp)->h_addr_list[0], &v6addr, sizeof(v6addr));
+12 −19
Original line number Original line Diff line number Diff line
@@ -113,7 +113,7 @@ static void map_v4v6_hostent(struct hostent*, char**, char*);
static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
static void pad_v4v6_hostent(struct hostent* hp, char** bpp, char* ep);
static void addrsort(char**, int, res_state);
static void addrsort(char**, int, res_state);


static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
                           const android_net_context* netcontext, getnamaddr* info);
                           const android_net_context* netcontext, getnamaddr* info);
static int dns_gethtbyname(const char* name, int af, getnamaddr* info);
static int dns_gethtbyname(const char* name, int af, getnamaddr* info);


@@ -545,7 +545,7 @@ static int android_gethostbyaddrfornetcontext_real(const void* addr, socklen_t l
    info.buf = buf;
    info.buf = buf;
    info.buflen = buflen;
    info.buflen = buflen;
    if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
    if (_hf_gethtbyaddr(uaddr, len, af, &info)) {
        int error = _dns_gethtbyaddr(uaddr, len, af, netcontext, &info);
        int error = dns_gethtbyaddr(uaddr, len, af, netcontext, &info);
        if (error != 0) {
        if (error != 0) {
            return error;
            return error;
        }
        }
@@ -796,13 +796,11 @@ static int dns_gethtbyname(const char* name, int addr_type, getnamaddr* info) {
    return 0;
    return 0;
}
}


static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
static int dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
                           const android_net_context* netcontext, getnamaddr* info) {
                           const android_net_context* netcontext, getnamaddr* info) {
    char qbuf[MAXDNAME + 1], *qp, *ep;
    char qbuf[MAXDNAME + 1], *qp, *ep;
    int n;
    int n;
    struct hostent* hp;
    int advance;
    int advance;
    res_state res;


    info->hp->h_length = len;
    info->hp->h_length = len;
    info->hp->h_addrtype = af;
    info->hp->h_addrtype = af;
@@ -835,25 +833,20 @@ static int _dns_gethtbyaddr(const unsigned char* uaddr, int len, int af,
            return EAI_FAMILY;
            return EAI_FAMILY;
    }
    }


    querybuf* buf = (querybuf*) malloc(sizeof(querybuf));
    auto buf = std::make_unique<querybuf>();
    if (buf == NULL) {

        return EAI_MEMORY;
    res_state res = res_get_state();
    }
    if (!res) return EAI_MEMORY;
    res = res_get_state();

    if (res == NULL) {
        free(buf);
        return EAI_MEMORY;
    }
    res_setnetcontext(res, netcontext);
    res_setnetcontext(res, netcontext);
    int herrno = NETDB_INTERNAL;
    int herrno = NETDB_INTERNAL;
    n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf), &herrno);
    n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int) sizeof(buf->buf), &herrno);
    if (n < 0) {
    if (n < 0) {
        free(buf);
        LOG(DEBUG) << "res_nquery failed (" << n << ")";
        LOG(DEBUG) << "res_nquery failed (" << n << ")";
        return herrnoToAiErrno(herrno);
        return herrnoToAiErrno(herrno);
    }
    }
    hp = getanswer(buf, n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &herrno);
    hostent* hp =
    free(buf);
            getanswer(buf.get(), n, qbuf, T_PTR, res, info->hp, info->buf, info->buflen, &herrno);
    if (hp == NULL) {
    if (hp == NULL) {
        return herrnoToAiErrno(herrno);
        return herrnoToAiErrno(herrno);
    }
    }