@@ -47,13 +47,8 @@ static inline bool __attribute__((pure)) valid_domain(const char *domain, const
47
47
if (domain == NULL || len == 0 || len > 255 )
48
48
return false;
49
49
50
- // Domain must not start or end with a hyphen or dot
51
- if (domain [ 0 ] == '-' || domain [ 0 ] == '.' ||
52
- domain [len - 1 ] == '-' || domain [len - 1 ] == '.' )
53
- return false;
54
-
55
50
// Loop over line and check for invalid characters
56
- unsigned int last_dot = 0 ;
51
+ int last_dot = -1 ;
57
52
for (unsigned int i = 0 ; i < len ; i ++ )
58
53
{
59
54
// Domain must not contain any character other than [a-zA-Z0-9.-_]
@@ -63,23 +58,14 @@ static inline bool __attribute__((pure)) valid_domain(const char *domain, const
63
58
(domain [i ] < '0' || domain [i ] > '9' ))
64
59
return false;
65
60
66
- // Multi-character checks
67
- if (i > 0 )
68
- {
69
- // Domain must not contain a hyphen immediately before a
70
- // dot or two consecutive dots
71
- if (domain [i ] == '.' && (domain [i - 1 ] == '-' || domain [i - 1 ] == '.' ))
72
- return false;
73
-
74
- // Domain must not contain a dot immediately before a
75
- // hyphen
76
- if (domain [i ] == '-' && domain [i - 1 ] == '.' )
77
- return false;
78
- }
79
-
80
61
// Individual label length check
81
62
if (domain [i ] == '.' )
82
63
{
64
+ // Label must be longer than 0 characters, i.e., two consecutive
65
+ // dots are not allowed
66
+ if (i - last_dot == 1 )
67
+ return false;
68
+
83
69
// Label must not be longer than 63 characters
84
70
// (actually 64 because the dot at the end of the label
85
71
// is included here)
@@ -95,9 +81,15 @@ static inline bool __attribute__((pure)) valid_domain(const char *domain, const
95
81
}
96
82
}
97
83
84
+ // TLD checks
85
+
98
86
// There must be at least two labels (i.e. one dot)
99
87
// e.g., "example.com" but not "localhost"
100
- if (last_dot == 0 )
88
+ if (last_dot == -1 )
89
+ return false;
90
+
91
+ // TLD must not start or end with a hyphen
92
+ if (domain [last_dot + 1 ] == '-' || domain [len - 1 ] == '-' )
101
93
return false;
102
94
103
95
// TLD length check
0 commit comments