]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add small tweaks to the code to fix compilation when ISC assertions are disabled
authorOndřej Surý <ondrej@sury.org>
Mon, 4 Jun 2018 11:41:09 +0000 (13:41 +0200)
committerOndřej Surý <ondrej@sury.org>
Thu, 8 Nov 2018 05:21:53 +0000 (12:21 +0700)
While implementing the new unit testing framework cmocka, it was found that the
BIND 9 code doesn't compile when assertions are disabled or replaced with any
function (such as mock_assert() from cmocka unit testing framework) that's not
directly recognized as assertion by the compiler.

This made the compiler to complain about blocks of code that was recognized as
unreachable before, but now it isn't.

The changes in this commit include:

* assigns default values to couple of local variables,
* moves some return statements around INSIST assertions,
* adds __builtin_unreachable(); annotations after some INSIST assertions,
* fixes one broken assertion (= instead of ==)

16 files changed:
bin/dig/dighost.c
bin/named/server.c
bin/named/zoneconf.c
bin/tests/system/rpz/dnsrps.c
config.h.in
configure
configure.ac
lib/dns/dnstap.c
lib/dns/iptable.c
lib/dns/master.c
lib/dns/rpz.c
lib/isc/include/isc/util.h
lib/isc/pk11.c
lib/isc/random.c
lib/ns/client.c
lib/ns/query.c

