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

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 commentary on size_t... will shave that yak another time 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 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
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*() family of functions parse a string representing a number.
40 // 78 //
41 // Recognized inputs take the form: 79 // Returns true on success, and fills |*output| with the result. If
mmenke 2016/04/07 20:42:53 Should these be plural, since they refer to all th
eroman 2016/04/08 18:57:32 Reworded the comment blocks.
42 // 1*DIGIT 80 // |optional_error| was non-null, then it is filled with the reason for the
81 // failure.
43 // 82 //
44 // Where DIGIT is an ASCII number in the range '0' - '9' 83 // On failure it is guaranteed that |*output| was not modified.
mmenke 2016/04/07 20:42:53 Maybe just: "On failure |*output| will not be mod
eroman 2016/04/08 18:57:32 Done.
45 // 84 //
46 // Note that: 85 // In the case of signed output types, |format| controls whether negative
47 // * Parsing is locale independent 86 // numbers are accepted. When parsing to an unsigned output the format behaves
48 // * Leading zeros are allowed (numbers needn't be in minimal encoding) 87 // as if NON_NEGATIVE.
49 // * Inputs that would overflow the output are rejected. 88 NET_EXPORT bool ParseInt32(const base::StringPiece& input,
50 // * Only accepts integers 89 ParseIntFormat format,
51 // 90 int32_t* output,
52 // Examples of recognized inputs are: 91 ParseIntError* optional_error = nullptr)
53 // "13" 92 WARN_UNUSED_RESULT;
54 // "0" 93
55 // "00013" 94 NET_EXPORT bool ParseInt64(const base::StringPiece& input,
56 // 95 ParseIntFormat format,
57 // Examples of rejected inputs are: 96 int64_t* output,
58 // " 13" 97 ParseIntError* optional_error = nullptr)
59 // "-13" 98 WARN_UNUSED_RESULT;
60 // "+13" 99
61 // "0x15" 100 NET_EXPORT bool ParseUint32(const base::StringPiece& input,
mmenke 2016/04/07 20:42:53 Should these have a separate comment? Don't think
eroman 2016/04/08 18:57:32 Done.
62 // "13.3" 101 uint32_t* output,
63 NET_EXPORT bool ParseNonNegativeDecimalInt(const base::StringPiece& input, 102 ParseIntError* optional_error = nullptr)
64 int* output) WARN_UNUSED_RESULT; 103 WARN_UNUSED_RESULT;
104
105 NET_EXPORT bool ParseUint64(const base::StringPiece& input,
106 uint64_t* output,
107 ParseIntError* optional_error = nullptr)
108 WARN_UNUSED_RESULT;
65 109
66 } // namespace net 110 } // namespace net
67 111
68 #endif // NET_BASE_PARSE_NUMBER_H_ 112 #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