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

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: Address remaining feedback 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
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.
Ryan Sleevi 2016/03/25 22:35:17 s/versions are/versions are/
eroman 2016/04/07 01:17:01 Done.
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.
Ryan Sleevi 2016/03/25 22:35:17 s/content-length/Content-Length/
eroman 2016/04/07 01:17:01 No longer applicable -I re-wrote this entire comme
29 // 29 //
30 // Another difficulty with using base::StringToInt() is distinguishing
31 // overflow/underflow from parsing failures.
Ryan Sleevi 2016/03/25 22:35:17 Not sure why this matters?
eroman 2016/03/25 23:02:23 Some code wants to know when overflow has happened
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 // Policy for parsing of integers that determines which form of numbers will be
38 // success, and fills |*output| with the result. Note that |*output| is not 41 // accepted.
39 // modified on failure. 42 enum class ParseInteger {
43 // Accept inputs of the form:
44 //
45 // 1*DIGIT
46 //
47 // This is consistent with the description in RFC 2616 for representing
Ryan Sleevi 2016/03/25 22:35:17 s/2616/7230/
eroman 2016/04/07 01:17:01 Done.
48 // numbers, as well as many other HTTP related standards.
Ryan Sleevi 2016/03/25 22:35:17 It's not just HTTP standards (DIGIT comes from the
eroman 2016/04/07 01:17:01 Done.
49 DISALLOW_NEGATIVE,
50
51 // Accept inputs of the form:
52 //
53 // ["-"] 1*DIGIT
54 //
55 // In other words, the number can optional start with a negative sign. Note
56 // that -0 is also counted as a valid input.
57 ALLOW_NEGATIVE
mmenke 2016/03/25 22:10:56 Think it's worth noting that if this is true, we r
eroman 2016/04/07 01:17:01 You mean a comment in DISALLOW_NEGATIVE? Done.
58 };
59
60 // The specific reason why a ParseInteger*() function failed.
61 enum class ParseIntegerError {
62 // The parsed number couldn't fit into the provided output type because it was
63 // too large.
mmenke 2016/03/25 22:10:56 nit: suggest high / low instead of large/small. L
eroman 2016/04/07 01:17:01 Done.
64 FAILED_OVERFLOW,
65
66 // The parsed number couldn't fit into the provided output type because it was
67 // too small (negative).
68 FAILED_UNDERFLOW,
69
70 // The number failed to be parsed because it wasn't a valid decimal number (as
71 // determined by the policy).
72 FAILED_PARSE,
73 };
74
75 // ParseIntegerBase10() parses a string representing a decimal number to a
76 // SIGNED integer result.
40 // 77 //
41 // Recognized inputs take the form: 78 // Returns true on success, and fills |*output| with the result. If
42 // 1*DIGIT 79 // |optional_error| was non-null, then it is filled with the reason for the
80 // failure.
43 // 81 //
44 // Where DIGIT is an ASCII number in the range '0' - '9' 82 // On failure it is guaranteed that |*output| was not modified.
45 // 83 //
46 // Note that: 84 // See the ParseInteger enum for a description of what types of input are
47 // * Parsing is locale independent 85 // accepted. Note that although the output type is signed, the parsing of
48 // * Leading zeros are allowed (numbers needn't be in minimal encoding) 86 // negative values can be disallowed by selecting the
mmenke 2016/03/25 22:10:56 "selecting the" -> "passing" / "using"?
eroman 2016/04/07 01:17:01 Done.
49 // * Inputs that would overflow the output are rejected. 87 // ParseInteger::DISALLOW_NEGATIVE.
50 // * Only accepts integers 88 NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
51 // 89 ParseInteger policy,
52 // Examples of recognized inputs are: 90 int32_t* output,
53 // "13" 91 ParseIntegerError* optional_error = nullptr)
Ryan Sleevi 2016/03/25 22:35:17 With respect to the defaults here, it seems like t
eroman 2016/03/25 23:02:23 The optionality of the error is primarily to make
54 // "0" 92 WARN_UNUSED_RESULT;
55 // "00013" 93
56 // 94 NET_EXPORT bool ParseIntegerBase10(const base::StringPiece& input,
57 // Examples of rejected inputs are: 95 ParseInteger policy,
58 // " 13" 96 int64_t* output,
59 // "-13" 97 ParseIntegerError* optional_error = nullptr)
60 // "+13" 98 WARN_UNUSED_RESULT;
Ryan Sleevi 2016/03/25 22:35:17 I would argue that overloading like this violates
mmenke 2016/03/25 22:49:59 "Use overloaded functions (including constructors)
eroman 2016/03/25 23:02:23 For technical reasons I am going to have to avoid
61 // "0x15" 99
62 // "13.3" 100 // ParseUnsignedIntegerBase10() is the same as calling ParseIntegerBase10()
63 NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input, 101 // except:
64 int* output) WARN_UNUSED_RESULT; 102 // * The output type is unsigned
103 // * The policy is implied to be ParseInteger::DISALLOW_NEGATIVE
104 NET_EXPORT bool ParseUnsignedIntegerBase10(
105 const base::StringPiece& input,
106 uint32_t* output,
107 ParseIntegerError* optional_error = nullptr) WARN_UNUSED_RESULT;
108
109 NET_EXPORT bool ParseUnsignedIntegerBase10(
110 const base::StringPiece& input,
111 uint64_t* output,
112 ParseIntegerError* optional_error = nullptr) WARN_UNUSED_RESULT;
65 113
66 } // namespace net 114 } // namespace net
67 115
68 #endif // NET_BASE_PARSE_NUMBER_H_ 116 #endif // NET_BASE_PARSE_NUMBER_H_
OLDNEW
« no previous file with comments | « net/base/ip_address.cc ('k') | net/base/parse_number.cc » ('j') | net/base/parse_number.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698