]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add dns_name_empty() to skip counting all the labels in the name
authorOndřej Surý <ondrej@isc.org>
Wed, 12 Aug 2026 11:00:27 +0000 (13:00 +0200)
committerOndřej Surý <ondrej@isc.org>
Thu, 13 Aug 2026 18:59:52 +0000 (20:59 +0200)
Checking whether a name is empty via dns_name_countlabels() walks the
whole name just to compare the label count with zero.  An empty name
is simply one with zero length, so add dns_name_empty() that checks
the length directly, along with a coccinelle patch that converts the
existing callers.

cocci/dns_name_empty.spatch [new file with mode: 0644]
lib/dns/include/dns/name.h

diff --git a/cocci/dns_name_empty.spatch b/cocci/dns_name_empty.spatch
new file mode 100644 (file)
index 0000000..ae47b33
--- /dev/null
@@ -0,0 +1,23 @@
+@@
+constant ZERO =~ "^0[uUlL]*$";
+expression E;
+@@
+
+- dns_name_countlabels(E) > ZERO
++ !dns_name_empty(E)
+
+@@
+constant ZERO =~ "^0[uUlL]*$";
+expression E;
+@@
+
+- dns_name_countlabels(E) == ZERO
++ dns_name_empty(E)
+
+@@
+constant ZERO =~ "^0[uUlL]*$";
+expression E;
+@@
+
+- dns_name_countlabels(E) != ZERO
++ !dns_name_empty(E)
index db9752fc827eab908ee09a74cbc3ce4565575baa..60506f73e4e86893b7b03fd16d916ec8542bcb0e 100644 (file)
@@ -591,6 +591,20 @@ dns_name_offsets(const dns_name_t *name, dns_offsets_t offsets);
  *     individual labels in the name
  */
 
+static inline bool
+dns_name_empty(const dns_name_t *name) {
+       REQUIRE(DNS_NAME_VALID(name));
+
+       return name->length == 0;
+}
+/*%<
+ * Return whether the name is empty.
+ *
+ * Requires:
+ * \li 'name' is a valid name
+ *
+ */
+
 static inline uint8_t
 dns_name_countlabels(const dns_name_t *name) {
        REQUIRE(DNS_NAME_VALID(name));