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

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: comment fix Created 4 years, 8 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 implementations.
19 // implementations.
20 // 19 //
21 // However using the number parsing functions from //base directly in network 20 // However using the base::StringTo*() has historically led to subtle bugs
22 // code can lead to subtle bugs, as the //base versions are more permissive. 21 // in the context of parsing network protocols:
23 // For instance "+99" is successfully parsed by base::StringToInt().
24 // 22 //
25 // However in the majority of places in //net, a leading plus on a number 23 // * Permitting a leading '+'
26 // should be considered invalid. For instance when parsing a host:port pair 24 // * Incorrectly classifying overflow/underflow from a parsing failure
27 // you wouldn't want to recognize "foo:+99" as having a port of 99. The same 25 // * Allowing negative numbers for non-negative fields
28 // issue applies when parsing a content-length header.
29 // 26 //
30 // To reduce the risk of such problems, use of these functions over the 27 // This API tries to avoid these problems by picking sensible defaults for
31 // //base versions. 28 // //net code. For more details see crbug.com/596523.
32 29
33 class GURL; 30 class GURL;
34 31
35 namespace net { 32 namespace net {
36 33
37 // Parses a string representing a decimal number to an |int|. Returns true on 34 // Format to use when parsing integers.
38 // success, and fills |*output| with the result. Note that |*output| is not 35 enum class ParseIntFormat {
39 // modified on failure. 36 // Accepts non-negative base 10 integers of the form:
37 //
38 // 1*DIGIT
39 //
40 // This construction is used in a variety of IETF standards, such as RFC 7230
41 // (HTTP).
42 //
43 // When attempting to parse a negative number using this format, the failure
44 // will be FAILED_PARSE since it violated the expected format (and not
45 // FAILED_UNDERFLOW).
46 //
47 // Also note that inputs need not be in minimal encoding: "0003" is valid and
48 // equivalent to "3".
49 NON_NEGATIVE,
50
51 // Accept optionally negative base 10 integers of the form:
52 //
53 // ["-"] 1*DIGIT
54 //
55 // In other words, this accepts the same things as DISALLOW_NEGATIVE, and
Ryan Sleevi 2016/04/08 19:50:45 I believe this should be NON_NEGATIVE ?
eroman 2016/04/08 20:53:07 Done.
56 // additionally recognizes those numbers with a leading '-'.
57 //
58 // Note that by this defintion "-0" IS a valid input.
59 OPTIONALLY_NEGATIVE
60 };
61
62 // The specific reason why a ParseInt*() function failed.
63 enum class ParseIntError {
64 // The parsed number couldn't fit into the provided output type because it was
65 // too high.
66 FAILED_OVERFLOW,
67
68 // The parsed number couldn't fit into the provided output type because it was
69 // too low.
70 FAILED_UNDERFLOW,
71
72 // The number failed to be parsed because it wasn't a valid decimal number (as
73 // determined by the policy).
74 FAILED_PARSE,
75 };
76
77 // The ParseInt*() functions parse a string representing a number.
40 // 78 //
41 // Recognized inputs take the form: 79 // The format of the strings that are accepted is controlled by the |format|
42 // 1*DIGIT 80 // parameter. This allows rejecting negative numbers.
43 // 81 //
44 // Where DIGIT is an ASCII number in the range '0' - '9' 82 // These functions return true on success, and fill |*output| with the result.
45 // 83 //
46 // Note that: 84 // On failure it is guaranteed that |*output| was not modified. If
Ryan Sleevi 2016/04/08 19:50:45 s/On failure it/On failure, it/
eroman 2016/04/08 20:53:07 Done.
47 // * Parsing is locale independent 85 // |optional_error| was non-null, then it is filled with the reason for the
48 // * Leading zeros are allowed (numbers needn't be in minimal encoding) 86 // failure.
49 // * Inputs that would overflow the output are rejected. 87 NET_EXPORT bool ParseInt32(const base::StringPiece& input,
50 // * Only accepts integers 88 ParseIntFormat format,
89 int32_t* output,
90 ParseIntError* optional_error = nullptr)
91 WARN_UNUSED_RESULT;
92
93 NET_EXPORT bool ParseInt64(const base::StringPiece& input,
94 ParseIntFormat format,
95 int64_t* output,
96 ParseIntError* optional_error = nullptr)
97 WARN_UNUSED_RESULT;
98
99 // The ParseUint*() functions parse a string representing a number.
51 // 100 //
52 // Examples of recognized inputs are: 101 // These are equivalent to calling ParseInt*() with a format string of
53 // "13" 102 // ParseIntFormat::NON_NEGATIVE and unsigned output types.
54 // "0" 103 NET_EXPORT bool ParseUint32(const base::StringPiece& input,
55 // "00013" 104 uint32_t* output,
56 // 105 ParseIntError* optional_error = nullptr)
57 // Examples of rejected inputs are: 106 WARN_UNUSED_RESULT;
58 // " 13" 107
59 // "-13" 108 NET_EXPORT bool ParseUint64(const base::StringPiece& input,
60 // "+13" 109 uint64_t* output,
61 // "0x15" 110 ParseIntError* optional_error = nullptr)
62 // "13.3" 111 WARN_UNUSED_RESULT;
63 NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input,
64 int* output) WARN_UNUSED_RESULT;
65 112
66 } // namespace net 113 } // namespace net
67 114
68 #endif // NET_BASE_PARSE_NUMBER_H_ 115 #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