Skip to content

Commit 2b8df91

Browse files
committed
Return length from scan_ip functions
1 parent 8c896fe commit 2b8df91

File tree

5 files changed

+35
-72
lines changed

5 files changed

+35
-72
lines changed

src/generic/apl.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static really_inline int32_t scan_apl(
1414
{
1515
uint8_t negate = text[0] == '!';
1616
uint8_t digits[3];
17-
size_t count;
17+
int32_t count;
1818
uint32_t prefix;
1919
const uint8_t af_inet[2] = { 0x00, 0x01 }, af_inet6[2] = { 0x00, 0x02 };
2020

@@ -27,7 +27,7 @@ static really_inline int32_t scan_apl(
2727
if (size < 8)
2828
return -1;
2929
memcpy(octets, af_inet, sizeof(af_inet));
30-
if (scan_ip4(&text[negate+2], &octets[4], &count) == -1)
30+
if (!(count = scan_ip4(&text[negate+2], &octets[4])))
3131
return -1;
3232
count += negate + 2;
3333
digits[0] = (uint8_t)text[count+1] - '0';
@@ -47,7 +47,7 @@ static really_inline int32_t scan_apl(
4747
if (size < 20)
4848
return -1;
4949
memcpy(octets, af_inet6, sizeof(af_inet6));
50-
if (scan_ip6(&text[negate+2], &octets[4], &count) == -1)
50+
if (!(count = scan_ip6(&text[negate+2], &octets[4])))
5151
return -1;
5252
count += negate + 2;
5353
digits[0] = (uint8_t)text[count+1] - '0';

src/generic/ip4.h

+6-31
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#define IP4_H
1111

1212
nonnull_all
13-
static really_inline int32_t scan_ip4(
14-
const char *text, uint8_t *wire, size_t *length)
13+
static really_inline int32_t scan_ip4(const char *text, uint8_t *wire)
1514
{
1615
const char *start = text;
1716
uint32_t round = 0;
@@ -22,7 +21,7 @@ static really_inline int32_t scan_ip4(
2221
digits[1] = (uint8_t)text[1] - '0';
2322
digits[2] = (uint8_t)text[2] - '0';
2423
if (digits[0] > 9)
25-
return -1;
24+
return 0;
2625
else if (digits[1] > 9)
2726
(void)(text += 1), octet = digits[0];
2827
else if (digits[2] > 9)
@@ -31,17 +30,16 @@ static really_inline int32_t scan_ip4(
3130
(void)(text += 3), octet = digits[0] * 100 + digits[1] * 10 + digits[2];
3231

3332
if (octet > 255)
34-
return -1;
33+
return 0;
3534
wire[round++] = (uint8_t)octet;
3635
if (text[0] != '.' || round == 4)
3736
break;
3837
text += 1;
3938
}
4039

4140
if (round != 4)
42-
return -1;
43-
*length = (uintptr_t)text - (uintptr_t)start;
44-
return 4;
41+
return 0;
42+
return (int32_t)(text - start);
4543
}
4644

4745
nonnull_all
@@ -52,31 +50,8 @@ static really_inline int32_t parse_ip4(
5250
rdata_t *rdata,
5351
const token_t *token)
5452
{
55-
uint8_t *o = rdata->octets;
56-
const uint8_t *os = o;
57-
uint64_t n = 0;
58-
const char *p = token->data;
59-
static const uint8_t m[] = { 0, 0, 10, 100 };
60-
61-
*o = 0;
62-
for (const char *ps = p;; p++) {
63-
const uint64_t d = (uint8_t)*p - '0';
64-
if (d <= 9) {
65-
n = n * 10 + (uint8_t)d;
66-
} else {
67-
if (!(p - ps) || p - ps > 3 || n < m[(p - ps)] || n > 255 || o - os > 3)
68-
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
69-
ps = p + 1;
70-
*o++ = (uint8_t)n;
71-
if (*p != '.')
72-
break;
73-
n = 0;
74-
}
75-
}
76-
77-
if (p != token->data + token->length || o - os != 4)
53+
if ((size_t)scan_ip4(token->data, rdata->octets) != token->length)
7854
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
79-
8055
rdata->octets += 4;
8156
return 0;
8257
}

src/generic/ip6.h

+17-24
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
* inet_pton4(src, dst)
3636
* like inet_aton() but without all the hexadecimal and shorthand.
3737
* return:
38-
* 1 if `src' is a valid dotted quad, else 0.
38+
* length if `src' is a valid dotted quad, else 0.
3939
* notice:
40-
* does not touch `dst' unless it's returning 1.
40+
* does not touch `dst' unless it's returning !0.
4141
* author:
4242
* Paul Vixie, 1996.
4343
*/
@@ -59,11 +59,11 @@ inet_pton4(const char *src, uint8_t *dst)
5959
uint32_t new = *tp * 10 + (uint32_t)(pch - digits);
6060

6161
if (new > 255)
62-
return -1;
62+
return (0);
6363
*tp = (uint8_t)new;
6464
if (! saw_digit) {
6565
if (++octets > 4)
66-
return -1;
66+
return (0);
6767
saw_digit = 1;
6868
}
6969
} else if (ch == '.' && saw_digit) {
@@ -75,7 +75,7 @@ inet_pton4(const char *src, uint8_t *dst)
7575
break;
7676
}
7777
if (octets < 4)
78-
return -1;
78+
return (0);
7979

8080
memcpy(dst, tmp, NS_INADDRSZ);
8181
return (int)(src - start);
@@ -85,9 +85,9 @@ inet_pton4(const char *src, uint8_t *dst)
8585
* inet_pton6(src, dst)
8686
* convert presentation level address to network order binary form.
8787
* return:
88-
* 1 if `src' is a valid [RFC1884 2.2] address, else 0.
88+
* length if `src' is a valid [RFC1884 2.2] address, else 0.
8989
* notice:
90-
* (1) does not touch `dst' unless it's returning 1.
90+
* (1) does not touch `dst' unless it's returning !0.
9191
* (2) :: in a full address is silently ignored.
9292
* credit:
9393
* inspired by Mark Andrews.
@@ -112,7 +112,7 @@ inet_pton6(const char *src, uint8_t *dst)
112112
/* Leading :: requires some special handling. */
113113
if (*src == ':')
114114
if (*++src != ':')
115-
return -1;
115+
return (0);
116116
curtok = src;
117117
saw_xdigit = 0;
118118
val = 0;
@@ -125,7 +125,7 @@ inet_pton6(const char *src, uint8_t *dst)
125125
val <<= 4;
126126
val |= (pch - xdigits);
127127
if (val > 0xffff)
128-
return -1;
128+
return (0);
129129
saw_xdigit = 1;
130130
continue;
131131
}
@@ -156,7 +156,7 @@ inet_pton6(const char *src, uint8_t *dst)
156156
}
157157
if (saw_xdigit) {
158158
if (tp + NS_INT16SZ > endp)
159-
return -1;
159+
return (0);
160160
*tp++ = (uint8_t) (val >> 8) & 0xff;
161161
*tp++ = (uint8_t) val & 0xff;
162162
}
@@ -175,20 +175,15 @@ inet_pton6(const char *src, uint8_t *dst)
175175
tp = endp;
176176
}
177177
if (tp != endp)
178-
return -1;
178+
return (0);
179179
memcpy(dst, tmp, NS_IN6ADDRSZ);
180180
return (int)(src - start);
181181
}
182182

