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

Unified Diff: core/fxcrt/fx_basic_gcc.cpp

Issue 1828873002: Fix FXSYS_StrToInt() (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: address comments Created 4 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | core/fxcrt/fx_basic_gcc_unittest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fxcrt/fx_basic_gcc.cpp
diff --git a/core/fxcrt/fx_basic_gcc.cpp b/core/fxcrt/fx_basic_gcc.cpp
index 2c9c54c8b60bd26cf6d6772517c6ec2f7df007e9..c908f7059d1cc0b3269651667b8760e87c1eb920 100644
--- a/core/fxcrt/fx_basic_gcc.cpp
+++ b/core/fxcrt/fx_basic_gcc.cpp
@@ -16,15 +16,25 @@ IntType FXSYS_StrToInt(const CharType* str) {
if (!str)
return 0;
- bool neg = std::numeric_limits<IntType>::is_signed && *str == '-';
- if (neg)
+ // Process the sign.
+ bool neg = *str == '-';
+ if (neg || *str == '+')
str++;
IntType num = 0;
while (*str && FXSYS_isDecimalDigit(*str)) {
IntType val = FXSYS_toDecimalDigit(*str);
- if (num > (std::numeric_limits<IntType>::max() - val) / 10)
- break;
+ if (num > (std::numeric_limits<IntType>::max() - val) / 10) {
+ if (neg && std::numeric_limits<IntType>::is_signed) {
+ // Return MIN when the represented number is signed type and is smaller
+ // than the min value.
+ return std::numeric_limits<IntType>::min();
+ } else {
+ // Return MAX when the represented number is signed type and is larger
+ // than the max value, or the number is unsigned type and out of range.
+ return std::numeric_limits<IntType>::max();
+ }
+ }
num = num * 10 + val;
str++;
« no previous file with comments | « no previous file | core/fxcrt/fx_basic_gcc_unittest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698