index 416a3eaa5cfb3258efdaeed21c809ce0e7ae4c56..daf7a923dd453640aaae07d9cfcfb102621d24d8 100644 (file)
@@ -2291,7 +2291,7 @@ setup_lookup(dig_lookup_t *lookup) {
 
                if (lookup->ecs_addr != NULL) {
                        uint8_t addr[16];
-                       uint16_t family;
+                       uint16_t family = 0;
                        uint32_t plen;
                        struct sockaddr *sa;
                        struct sockaddr_in *sin;
index 4f14900f7fbe162324d4d6d2566a72214e485961..93924b2afeb603a590e2958731d80f0f823babca 100644 (file)
@@ -2718,7 +2718,7 @@ catz_create_chg_task(dns_catz_entry_t *entry, dns_catz_zone_t *origin,
        catz_chgzone_event_t *event;
        isc_task_t *task;
        isc_result_t result;
-       isc_taskaction_t action;
+       isc_taskaction_t action = NULL;
 
        switch (type) {
        case DNS_EVENT_CATZADDZONE:
@@ -4356,7 +4356,7 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
                obj2 = cfg_tuple_get(obj, "response");
                if (!cfg_obj_isvoid(obj2)) {
                        const char *resp = cfg_obj_asstring(obj2);
-                       isc_result_t r;
+                       isc_result_t r = DNS_R_SERVFAIL;
 
                        if (strcasecmp(resp, "drop") == 0)
                                r = DNS_R_DROP;
@@ -5036,7 +5036,7 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
        obj2 = cfg_tuple_get(obj, "response");
        if (!cfg_obj_isvoid(obj2)) {
                const char *resp = cfg_obj_asstring(obj2);
-               isc_result_t r;
+               isc_result_t r = DNS_R_SERVFAIL;
 
                if (strcasecmp(resp, "drop") == 0)
                        r = DNS_R_DROP;
@@ -5281,7 +5281,7 @@ configure_view(dns_view_t *view, dns_viewlist_t *viewlist,
                const char *empty_dbtype[4] =
                                    { "_builtin", "empty", NULL, NULL };
                int empty_dbtypec = 4;
-               dns_zonestat_level_t statlevel;
+               dns_zonestat_level_t statlevel = dns_zonestat_none;
 
                name = dns_fixedname_initname(&fixed);
 
@@ -9920,7 +9920,7 @@ static void
 named_server_reload(isc_task_t *task, isc_event_t *event) {
        named_server_t *server = (named_server_t *)event->ev_arg;
 
-       INSIST(task = server->task);
+       INSIST(task == server->task);
        UNUSED(task);
 
        isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
@@ -12586,7 +12586,7 @@ newzone_parse(named_server_t *server, char *command, dns_view_t **viewp,
        const char *viewname = NULL;
        dns_rdataclass_t rdclass;
        dns_view_t *view = NULL;
-       const char *bn;
+       const char *bn = NULL;
 
        REQUIRE(viewp != NULL && *viewp == NULL);
        REQUIRE(zoneobjp != NULL && *zoneobjp == NULL);
index 52264239e09a8b4884d22853a815bd0c4343041e..f01004a8ba0b17fff98390555d63eef3dccd69c5 100644 (file)
@@ -903,7 +903,7 @@ named_zone_configure(const cfg_obj_t *config, const cfg_obj_t *vconfig,
        const dns_master_style_t *masterstyle = &dns_master_style_default;
        isc_stats_t *zoneqrystats;
        dns_stats_t *rcvquerystats;
-       dns_zonestat_level_t statlevel;
+       dns_zonestat_level_t statlevel = dns_zonestat_none;
        int seconds;
        dns_zone_t *mayberaw = (raw != NULL) ? raw : zone;
        isc_dscp_t dscp;
index 06da69144d10412fd0e47d2e95e278388ce5d6ab..0ad3add41a3fb2aed4a895affb83e321d44c4ee9 100644 (file)
@@ -82,10 +82,11 @@ main(int argc, char **argv) {
                        }
 #ifdef USE_DNSRPS
                        printf("%s\n", librpz->dnsrpzd_path);
-                       return (0);
 #else
                        INSIST(0);
+                       ISC_UNREACHABLE();
 #endif
+                       return (0);
 
                case 'n':
                        if (!link_dnsrps(&emsg)) {
@@ -132,10 +133,11 @@ main(int argc, char **argv) {
                        librpz->rsp_detach(&rsp);
                        librpz->client_detach(&client);
                        printf("%u\n", serial);
-                       return (0);
 #else
                        INSIST(0);
+                       ISC_UNREACHABLE();
 #endif
+                       return (0);
 
                case 'w':
                        seconds = strtod(optarg, &p);
index 8fccd17b65e0bfbd7ef2f7d768336470ff34c85e..0ac0deb2f7077f10053c6188233317cabe774765 100644 (file)
@@ -60,6 +60,9 @@
 /* Define to 1 if the compiler supports __builtin_expect. */
 #undef HAVE_BUILTIN_EXPECT
 
+/* define if the compiler supports __builtin_unreachable(). */
+#undef HAVE_BUILTIN_UNREACHABLE
+
 /* Define to 1 if you have the `catgets' function. */
 #undef HAVE_CATGETS
 
index 90cffa9ec42b33091d93544bbff156314374e662..5576d75ccb22c8713179d45077ab2fcb1166aeb5 100755 (executable)
--- a/configure
+++ b/configure
 done
 
 
+#
+# Check for __builtin_unreachable
+#
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking compiler support for __builtin_unreachable()" >&5
+$as_echo_n "checking compiler support for __builtin_unreachable()... " >&6; }
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main ()
+{
+__builtin_unreachable();
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_c_try_link "$LINENO"; then :
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+
+$as_echo "#define HAVE_BUILTIN_UNREACHABLE 1" >>confdefs.h
+
+
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+fi
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+
 #
 # Check for __builtin_expect
 #
index d100ae29acfdc8f3df763de8831d583d464eb141..55e65150bd7b39af03216f63c14904a8e8e50993 100644 (file)
@@ -2017,6 +2017,21 @@ LIBS="$LIBS $ISC_ATOMIC_LIBS"
 
 AC_CHECK_HEADERS([uchar.h])
 
+#
+# Check for __builtin_unreachable
+#
+AC_MSG_CHECKING([compiler support for __builtin_unreachable()])
+AC_LINK_IFELSE(
+  [AC_LANG_PROGRAM(
+     [[]],
+     [[__builtin_unreachable();]]
+   )],
+  [AC_MSG_RESULT([yes])
+   AC_DEFINE([HAVE_BUILTIN_UNREACHABLE], [1], [define if the compiler supports __builtin_unreachable().])
+  ],
+  [AC_MSG_RESULT([no])
+  ])
+
 #
 # Check for __builtin_expect
 #
index b9b26580d6e8629492a655ce3d83dd60a106c52c..3368d9010523d2e1cbdd1fb12cf111863c70b0b7 100644 (file)
@@ -700,6 +700,7 @@ dnstap_type(dns_dtmsgtype_t msgtype) {
                return (DNSTAP__MESSAGE__TYPE__UPDATE_RESPONSE);
        default:
                INSIST(0);
+               ISC_UNREACHABLE();
        }
 }
 
index 36bad9bf587e30adedf16b760921a64bf8773691..c4e3b0d06ff795cbbc6b527c7d8c14fb622322e4 100644 (file)
@@ -67,7 +67,7 @@ dns_iptable_addprefix(dns_iptable_t *tab, const isc_netaddr_t *addr,
        int i;
 
        INSIST(DNS_IPTABLE_VALID(tab));
-       INSIST(tab->radix);
+       INSIST(tab->radix != NULL);
 
        NETADDR_TO_PREFIX_T(addr, pfx, bitlen);
 
index c4025832cfe9f634cc54a926e02371350b450a5a..8b39beca3b7991a0a571aaa3e7314721b5698f4c 100644 (file)
@@ -549,8 +549,6 @@ loadctx_create(dns_masterformat_t format, isc_mem_t *mctx,
 
        lctx->format = format;
        switch (format) {
-       default:
-               INSIST(0);
        case dns_masterformat_text:
                lctx->openfile = openfile_text;
                lctx->load = load_text;
@@ -563,6 +561,9 @@ loadctx_create(dns_masterformat_t format, isc_mem_t *mctx,
                lctx->openfile = openfile_map;
                lctx->load = load_map;
                break;
+       default:
+               INSIST(0);
+               ISC_UNREACHABLE();
        }
 
        if (lex != NULL) {
index 00751923e2b0fce3716b06e90c619c17cb10c827..7a70ce2b4d146b4c380b7dc95d25486e17f0cc40 100644 (file)
@@ -567,8 +567,8 @@ adj_trigger_cnt(dns_rpz_zones_t *rpzs, dns_rpz_num_t rpz_num,
                const dns_rpz_cidr_key_t *tgt_ip, dns_rpz_prefix_t tgt_prefix,
                bool inc)
 {
-       dns_rpz_trigger_counter_t *cnt;
-       dns_rpz_zbits_t *have;
+       dns_rpz_trigger_counter_t *cnt = 0;
+       dns_rpz_zbits_t *have = 0;
 
        switch (rpz_type) {
        case DNS_RPZ_TYPE_CLIENT_IP:
@@ -611,6 +611,7 @@ adj_trigger_cnt(dns_rpz_zones_t *rpzs, dns_rpz_num_t rpz_num,
                break;
        default:
                INSIST(0);
+               ISC_UNREACHABLE();
        }
 
        if (inc) {
@@ -2477,7 +2478,7 @@ dns_rpz_find_ip(dns_rpz_zones_t *rpzs, dns_rpz_type_t rpz_type,
        dns_rpz_addr_zbits_t tgt_set;
        dns_rpz_cidr_node_t *found;
        isc_result_t result;
-       dns_rpz_num_t rpz_num;
+       dns_rpz_num_t rpz_num = 0;
        dns_rpz_have_t have;
        int i;
 
index 2343bebd97f8240656434c4922f1bde006a43bf9..6c5c32dee45ff0312960286255cb867bc3bdf2c6 100644 (file)
  */
 #include <isc/likely.h>
 
+#ifdef HAVE_BUILTIN_UNREACHABLE
+#define ISC_UNREACHABLE() __builtin_unreachable();
+#else
+#define ISC_UNREACHABLE()
+#endif
+
 #ifdef UNIT_TESTING
 extern void mock_assert(const int result, const char* const expression,
                        const char * const file, const int line);
index 6b77ed9fea3191cbce3dc8733abc8fe2f6b6f054..c405e61608d89808071d3cc36ca2d59b6388c649 100644 (file)
@@ -667,6 +667,7 @@ pk11_numbits(CK_BYTE_PTR data, unsigned int bytecnt) {
                break;
        }
        INSIST(0);
+       ISC_UNREACHABLE();
 }
 
 CK_ATTRIBUTE *
index c682cbc3f76536ccf1be1707c6c1996636ff928b..54a60a072eb11229d25bf458a903b4363491e4da 100644 (file)
@@ -114,7 +114,7 @@ isc_random_buf(void *buf, size_t buflen) {
        int i;
        uint32_t r;
 
-       REQUIRE(buf);
+       REQUIRE(buf != NULL);
        REQUIRE(buflen > 0);
 
        RUNTIME_CHECK(isc_once_do(&isc_random_once,
index 7df2336c1b9a9bba6e8d74ef9df1b5f34fb9162b..6a8d2084989a6568e45c4946bb6d096cbbcf48b5 100644 (file)
@@ -1686,7 +1686,7 @@ ns_client_addopt(ns_client_t *client, dns_message_t *message,
                isc_buffer_t buf;
                uint8_t addr[16];
                uint32_t plen, addrl;
-               uint16_t family;
+               uint16_t family = 0;
 
                /* Add CLIENT-SUBNET option. */
 
index 937da5d68e32eaa5154f5f43244709b4da4ec8af..71aa3824d9ca8f3afa3f0d6f0a8cae88af4ed9eb 100644 (file)
@@ -2587,7 +2587,7 @@ rpz_get_zbits(ns_client_t *client,
              dns_rdatatype_t ip_type, dns_rpz_type_t rpz_type)
 {
        dns_rpz_st_t *st;
-       dns_rpz_zbits_t zbits;
+       dns_rpz_zbits_t zbits = 0;
 
        REQUIRE(client != NULL);
        REQUIRE(client->query.rpz_st != NULL);