1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/parse_number.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_util.h"
9
10 namespace net {
11
12 namespace {
13
14 // The string to number conversion functions in //base include the type in the
15 // name (like StringToInt64()). The following wrapper methods create a
16 // consistent interface to StringToXXX() that calls the appropriate //base
17 // version. This simplifies writing generic code with a template.
18
StringToNumber(std::string_view input,int32_t * output)19 bool StringToNumber(std::string_view input, int32_t* output) {
20 // This assumes ints are 32-bits (will fail compile if that ever changes).
21 return base::StringToInt(input, output);
22 }
23
StringToNumber(std::string_view input,uint32_t * output)24 bool StringToNumber(std::string_view input, uint32_t* output) {
25 // This assumes ints are 32-bits (will fail compile if that ever changes).
26 return base::StringToUint(input, output);
27 }
28
StringToNumber(std::string_view input,int64_t * output)29 bool StringToNumber(std::string_view input, int64_t* output) {
30 return base::StringToInt64(input, output);
31 }
32
StringToNumber(std::string_view input,uint64_t * output)33 bool StringToNumber(std::string_view input, uint64_t* output) {
34 return base::StringToUint64(input, output);
35 }
36
SetError(ParseIntError error,ParseIntError * optional_error)37 bool SetError(ParseIntError error, ParseIntError* optional_error) {
38 if (optional_error)
39 *optional_error = error;
40 return false;
41 }
42
43 template <typename T>
ParseIntHelper(std::string_view input,ParseIntFormat format,T * output,ParseIntError * optional_error)44 bool ParseIntHelper(std::string_view input,
45 ParseIntFormat format,
46 T* output,
47 ParseIntError* optional_error) {
48 // Check that the input matches the format before calling StringToNumber().
49 // Numbers must start with either a digit or a negative sign.
50 if (input.empty())
51 return SetError(ParseIntError::FAILED_PARSE, optional_error);
52
53 bool is_non_negative = (format == ParseIntFormat::NON_NEGATIVE ||
54 format == ParseIntFormat::STRICT_NON_NEGATIVE);
55 bool is_strict = (format == ParseIntFormat::STRICT_NON_NEGATIVE ||
56 format == ParseIntFormat::STRICT_OPTIONALLY_NEGATIVE);
57
58 bool starts_with_negative = input[0] == '-';
59 bool starts_with_digit = base::IsAsciiDigit(input[0]);
60
61 if (!starts_with_digit) {
62 // The length() < 2 check catches "-". It's needed here to prevent reading
63 // beyond the end of the array on line 70.
64 if (is_non_negative || !starts_with_negative || input.length() < 2) {
65 return SetError(ParseIntError::FAILED_PARSE, optional_error);
66 }
67 // If the first digit after the negative is a 0, then either the number is
68 // -0 or it has an unnecessary leading 0. Either way, it violates the
69 // requirements of being "strict", so fail if strict.
70 if (is_strict && input[1] == '0') {
71 return SetError(ParseIntError::FAILED_PARSE, optional_error);
72 }
73 } else {
74 // Fail if the first character is a zero and the string has more than 1
75 // digit.
76 if (is_strict && input[0] == '0' && input.length() > 1) {
77 return SetError(ParseIntError::FAILED_PARSE, optional_error);
78 }
79 }
80
81 // Dispatch to the appropriate flavor of base::StringToXXX() by calling one of
82 // the type-specific overloads.
83 T result;
84 if (StringToNumber(input, &result)) {
85 *output = result;
86 return true;
87 }
88
89 // Optimization: If the error is not going to be inspected, don't bother
90 // calculating it.
91 if (!optional_error)
92 return false;
93
94 // Set an error that distinguishes between parsing/underflow/overflow errors.
95 //
96 // Note that the output set by base::StringToXXX() on failure cannot be used
97 // as it has ambiguity with parse errors.
98
99 // Strip any leading negative sign off the number.
100 std::string_view numeric_portion =
101 starts_with_negative ? input.substr(1) : input;
102
103 // Test if |numeric_portion| is a valid non-negative integer.
104 if (!numeric_portion.empty() &&
105 numeric_portion.find_first_not_of("0123456789") == std::string::npos) {
106 // If it was, the failure must have been due to underflow/overflow.
107 return SetError(starts_with_negative ? ParseIntError::FAILED_UNDERFLOW
108 : ParseIntError::FAILED_OVERFLOW,
109 optional_error);
110 }
111
112 // Otherwise it was a mundane parsing error.
113 return SetError(ParseIntError::FAILED_PARSE, optional_error);
114 }
115
116 } // namespace
117
ParseInt32(std::string_view input,ParseIntFormat format,int32_t * output,ParseIntError * optional_error)118 bool ParseInt32(std::string_view input,
119 ParseIntFormat format,
120 int32_t* output,
121 ParseIntError* optional_error) {
122 return ParseIntHelper(input, format, output, optional_error);
123 }
124
ParseInt64(std::string_view input,ParseIntFormat format,int64_t * output,ParseIntError * optional_error)125 bool ParseInt64(std::string_view input,
126 ParseIntFormat format,
127 int64_t* output,
128 ParseIntError* optional_error) {
129 return ParseIntHelper(input, format, output, optional_error);
130 }
131
ParseUint32(std::string_view input,ParseIntFormat format,uint32_t * output,ParseIntError * optional_error)132 bool ParseUint32(std::string_view input,
133 ParseIntFormat format,
134 uint32_t* output,
135 ParseIntError* optional_error) {
136 CHECK(format == ParseIntFormat::NON_NEGATIVE ||
137 format == ParseIntFormat::STRICT_NON_NEGATIVE);
138 return ParseIntHelper(input, format, output, optional_error);
139 }
140
ParseUint64(std::string_view input,ParseIntFormat format,uint64_t * output,ParseIntError * optional_error)141 bool ParseUint64(std::string_view input,
142 ParseIntFormat format,
143 uint64_t* output,
144 ParseIntError* optional_error) {
145 CHECK(format == ParseIntFormat::NON_NEGATIVE ||
146 format == ParseIntFormat::STRICT_NON_NEGATIVE);
147 return ParseIntHelper(input, format, output, optional_error);
148 }
149
150 } // namespace net
151