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

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

Issue 2528243002: Fix silent truncations when extracting values from CheckedNumeric (Closed)
Patch Set: compile cleanup and fix Created 4 years 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 (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 17 matching lines...) Expand all
28 const size_t kOutputBufSize = 28 const size_t kOutputBufSize =
29 3 * sizeof(INT) + std::numeric_limits<INT>::is_signed; 29 3 * sizeof(INT) + std::numeric_limits<INT>::is_signed;
30 30
31 // Create the string in a temporary buffer, write it back to front, and 31 // Create the string in a temporary buffer, write it back to front, and
32 // then return the substr of what we ended up using. 32 // then return the substr of what we ended up using.
33 using CHR = typename STR::value_type; 33 using CHR = typename STR::value_type;
34 CHR outbuf[kOutputBufSize]; 34 CHR outbuf[kOutputBufSize];
35 35
36 // The ValueOrDie call below can never fail, because UnsignedAbs is valid 36 // The ValueOrDie call below can never fail, because UnsignedAbs is valid
37 // for all valid inputs. 37 // for all valid inputs.
38 auto res = CheckedNumeric<INT>(value).UnsignedAbs().ValueOrDie(); 38 typename decltype(CheckedNumeric<INT>(value).UnsignedAbs())::type res =
39 CheckedNumeric<INT>(value).UnsignedAbs().ValueOrDie();
39 40
40 CHR* end = outbuf + kOutputBufSize; 41 CHR* end = outbuf + kOutputBufSize;
41 CHR* i = end; 42 CHR* i = end;
42 do { 43 do {
43 --i; 44 --i;
44 DCHECK(i != outbuf); 45 DCHECK(i != outbuf);
45 *i = static_cast<CHR>((res % 10) + '0'); 46 *i = static_cast<CHR>((res % 10) + '0');
46 res /= 10; 47 res /= 10;
47 } while (res != 0); 48 } while (res != 0);
48 if (IsValueNegative(value)) { 49 if (IsValueNegative(value)) {
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 bool HexStringToUInt64(const StringPiece& input, uint64_t* output) { 476 bool HexStringToUInt64(const StringPiece& input, uint64_t* output) {
476 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( 477 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke(
477 input.begin(), input.end(), output); 478 input.begin(), input.end(), output);
478 } 479 }
479 480
480 bool HexStringToBytes(const std::string& input, std::vector<uint8_t>* output) { 481 bool HexStringToBytes(const std::string& input, std::vector<uint8_t>* output) {
481 return HexStringToBytesT(input, output); 482 return HexStringToBytesT(input, output);
482 } 483 }
483 484
484 } // namespace base 485 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698