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

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

Issue 1178733004: Fix -INT_MIN integer overflow in string_number_conversions.cc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cosmetics. Created 5 years, 6 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 | no next file » | 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 24 matching lines...) Expand all
35 template <typename INT2, typename UINT2> 35 template <typename INT2, typename UINT2>
36 struct ToUnsignedT<INT2, UINT2, false> { 36 struct ToUnsignedT<INT2, UINT2, false> {
37 static UINT2 ToUnsigned(INT2 value) { 37 static UINT2 ToUnsigned(INT2 value) {
38 return static_cast<UINT2>(value); 38 return static_cast<UINT2>(value);
39 } 39 }
40 }; 40 };
41 41
42 template <typename INT2, typename UINT2> 42 template <typename INT2, typename UINT2>
43 struct ToUnsignedT<INT2, UINT2, true> { 43 struct ToUnsignedT<INT2, UINT2, true> {
44 static UINT2 ToUnsigned(INT2 value) { 44 static UINT2 ToUnsigned(INT2 value) {
45 return static_cast<UINT2>(value < 0 ? -value : value); 45 if (value >= 0) {
46 return value;
47 } else {
48 // Avoid integer overflow when negating INT_MIN.
49 return static_cast<UINT2>(-(value + 1)) + 1;
50 }
46 } 51 }
47 }; 52 };
48 53
49 // This set of templates is very similar to the above templates, but 54 // This set of templates is very similar to the above templates, but
50 // for testing whether an integer is negative. 55 // for testing whether an integer is negative.
51 template <typename INT2, bool NEG2> 56 template <typename INT2, bool NEG2>
52 struct TestNegT {}; 57 struct TestNegT {};
53 template <typename INT2> 58 template <typename INT2>
54 struct TestNegT<INT2, false> { 59 struct TestNegT<INT2, false> {
55 static bool TestNeg(INT2 value) { 60 static bool TestNeg(INT2 value) {
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
520 bool HexStringToUInt64(const StringPiece& input, uint64* output) { 525 bool HexStringToUInt64(const StringPiece& input, uint64* output) {
521 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke( 526 return IteratorRangeToNumber<HexIteratorRangeToUInt64Traits>::Invoke(
522 input.begin(), input.end(), output); 527 input.begin(), input.end(), output);
523 } 528 }
524 529
525 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) { 530 bool HexStringToBytes(const std::string& input, std::vector<uint8>* output) {
526 return HexStringToBytesT(input, output); 531 return HexStringToBytesT(input, output);
527 } 532 }
528 533
529 } // namespace base 534 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698