]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Refactor tkey.c:buildquery() error handling
authorAram Sargsyan <aram@isc.org>
Thu, 11 Aug 2022 18:27:11 +0000 (18:27 +0000)
committerAram Sargsyan <aram@isc.org>
Tue, 16 Aug 2022 07:36:12 +0000 (07:36 +0000)
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.

lib/dns/tkey.c

index a46ac788782809cb54498b7cbb202f63ee684168..94be036681513390c4e0c8db84208f55dad9ad48 100644 (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