[git commit] dhcpc: code shrink in good_hostname

Denys Vlasenko vda.linux at googlemail.com
Mon Jun 8 23:49:10 UTC 2020


commit: https://git.busybox.net/busybox/commit/?id=726d0d148b5a7a21eccaf8b17d2726f56ce2ce8f
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

Incorporated valid_domain_label into good_hostname to simplify the implementation.

function                                             old     new   delta
static.xmalloc_optname_optval                        973     958     -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-15)             Total: -15 bytes
   text	   data	    bss	    dec	    hex	filename
 993144	  16915	   1872	1011931	  f70db	busybox_old
 993129	  16915	   1872	1011916	  f70cc	busybox_unstripped

Signed-off-by: Martin Lewis <martin.lewis.x84 at gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 networking/udhcp/dhcpc.c | 62 +++++++++++-------------------------------------
 1 file changed, 14 insertions(+), 48 deletions(-)

diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index 5a1f8fd7a..6422181da 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -159,61 +159,27 @@ static int mton(uint32_t mask)
 }
 
 #if ENABLE_FEATURE_UDHCPC_SANITIZEOPT
-/* Check if a given label represents a valid DNS label
- * Return pointer to the first character after the label
- * (NUL or dot) upon success, NULL otherwise.
- * See RFC1035, 2.3.1
- */
+/* Check if a given name represents a valid DNS name */
+/* See RFC1035, 2.3.1 */
 /* We don't need to be particularly anal. For example, allowing _, hyphen
  * at the end, or leading and trailing dots would be ok, since it
- * can't be used for attacks. (Leading hyphen can be, if someone uses
- * cmd "$hostname"
+ * can't be used for attacks. (Leading hyphen can be, if someone uses cmd "$hostname"
  * in the script: then hostname may be treated as an option)
  */
-static const char *valid_domain_label(const char *label)
-{
-	unsigned char ch;
-	//unsigned pos = 0;
-
-	if (label[0] == '-')
-		return NULL;
-	for (;;) {
-		ch = *label;
-		if ((ch|0x20) < 'a' || (ch|0x20) > 'z') {
-			if (ch < '0' || ch > '9') {
-				if (ch == '\0' || ch == '.')
-					return label;
-				/* DNS allows only '-', but we are more permissive */
-				if (ch != '-' && ch != '_')
-					return NULL;
-			}
-		}
-		label++;
-		//pos++;
-		//Do we want this?
-		//if (pos > 63) /* NS_MAXLABEL; labels must be 63 chars or less */
-		//	return NULL;
-	}
-}
-
-/* Check if a given name represents a valid DNS name */
-/* See RFC1035, 2.3.1 */
 static int good_hostname(const char *name)
 {
-	//const char *start = name;
-
-	for (;;) {
-		name = valid_domain_label(name);
-		if (!name)
-			return 0;
-		if (!name[0])
-			return 1;
-			//Do we want this?
-			//return ((name - start) < 1025); /* NS_MAXDNAME */
-		name++;
-		if (*name == '\0')
-			return 1; // We allow trailing dot too
+	if (*name == '-') /* Can't start with '-' */
+		return 0;
+
+	while (*name) {
+		unsigned char ch = *name++;
+		if (!isalnum(ch))
+			/* DNS allows only '-', but we are more permissive */
+			if (ch != '-' && ch != '_' && ch != '.')
+				return 0;
+		// TODO: do we want to validate lengths against NS_MAXLABEL and NS_MAXDNAME?
 	}
+	return 1;
 }
 #else
 # define good_hostname(name) 1


More information about the busybox-cvs mailing list