summaryrefslogtreecommitdiff
path: root/core/fxcrt/fx_basic_gcc.cpp
diff options
context:
space:
mode:
authorWei Li <weili@chromium.org>2016-03-25 15:02:07 -0700
committerWei Li <weili@chromium.org>2016-03-25 15:02:07 -0700
commit6d18bd3b8ec82ae3c24a439f5c7925786a0e2d8b (patch)
tree84cd5ffed5a358aa806ebc2b0b80dc03d2bcb3a5 /core/fxcrt/fx_basic_gcc.cpp
parentc3255f55954b2adc3edbd3269c0a8779f7d4b7ad (diff)
downloadpdfium-6d18bd3b8ec82ae3c24a439f5c7925786a0e2d8b.tar.xz
Fix FXSYS_StrToInt()
Correctly handle sign and out of range values. R=dsinclair@chromium.org, tsepez@chromium.org Review URL: https://codereview.chromium.org/1828873002 .
Diffstat (limited to 'core/fxcrt/fx_basic_gcc.cpp')
-rw-r--r--core/fxcrt/fx_basic_gcc.cpp18
1 files changed, 14 insertions, 4 deletions
diff --git a/core/fxcrt/fx_basic_gcc.cpp b/core/fxcrt/fx_basic_gcc.cpp
index 440f924795..c29c9956e2 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++;