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

Side by Side Diff: base/strings/string_number_conversions.cc

Issue 1685743006: StringToUint should output zero on negative numbers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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 | « no previous file | base/strings/string_number_conversions_unittest.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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 #include "base/strings/string_number_conversions.h" 5 #include "base/strings/string_number_conversions.h"
6 6
7 #include <ctype.h> 7 #include <ctype.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <wctype.h> 10 #include <wctype.h>
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 typedef typename traits::value_type value_type; 133 typedef typename traits::value_type value_type;
134 134
135 // Generalized iterator-range-to-number conversion. 135 // Generalized iterator-range-to-number conversion.
136 // 136 //
137 static bool Invoke(const_iterator begin, 137 static bool Invoke(const_iterator begin,
138 const_iterator end, 138 const_iterator end,
139 value_type* output) { 139 value_type* output) {
140 bool valid = true; 140 bool valid = true;
141 141
142 while (begin != end && LocalIsWhitespace(*begin)) { 142 while (begin != end && LocalIsWhitespace(*begin)) {
143 valid = false; 143 valid = false;
Nico 2016/02/11 21:40:31 this also can return false and doesn't write to ou
davidben 2016/02/11 21:47:22 This just falls through to later, so I think it's
144 ++begin; 144 ++begin;
145 } 145 }
146 146
147 if (begin != end && *begin == '-') { 147 if (begin != end && *begin == '-') {
148 if (!std::numeric_limits<value_type>::is_signed) { 148 if (!std::numeric_limits<value_type>::is_signed) {
149 *output = 0;
149 valid = false; 150 valid = false;
150 } else if (!Negative::Invoke(begin + 1, end, output)) { 151 } else if (!Negative::Invoke(begin + 1, end, output)) {
151 valid = false; 152 valid = false;
152 } 153 }
153 } else { 154 } else {
154 if (begin != end && *begin == '+') { 155 if (begin != end && *begin == '+') {
155 ++begin; 156 ++begin;
156 } 157 }
157 if (!Positive::Invoke(begin, end, output)) { 158 if (!Positive::Invoke(begin, end, output)) {
158 valid = false; 159 valid = false;
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 HexIteratorRangeToInt64Traits; 281 HexIteratorRangeToInt64Traits;
281 282
282 typedef BaseHexIteratorRangeToUInt64Traits<StringPiece::const_iterator> 283 typedef BaseHexIteratorRangeToUInt64Traits<StringPiece::const_iterator>
283 HexIteratorRangeToUInt64Traits; 284 HexIteratorRangeToUInt64Traits;
284 285
285 template <typename STR> 286 template <typename STR>
286 bool HexStringToBytesT(const STR& input, std::vector<uint8_t>* output) { 287 bool HexStringToBytesT(const STR& input, std::vector<uint8_t>* output) {
287 DCHECK_EQ(output->size(), 0u); 288 DCHECK_EQ(output->size(), 0u);
288 size_t count = input.size(); 289 size_t count = input.size();
289 if (count == 0 || (count % 2) != 0) 290 if (count == 0 || (count % 2) != 0)
290 return false; 291 return false;
Nico 2016/02/11 21:40:31 (this also doesn't write to output from what i can
davidben 2016/02/11 21:47:22 Hrm. I dunno. I don't have strong opinions about w
291 for (uintptr_t i = 0; i < count / 2; ++i) { 292 for (uintptr_t i = 0; i < count / 2; ++i) {
292 uint8_t msb = 0; // most significant 4 bits 293 uint8_t msb = 0; // most significant 4 bits
293 uint8_t lsb = 0; // least significant 4 bits 294 uint8_t lsb = 0; // least significant 4 bits
294 if (!CharToDigit<16>(input[i * 2], &msb) || 295 if (!CharToDigit<16>(input[i * 2], &msb) ||
295 !CharToDigit<16>(input[i * 2 + 1], &lsb)) 296 !CharToDigit<16>(input[i * 2 + 1], &lsb))
296 return false; 297 return false;
297 output->push_back((msb << 4) | lsb); 298 output->push_back((msb << 4) | lsb);
298 } 299 }
299 return true; 300 return true;
300 } 301 }
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 bool HexStringToUInt64(const StringPiece& input, uint64_t* output) { 477 bool HexStringToUInt64(const StringPiece& input, uint64_t* output) {
477 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( 478 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke(
478 input.begin(), input.end(), output); 479 input.begin(), input.end(), output);
479 } 480 }
480 481
481 bool HexStringToBytes(const std::string& input, std::vector<uint8_t>* output) { 482 bool HexStringToBytes(const std::string& input, std::vector<uint8_t>* output) {
482 return HexStringToBytesT(input, output); 483 return HexStringToBytesT(input, output);
483 } 484 }
484 485
485 } // namespace base 486 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/strings/string_number_conversions_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698