| 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 "../../include/fxcrt/fx_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
| 8 #include "../../../third_party/base/numerics/safe_math.h" | 8 #include "../../../third_party/base/numerics/safe_math.h" |
| 9 | 9 |
| 10 static CFX_StringDataW* FX_AllocStringW(int nLen) | 10 static CFX_StringDataW* FX_AllocStringW(int nLen) |
| 11 { | 11 { |
| 12 // TODO(palmer): |nLen| should really be declared as |size_t|, or |
| 13 // at least unsigned. |
| 12 if (nLen == 0 || nLen < 0) { | 14 if (nLen == 0 || nLen < 0) { |
| 13 return NULL; | 15 return NULL; |
| 14 } | 16 } |
| 15 | 17 |
| 16 pdfium::base::CheckedNumeric<int> iSize = static_cast<int>(sizeof(FX_WCHAR))
; | 18 int overhead = 3 * sizeof(long) + sizeof(FX_WCHAR); // +WCHAR is for NUL. |
| 17 iSize *= nLen + 1; | 19 pdfium::base::CheckedNumeric<int> iSize = nLen; |
| 18 iSize += sizeof(long) * 3; | 20 iSize *= sizeof(FX_WCHAR); |
| 21 iSize += overhead; |
| 22 |
| 23 // Now round to an 8-byte boundary. We'd expect that this is the minimum |
| 24 // granularity of any of the underlying allocators, so there may be cases |
| 25 // where we can save a re-alloc when adding a few characters to a string |
| 26 // by using this otherwise wasted space. |
| 27 iSize += 7; |
| 28 int totalSize = iSize.ValueOrDie() & ~7; |
| 29 int usableLen = (totalSize - overhead) / sizeof(FX_WCHAR); |
| 30 FXSYS_assert(usableLen >= nLen); |
| 31 |
| 19 CFX_StringDataW* pData = (CFX_StringDataW*)FX_Alloc(FX_BYTE, iSize.ValueOrDi
e()); | 32 CFX_StringDataW* pData = (CFX_StringDataW*)FX_Alloc(FX_BYTE, iSize.ValueOrDi
e()); |
| 20 if (!pData) { | 33 if (!pData) { |
| 21 return NULL; | 34 return NULL; |
| 22 } | 35 } |
| 23 | 36 |
| 24 // TODO(palmer): |nLen| should really be declared as |size_t|, but for | 37 pData->m_nAllocLength = usableLen; |
| 25 // now I just want to fix the overflow without changing any interfaces. | |
| 26 // Declaring |nLen| as |size_t| will also simplify the above code | |
| 27 // somewhat. | |
| 28 pData->m_nAllocLength = nLen; | |
| 29 pData->m_nDataLength = nLen; | 38 pData->m_nDataLength = nLen; |
| 30 pData->m_nRefs = 1; | 39 pData->m_nRefs = 1; |
| 31 pData->m_String[nLen] = 0; | 40 pData->m_String[nLen] = 0; |
| 32 return pData; | 41 return pData; |
| 33 } | 42 } |
| 34 static void FX_ReleaseStringW(CFX_StringDataW* pData) | 43 static void FX_ReleaseStringW(CFX_StringDataW* pData) |
| 35 { | 44 { |
| 36 if (pData == NULL) { | 45 if (pData == NULL) { |
| 37 return; | 46 return; |
| 38 } | 47 } |
| (...skipping 1068 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1107 return (CFX_CharMap*)&g_DefaultJISMapper; | 1116 return (CFX_CharMap*)&g_DefaultJISMapper; |
| 1108 case 936: | 1117 case 936: |
| 1109 return (CFX_CharMap*)&g_DefaultGBKMapper; | 1118 return (CFX_CharMap*)&g_DefaultGBKMapper; |
| 1110 case 949: | 1119 case 949: |
| 1111 return (CFX_CharMap*)&g_DefaultUHCMapper; | 1120 return (CFX_CharMap*)&g_DefaultUHCMapper; |
| 1112 case 950: | 1121 case 950: |
| 1113 return (CFX_CharMap*)&g_DefaultBig5Mapper; | 1122 return (CFX_CharMap*)&g_DefaultBig5Mapper; |
| 1114 } | 1123 } |
| 1115 return NULL; | 1124 return NULL; |
| 1116 } | 1125 } |
| OLD | NEW |