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

Side by Side Diff: core/src/fxcrt/fx_basic_wstring.cpp

Issue 383563002: Fix an out-of-boundary issue for wide string (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Add WStringLength function Created 6 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 | « core/src/fxcrt/fx_basic_util.cpp ('k') | core/src/fxge/win32/fx_win32_device.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 "../../include/fxcrt/fx_basic.h" 7 #include "../../include/fxcrt/fx_basic.h"
8 #include "../../../third_party/numerics/safe_math.h" 8 #include "../../../third_party/numerics/safe_math.h"
9 9
10 static CFX_StringDataW* FX_AllocStringW(int nLen) 10 static CFX_StringDataW* FX_AllocStringW(int nLen)
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 return m_pData->m_String; 391 return m_pData->m_String;
392 } 392 }
393 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len) 393 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len)
394 { 394 {
395 CFX_WideString result; 395 CFX_WideString result;
396 result.ConvertFrom(CFX_ByteString(str, len)); 396 result.ConvertFrom(CFX_ByteString(str, len));
397 return result; 397 return result;
398 } 398 }
399 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len) 399 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len)
400 { 400 {
401 if (!str) { 401 if (!str || 0 == len) {
402 return CFX_WideString(); 402 return CFX_WideString();
403 } 403 }
404 if (len < 0) { 404
405 len = 0;
406 while (str[len]) {
407 len ++;
408 }
409 }
410 CFX_UTF8Decoder decoder; 405 CFX_UTF8Decoder decoder;
411 for (FX_STRSIZE i = 0; i < len; i ++) { 406 for (FX_STRSIZE i = 0; i < len; i ++) {
412 decoder.Input(str[i]); 407 decoder.Input(str[i]);
413 } 408 }
414 return decoder.GetResult(); 409 return decoder.GetResult();
415 } 410 }
416 CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, FX_STRSIZ E wlen) 411 CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, FX_STRSIZ E wlen)
417 { 412 {
418 if (!wstr || !wlen) { 413 if (!wstr || 0 == wlen) {
419 return CFX_WideString(); 414 return CFX_WideString();
420 } 415 }
421 if (wlen < 0) { 416
422 wlen = 0;
423 while (wstr[wlen]) {
424 wlen ++;
425 }
426 }
427 CFX_WideString result; 417 CFX_WideString result;
428 FX_WCHAR* buf = result.GetBuffer(wlen); 418 FX_WCHAR* buf = result.GetBuffer(wlen);
429 for (int i = 0; i < wlen; i ++) { 419 for (int i = 0; i < wlen; i ++) {
430 buf[i] = wstr[i]; 420 buf[i] = wstr[i];
431 } 421 }
432 result.ReleaseBuffer(wlen); 422 result.ReleaseBuffer(wlen);
433 return result; 423 return result;
434 } 424 }
425 FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str)
426 {
427 FX_STRSIZE len = 0;
428 if (str)
429 while (str[len]) len++;
430 return len;
431 }
432
433
434
435 void CFX_WideString::AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STR SIZE nCopyIndex) const 435 void CFX_WideString::AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STR SIZE nCopyIndex) const
436 { 436 {
437 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It 437 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It
438 // should be a |size_t|, or at least unsigned. 438 // should be a |size_t|, or at least unsigned.
439 if (nCopyLen == 0 || nCopyLen < 0) { 439 if (nCopyLen == 0 || nCopyLen < 0) {
440 return; 440 return;
441 } 441 }
442 base::CheckedNumeric<FX_STRSIZE> iSize = static_cast<FX_STRSIZE>(sizeof(FX_W CHAR)); 442 base::CheckedNumeric<FX_STRSIZE> iSize = static_cast<FX_STRSIZE>(sizeof(FX_W CHAR));
443 iSize *= nCopyLen; 443 iSize *= nCopyLen;
444 ASSERT(dest.m_pData == NULL); 444 ASSERT(dest.m_pData == NULL);
(...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 return (CFX_CharMap*)&g_DefaultJISMapper; 1120 return (CFX_CharMap*)&g_DefaultJISMapper;
1121 case 936: 1121 case 936:
1122 return (CFX_CharMap*)&g_DefaultGBKMapper; 1122 return (CFX_CharMap*)&g_DefaultGBKMapper;
1123 case 949: 1123 case 949:
1124 return (CFX_CharMap*)&g_DefaultUHCMapper; 1124 return (CFX_CharMap*)&g_DefaultUHCMapper;
1125 case 950: 1125 case 950:
1126 return (CFX_CharMap*)&g_DefaultBig5Mapper; 1126 return (CFX_CharMap*)&g_DefaultBig5Mapper;
1127 } 1127 }
1128 return NULL; 1128 return NULL;
1129 } 1129 }
OLDNEW
« no previous file with comments | « core/src/fxcrt/fx_basic_util.cpp ('k') | core/src/fxge/win32/fx_win32_device.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698