Refactor tkey.c:buildquery() error handling

After an earlier code cleanup, `dns_rdatalist_tordataset()` always
succeeds, so the `RETERR` error handling macro below the function
call was removed. After that change the `dynbuf` variable can never
be `NULL` in the error handling code path under the `failure` label.

    *** CID 355779:  Null pointer dereferences  (REVERSE_INULL)
    /lib/dns/tkey.c: 997 in buildquery()
    991                 dns_message_puttempname(msg, &aname);
    992         }
    993         if (question != NULL) {
    994                 dns_rdataset_disassociate(question);
    995                 dns_message_puttemprdataset(msg, &question);
    996         }
    >>>     CID 355779:  Null pointer dereferences  (REVERSE_INULL)
    >>>     Null-checking "dynbuf" suggests that it may be null, but it has already been dereferenced on all paths leading to the check.
    997         if (dynbuf != NULL) {
    998                 isc_buffer_free(&dynbuf);
    999         }
    1000        return (result);
    1001     }
    1002

Refactor the `buildquery()` function to simplify its error handling.
This commit is contained in:
Aram Sargsyan 2022-08-11 18:27:11 +00:00
parent 5604d942fb
commit 2e01162258

View file

@ -940,6 +940,18 @@ buildquery(dns_message_t *msg, const dns_name_t *name, dns_rdata_tkey_t *tkey,
REQUIRE(name != NULL);
REQUIRE(tkey != NULL);
len = 16 + tkey->algorithm.length + tkey->keylen + tkey->otherlen;
isc_buffer_allocate(msg->mctx, &dynbuf, len);
dns_message_gettemprdata(msg, &rdata);
result = dns_rdata_fromstruct(rdata, dns_rdataclass_any,
dns_rdatatype_tkey, tkey, dynbuf);
if (result != ISC_R_SUCCESS) {
dns_message_puttemprdata(msg, &rdata);
isc_buffer_free(&dynbuf);
return (result);
}
dns_message_takebuffer(msg, &dynbuf);
dns_message_gettempname(msg, &qname);
dns_message_gettempname(msg, &aname);
@ -947,14 +959,6 @@ buildquery(dns_message_t *msg, const dns_name_t *name, dns_rdata_tkey_t *tkey,
dns_rdataset_makequestion(question, dns_rdataclass_any,
dns_rdatatype_tkey);
len = 16 + tkey->algorithm.length + tkey->keylen + tkey->otherlen;
isc_buffer_allocate(msg->mctx, &dynbuf, len);
dns_message_gettemprdata(msg, &rdata);
RETERR(dns_rdata_fromstruct(rdata, dns_rdataclass_any,
dns_rdatatype_tkey, tkey, dynbuf));
dns_message_takebuffer(msg, &dynbuf);
dns_message_gettemprdatalist(msg, &tkeylist);
tkeylist->rdclass = dns_rdataclass_any;
tkeylist->type = dns_rdatatype_tkey;
@ -982,25 +986,6 @@ buildquery(dns_message_t *msg, const dns_name_t *name, dns_rdata_tkey_t *tkey,
}
return (ISC_R_SUCCESS);
failure:
if (qname != NULL) {
dns_message_puttempname(msg, &qname);
}
if (aname != NULL) {
dns_message_puttempname(msg, &aname);
}
if (question != NULL) {
dns_rdataset_disassociate(question);
dns_message_puttemprdataset(msg, &question);
}
if (dynbuf != NULL) {
isc_buffer_free(&dynbuf);
}
if (rdata != NULL) {
dns_message_puttemprdata(msg, &rdata);
}
return (result);
}
isc_result_t