183183
nonnull_all
184-
static really_inline int32_t scan_ip6(
185-
const char *text, uint8_t *wire, size_t *length)
184+
static really_inline int32_t scan_ip6(const char *text, uint8_t *wire)
186185
{
187-
int len = inet_pton6(text, wire);
188-
if (len == -1)
189-
return -1;
190-
*length = (size_t)len;
191-
return 16;
186+
return inet_pton6(text, wire);
192187
}
193188

194189
nonnull_all
@@ -199,12 +194,10 @@ static really_inline int32_t parse_ip6(
199194
rdata_t *rdata,
200195
const token_t *token)
201196
{
202-
if (inet_pton6(token->data, rdata->octets) != -1) {
203-
rdata->octets += 16;
204-
return 0;
205-
}
206-
207-
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
197+
if ((size_t)inet_pton6(token->data, rdata->octets) != token->length)
198+
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(item), NAME(type));
199+
rdata->octets += 16;
200+
return 0;
208201
}
209202

210203
#endif // IP6_H

src/generic/svcb.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,15 @@ static int32_t parse_ipv4hint(
203203
(void)key;
204204
(void)param;
205205

206-
if (scan_ip4(t, rdata->octets, &n) == -1)
206+
if ((n = (size_t)scan_ip4(t, rdata->octets)) == 0)
207207
SYNTAX_ERROR(parser, "Invalid ipv4hint in %s", NAME(type));
208208
rdata->octets += 4;
209209
t += n;
210210

211211
while (*t == ',') {
212212
if (rdata->octets > rdata->limit)
213213
SYNTAX_ERROR(parser, "Invalid ipv4hint in %s", NAME(type));
214-
if (scan_ip4(t + 1, rdata->octets, &n) == -1)
214+
if ((n = (size_t)scan_ip4(t + 1, rdata->octets)) == 0)
215215
SYNTAX_ERROR(parser, "Invalid ipv4hint in %s", NAME(type));
216216
rdata->octets += 4;
217217
t += n + 1;
@@ -271,15 +271,15 @@ static int32_t parse_ipv6hint(
271271
(void)key;
272272
(void)param;
273273

274-
if (scan_ip6(t, rdata->octets, &n) == -1)
274+
if ((n = (size_t)scan_ip6(t, rdata->octets)) == 0)
275275
SYNTAX_ERROR(parser, "Invalid ipv6hint in %s", NAME(type));
276276
rdata->octets += 16;
277277
t += n;
278278

279279
while (*t == ',') {
280280
if (rdata->octets >= rdata->limit)
281281
SYNTAX_ERROR(parser, "Invalid ipv6hint in %s", NAME(type));
282-
if (scan_ip6(t + 1, rdata->octets, &n) == -1)
282+
if ((n = (size_t)scan_ip6(t + 1, rdata->octets)) == 0)
283283
SYNTAX_ERROR(parser, "Invalid ipv6hint in %s", NAME(type));
284284
rdata->octets += 16;
285285
t += n + 1;

src/westmere/ip4.h

+5-10
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,8 @@ static inline int sse_inet_aton(const char* ipv4_string, uint8_t* destination, s
151151

152152
uint32_t hash_key = (partition_mask * 0x00CF7800) >> 24;
153153
uint8_t hash_id = patterns_id[hash_key];
154-
if (hash_id >= 81) {
154+
if (hash_id >= 81)
155155
return 0;
156-
}
157156
const uint8_t* const pattern_ptr = &patterns[hash_id][0];
158157

159158
__m128i shuf = _mm_loadu_si128((const __m128i *)pattern_ptr);
@@ -181,14 +180,12 @@ static inline int sse_inet_aton(const char* ipv4_string, uint8_t* destination, s
181180
}
182181

183182
nonnull_all
184-
static really_inline int32_t scan_ip4(
185-
const char *text, uint8_t *wire, size_t *length)
183+
static really_inline int32_t scan_ip4(const char *text, uint8_t *wire)
186184
{
187185
size_t len;
188186
if (sse_inet_aton(text, wire, &len) != 1)
189-
return -1;
190-
*length = len;
191-
return 4;
187+
return 0;
188+
return (int32_t)len;
192189
}
193190

194191
nonnull_all
@@ -199,10 +196,8 @@ static really_inline int32_t parse_ip4(
199196
rdata_t *rdata,
200197
const token_t *token)
201198
{
202-
size_t length;
203-
204199
// Note that this assumes that reading up to token->data + 16 is safe (i.e., we do not cross a page).
205-
if (sse_inet_aton(token->data, rdata->octets, &length) != 1 || length != token->length)
200+
if ((size_t)scan_ip4(token->data, rdata->octets) != token->length)
206201
SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type));
207202
rdata->octets += 4;
208203
return 0;

0 commit comments

Comments
 (0)