Skip to content

Commit f956a4b

Browse files
committed
net: Add branch to Parser::read_number for parsing without checked
arithmetic If `max_digits.is_some()`, then we know we are parsing a `u8` or `u16` because `read_number` is only called with `Some(3)` or `Some(4)`. Both types fit well within a `u32` without risk of overflow. Thus, we can use plain arithmetic to avoid extra instructions from `checked_mul` and `checked_add`.
1 parent 3406ada commit f956a4b

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

library/core/src/net/parser.rs

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This module is "publicly exported" through the `FromStr` implementations
44
//! below.
55
6-
use crate::convert::TryInto;
6+
use crate::convert::{TryFrom, TryInto};
77
use crate::error::Error;
88
use crate::fmt;
99
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
@@ -104,36 +104,62 @@ impl<'a> Parser<'a> {
104104
// Read a number off the front of the input in the given radix, stopping
105105
// at the first non-digit character or eof. Fails if the number has more
106106
// digits than max_digits or if there is no number.
107-
fn read_number<T: ReadNumberHelper>(
107+
//
108+
// INVARIANT: `max_digits` must be less than or equal to the number of
109+
// digits that `u32` can represent.
110+
fn read_number<T: ReadNumberHelper + TryFrom<u32>>(
108111
&mut self,
109112
radix: u32,
110113
max_digits: Option<usize>,
111114
allow_zero_prefix: bool,
112115
) -> Option<T> {
113-
self.read_atomically(move |p| {
114-
let mut result = T::ZERO;
115-
let mut digit_count = 0;
116-
let has_leading_zero = p.peek_char() == Some('0');
117-
118-
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
119-
result = result.checked_mul(radix)?;
120-
result = result.checked_add(digit)?;
121-
digit_count += 1;
122-
if let Some(max_digits) = max_digits {
116+
// If max_digits.is_some(), then we are parsing a `u8` or `u16` and
117+
// don't need to use checked arithmetic since it fits within a `u32`.
118+
if let Some(max_digits) = max_digits {
119+
self.read_atomically(move |p| {
120+
let mut result = 0_u32;
121+
let mut digit_count = 0;
122+
let has_leading_zero = p.peek_char() == Some('0');
123+
124+
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
125+
result *= radix;
126+
result += digit;
127+
digit_count += 1;
128+
123129
if digit_count > max_digits {
124130
return None;
125131
}
126132
}
127-
}
128133

129-
if digit_count == 0 {
130-
None
131-
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
132-
None
133-
} else {
134-
Some(result)
135-
}
136-
})
134+
if digit_count == 0 {
135+
None
136+
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
137+
None
138+
} else {
139+
result.try_into().ok()
140+
}
141+
})
142+
} else {
143+
self.read_atomically(move |p| {
144+
let mut result = T::ZERO;
145+
let mut digit_count = 0;
146+
let has_leading_zero = p.peek_char() == Some('0');
147+
148+
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
149+
result = result.checked_mul(radix)?;
150+
result = result.checked_add(digit)?;
151+
digit_count += 1;
152+
}
153+
154+
if digit_count == 0 {
155+
None
156+
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
157+
None
158+
} else {
159+
Some(result)
160+
}
161+
})
162+
}
137163
}
138164

139165
/// Read an IPv4 address.

0 commit comments

Comments
 (0)