From: Ondřej Surý Date: Wed, 12 Aug 2026 11:00:27 +0000 (+0200) Subject: Add dns_name_empty() to skip counting all the labels in the name X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7e350ac30d1a7372f1241d4a815bd15ecb9dc2b9;p=thirdparty%2Fbind9.git Add dns_name_empty() to skip counting all the labels in the name 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. --- diff --git a/cocci/dns_name_empty.spatch b/cocci/dns_name_empty.spatch new file mode 100644 index 00000000000..ae47b3326ca --- /dev/null +++ b/cocci/dns_name_empty.spatch @@ -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) diff --git a/lib/dns/include/dns/name.h b/lib/dns/include/dns/name.h index db9752fc827..60506f73e4e 100644 --- a/lib/dns/include/dns/name.h +++ b/lib/dns/include/dns/name.h @@ -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));