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

Side by Side Diff: core/fxcrt/fx_basic_util.cpp

Issue 2168173002: Fixup integer conversion logic. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Fixup test value Created 4 years, 5 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 | core/fxcrt/fx_basic_util_unittest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "core/fxcrt/include/fx_basic.h" 7 #include "core/fxcrt/include/fx_basic.h"
8 #include "core/fxcrt/include/fx_ext.h" 8 #include "core/fxcrt/include/fx_ext.h"
9 9
10 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_ 10 #if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
11 #include <dirent.h> 11 #include <dirent.h>
12 #include <sys/types.h> 12 #include <sys/types.h>
13 #else 13 #else
14 #include <direct.h> 14 #include <direct.h>
15 #endif 15 #endif
16 16
17 #include <algorithm> 17 #include <algorithm>
18 #include <cctype> 18 #include <cctype>
19 #include <limits>
19 #include <memory> 20 #include <memory>
20 21
22 namespace {
23
24 const int kDefaultIntValue = 0;
25
26 } // namespace
27
21 bool FX_atonum(const CFX_ByteStringC& strc, void* pData) { 28 bool FX_atonum(const CFX_ByteStringC& strc, void* pData) {
22 if (strc.Find('.') != -1) { 29 if (strc.Find('.') != -1) {
23 FX_FLOAT* pFloat = static_cast<FX_FLOAT*>(pData); 30 FX_FLOAT* pFloat = static_cast<FX_FLOAT*>(pData);
24 *pFloat = FX_atof(strc); 31 *pFloat = FX_atof(strc);
25 return false; 32 return false;
26 } 33 }
27 34
35 // Note, numbers in PDF are typically of the form 123, -123, etc. But,
36 // for things like the Permissions on the encryption hash the number is
37 // actually a unsigned value. We treat use uint32_t so we can deal with the
Lei Zhang 2016/07/21 20:39:31 "We treat use uint32_t" does not parse English bad
dsinclair 2016/07/25 13:47:59 Done.
38 // unsigned and then check for overflow if the user actually signed the value.
39 // Permissions flag is listed in Table 3.20 PDF 1.7 spec.
40 pdfium::base::CheckedNumeric<uint32_t> integer = 0;
41 bool bNegative = false;
42 bool bSigned = false;
28 int cc = 0; 43 int cc = 0;
29 pdfium::base::CheckedNumeric<int> integer = 0;
30 bool bNegative = false;
31 if (strc[0] == '+') { 44 if (strc[0] == '+') {
32 cc++; 45 cc++;
46 bSigned = true;
33 } else if (strc[0] == '-') { 47 } else if (strc[0] == '-') {
34 bNegative = true; 48 bNegative = true;
49 bSigned = true;
35 cc++; 50 cc++;
36 } 51 }
52
37 while (cc < strc.GetLength() && std::isdigit(strc[cc])) { 53 while (cc < strc.GetLength() && std::isdigit(strc[cc])) {
38 integer = integer * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc)); 54 integer = integer * 10 + FXSYS_toDecimalDigit(strc.CharAt(cc));
39 if (!integer.IsValid()) 55 if (!integer.IsValid())
40 break; 56 break;
41 cc++; 57 cc++;
42 } 58 }
59
60 // We have a sign, and the value was greater then a regular integer
61 // we've overflowed, reset to the default value.
62 if (bSigned &&
63 integer.ValueOrDefault(kDefaultIntValue) >=
64 static_cast<uint32_t>(std::numeric_limits<int>::max())) {
65 integer = kDefaultIntValue;
66 }
67
68 // Switch back to the int space so we can flip to a negative if we need.
69 int value = integer.ValueOrDefault(kDefaultIntValue);
43 if (bNegative) 70 if (bNegative)
44 integer = -integer; 71 value = -value;
Lei Zhang 2016/07/21 20:39:31 Do we need to worry about the value = INT_MIN case
dsinclair 2016/07/25 13:47:59 Fixed up some issues around boundary conversions.
45 72
46 int* pInt = static_cast<int*>(pData); 73 int* pInt = static_cast<int*>(pData);
47 *pInt = integer.ValueOrDefault(0); 74 *pInt = value;
48 return true; 75 return true;
49 } 76 }
50 77
51 static const FX_FLOAT fraction_scales[] = { 78 static const FX_FLOAT fraction_scales[] = {
52 0.1f, 0.01f, 0.001f, 0.0001f, 79 0.1f, 0.01f, 0.001f, 0.0001f,
53 0.00001f, 0.000001f, 0.0000001f, 0.00000001f, 80 0.00001f, 0.000001f, 0.0000001f, 0.00000001f,
54 0.000000001f, 0.0000000001f, 0.00000000001f}; 81 0.000000001f, 0.0000000001f, 0.00000000001f};
55 int FXSYS_FractionalScaleCount() { 82 int FXSYS_FractionalScaleCount() {
56 return FX_ArraySize(fraction_scales); 83 return FX_ArraySize(fraction_scales);
57 } 84 }
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 dstShift -= 8; 329 dstShift -= 8;
303 result |= *dataPtr++ << dstShift; 330 result |= *dataPtr++ << dstShift;
304 } 331 }
305 if (dstShift > 0) { 332 if (dstShift > 0) {
306 bitShift = 8 - dstShift; 333 bitShift = 8 - dstShift;
307 bitMask = (1 << dstShift) - 1; 334 bitMask = (1 << dstShift) - 1;
308 result |= *dataPtr++ >> bitShift & bitMask; 335 result |= *dataPtr++ >> bitShift & bitMask;
309 } 336 }
310 return result; 337 return result;
311 } 338 }
OLDNEW
« no previous file with comments | « no previous file | core/fxcrt/fx_basic_util_unittest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698