OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium 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 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
| 7 #include <limits> |
7 #include "../../include/fxcrt/fx_ext.h" | 8 #include "../../include/fxcrt/fx_ext.h" |
8 template <class T, class STR_T> | 9 template <class T, class STR_T> |
9 T FXSYS_StrToInt(STR_T str) | 10 T FXSYS_StrToInt(STR_T str) |
10 { | 11 { |
11 FX_BOOL neg = FALSE; | 12 FX_BOOL neg = FALSE; |
12 if (str == NULL) { | 13 if (str == NULL) { |
13 return 0; | 14 return 0; |
14 } | 15 } |
15 if (*str == '-') { | 16 if (*str == '-') { |
16 neg = TRUE; | 17 neg = TRUE; |
17 str ++; | 18 str ++; |
18 } | 19 } |
19 T num = 0; | 20 T num = 0; |
20 while (*str) { | 21 while (*str) { |
21 if ((*str) < '0' || (*str) > '9') { | 22 if ((*str) < '0' || (*str) > '9') { |
22 break; | 23 break; |
23 } | 24 } |
| 25 if (num > (std::numeric_limits<T>::max() - 9) / 10) { |
| 26 break; |
| 27 } |
24 num = num * 10 + (*str) - '0'; | 28 num = num * 10 + (*str) - '0'; |
25 str ++; | 29 str ++; |
26 } | 30 } |
27 return neg ? -num : num; | 31 return neg ? -num : num; |
28 } | 32 } |
29 template <typename T, typename STR_T> | 33 template <typename T, typename STR_T> |
30 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) | 34 STR_T FXSYS_IntToStr(T value, STR_T string, int radix) |
31 { | 35 { |
32 int i = 0; | 36 int i = 0; |
33 if (value < 0) { | 37 if (value < 0) { |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 buf[wlen] = bstr[i]; | 227 buf[wlen] = bstr[i]; |
224 } | 228 } |
225 wlen ++; | 229 wlen ++; |
226 } | 230 } |
227 return wlen; | 231 return wlen; |
228 } | 232 } |
229 #ifdef __cplusplus | 233 #ifdef __cplusplus |
230 } | 234 } |
231 #endif | 235 #endif |
232 #endif | 236 #endif |
OLD | NEW |