Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(567)

Side by Side Diff: net/base/parse_number.h

Issue 1828103002: Extend net/base/parse_number.h for parsing of negative numbers, and determining if there was overflo (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@proxy_num
Patch Set: remove http_response_headers changes (moving to other CL) Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/base/ip_address.cc ('k') | net/base/parse_number.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef NET_BASE_PARSE_NUMBER_H_ 5 #ifndef NET_BASE_PARSE_NUMBER_H_
6 #define NET_BASE_PARSE_NUMBER_H_ 6 #define NET_BASE_PARSE_NUMBER_H_
7 7
8 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
9 #include "base/strings/string_piece.h" 9 #include "base/strings/string_piece.h"
10 #include "net/base/net_export.h" 10 #include "net/base/net_export.h"
11 11
12 // This file contains utility functions for parsing numbers, in the context of 12 // This file contains utility functions for parsing numbers, in the context of
13 // network protocols. 13 // network protocols.
14 // 14 //
15 // Q: Doesn't //base already provide these in string_number_conversions.h, with 15 // Q: Doesn't //base already provide these in string_number_conversions.h, with
16 // functions like base::StringToInt()? 16 // functions like base::StringToInt()?
17 // 17 //
18 // A: Yes, and those functions are used under the hood by these 18 // A: Yes, and those functions are used under the hood by these
19 // implementations. 19 // implementations.
20 // 20 //
21 // However using the number parsing functions from //base directly in network 21 // However using the number parsing functions from //base directly in network
22 // code can lead to subtle bugs, as the //base versions are more permissive. 22 // code can lead to subtle bugs, as the //base versions are more permissive.
23 // For instance "+99" is successfully parsed by base::StringToInt(). 23 // For instance "+99" is successfully parsed by base::StringToInt().
24 // 24 //
25 // However in the majority of places in //net, a leading plus on a number 25 // However in the majority of places in //net, a leading plus on a number
26 // should be considered invalid. For instance when parsing a host:port pair 26 // should be considered invalid. For instance when parsing a host:port pair
27 // you wouldn't want to recognize "foo:+99" as having a port of 99. The same 27 // you wouldn't want to recognize "foo:+99" as having a port of 99. The same
28 // issue applies when parsing a content-length header. 28 // issue applies when parsing a content-length header.
29 // 29 //
30 // Another difficulty with using base::StringToInt() is distinguishing
31 // overflow/underflow from parsing failures.
32 //
30 // To reduce the risk of such problems, use of these functions over the 33 // To reduce the risk of such problems, use of these functions over the
31 // //base versions. 34 // //base versions.
32 35
33 class GURL; 36 class GURL;
34 37
35 namespace net { 38 namespace net {
36 39
37 // Parses a string representing a decimal number to an |int|. Returns true on 40 // Possible return values from ParseInteger().
38 // success, and fills |*output| with the result. Note that |*output| is not 41 enum class ParseIntegerResult {
39 // modified on failure. 42 // Success!
43 OK,
44
45 // The parsed number couldn't fit into the provided output type because it was
46 // too large.
47 FAILED_OVERFLOW,
48
49 // The parsed number couldn't fit into the provided output type because it was
50 // too small (negative).
51 FAILED_UNDERFLOW,
52
53 // The number failed to be parsed because it wasn't a valid decimal number.
54 // This includes the case where the number was negative but the sign-policy
55 // requested a non-negative number. Since in this domain a leading '-' is
56 // never accepted, even if the end result was non-negative (like "-0").
57 FAILED_PARSE,
mmenke 2016/03/24 21:41:48 I'm a bit wary of these multiple-failure-value fun
eroman 2016/03/24 22:12:16 That is a valid concern. One idea would be to uni
58 };
59
60 // The policy regarding negative numbers.
61 enum class ParseIntegerSignPolicy {
62 // Reject any number is negative. This includes "-0". Another way to think
63 // about this policy is that it matches only 1*DIGIT.
64 NON_NEGATIVE,
65
66 // Accept both positive and negative numbers, so long as they fit into the
67 // output type.
68 //
69 // TODO(eroman): There is however a quirk in that "-0" is acepted when the
70 // output type is signed, but is rejected when the output type is unsigned
71 // (failing with an underflow).
72 ANY,
73 };
74
75 // ParseInteger() parses a string representing a decimal number.
76 // Returns true on success, and fills |*output| with the result:
40 // 77 //
41 // Recognized inputs take the form: 78 // When sign_policy=NON_NEGATIVE it accepts inputs of the form:
79 //
42 // 1*DIGIT 80 // 1*DIGIT
43 // 81 //
44 // Where DIGIT is an ASCII number in the range '0' - '9' 82 // Whereas when sign_policy=ANY it accepts inputs of the form:
45 // 83 //
46 // Note that: 84 // ("" | "-") 1*DIGIT
85 //
86 // Where DIGIT is an ASCII number in the range '0' - '9'
87 //
88 // Note that:
47 // * Parsing is locale independent 89 // * Parsing is locale independent
90 // * |*output| is never modified on failure
48 // * Leading zeros are allowed (numbers needn't be in minimal encoding) 91 // * Leading zeros are allowed (numbers needn't be in minimal encoding)
49 // * Inputs that would overflow the output are rejected. 92 // * Zero can be expressed as a negative or positive quantity.
50 // * Only accepts integers 93 // TODO(eroman): However -0 is rejected when the output type is unsigned.
51 // 94 NET_EXPORT ParseIntegerResult ParseInteger(const base::StringPiece& input,
52 // Examples of recognized inputs are: 95 ParseIntegerSignPolicy sign_policy,
53 // "13" 96 int32_t* output) WARN_UNUSED_RESULT;
54 // "0" 97
55 // "00013" 98 NET_EXPORT ParseIntegerResult ParseInteger(const base::StringPiece& input,
56 // 99 ParseIntegerSignPolicy sign_policy,
57 // Examples of rejected inputs are: 100 uint32_t* output) WARN_UNUSED_RESULT;
58 // " 13" 101
59 // "-13" 102 NET_EXPORT ParseIntegerResult ParseInteger(const base::StringPiece& input,
60 // "+13" 103 ParseIntegerSignPolicy sign_policy,
61 // "0x15" 104 int64_t* output) WARN_UNUSED_RESULT;
62 // "13.3" 105
63 NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input, 106 NET_EXPORT ParseIntegerResult ParseInteger(const base::StringPiece& input,
64 int* output) WARN_UNUSED_RESULT; 107 ParseIntegerSignPolicy sign_policy,
108 uint64_t* output) WARN_UNUSED_RESULT;
109
110 // Shorter aliases for ParseInteger() where sign_policy=NON_NEGATIVE, and the
111 // return value is true only when the result was OK and |*output| was written
112 // to.
113 NET_EXPORT bool ParseNonNegativeInteger(const base::StringPiece& input,
114 int32_t* output) WARN_UNUSED_RESULT;
115
116 NET_EXPORT bool ParseNonNegativeInteger(const base::StringPiece& input,
117 uint32_t* output) WARN_UNUSED_RESULT;
118
119 NET_EXPORT bool ParseNonNegativeInteger(const base::StringPiece& input,
120 int64_t* output) WARN_UNUSED_RESULT;
121
122 NET_EXPORT bool ParseNonNegativeInteger(const base::StringPiece& input,
123 uint64_t* output) WARN_UNUSED_RESULT;
65 124
66 } // namespace net 125 } // namespace net
67 126
68 #endif // NET_BASE_PARSE_NUMBER_H_ 127 #endif // NET_BASE_PARSE_NUMBER_H_
OLDNEW
« no previous file with comments | « net/base/ip_address.cc ('k') | net/base/parse_number.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698