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: third_party/WebKit/Source/wtf/text/TextCodecUserDefined.cpp

Issue 2464373006: Fix vector resize logic when encoding non-BMP chars to x-user-defined (Closed)
Patch Set: rebased Created 4 years, 1 month 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007, 2008 Apple, Inc. All rights reserved. 2 * Copyright (C) 2007, 2008 Apple, Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 result.append(static_cast<UChar>(c & 0xF7FF)); 62 result.append(static_cast<UChar>(c & 0xF7FF));
63 } 63 }
64 64
65 return result.toString(); 65 return result.toString();
66 } 66 }
67 67
68 template <typename CharType> 68 template <typename CharType>
69 static CString encodeComplexUserDefined(const CharType* characters, 69 static CString encodeComplexUserDefined(const CharType* characters,
70 size_t length, 70 size_t length,
71 UnencodableHandling handling) { 71 UnencodableHandling handling) {
72 Vector<char> result(length); 72 size_t targetLength = length;
73 Vector<char> result(targetLength);
73 char* bytes = result.data(); 74 char* bytes = result.data();
74 75
75 size_t resultLength = 0; 76 size_t resultLength = 0;
76 for (size_t i = 0; i < length;) { 77 for (size_t i = 0; i < length;) {
77 UChar32 c; 78 UChar32 c;
79 // TODO(jsbell): Will the input for x-user-defined ever be LChars?
78 U16_NEXT(characters, i, length, c); 80 U16_NEXT(characters, i, length, c);
81 // If the input was a surrogate pair (non-BMP character) then we
82 // overestimated the length.
83 if (c > 0xffff)
84 --targetLength;
79 signed char signedByte = static_cast<signed char>(c); 85 signed char signedByte = static_cast<signed char>(c);
80 if ((signedByte & 0xF7FF) == c) { 86 if ((signedByte & 0xF7FF) == c) {
81 bytes[resultLength++] = signedByte; 87 bytes[resultLength++] = signedByte;
82 } else { 88 } else {
83 // No way to encode this character with x-user-defined. 89 // No way to encode this character with x-user-defined.
84 UnencodableReplacementArray replacement; 90 UnencodableReplacementArray replacement;
85 int replacementLength = 91 int replacementLength =
86 TextCodec::getUnencodableReplacement(c, handling, replacement); 92 TextCodec::getUnencodableReplacement(c, handling, replacement);
87 result.grow(resultLength + replacementLength + length - i); 93 DCHECK_GT(replacementLength, 0);
88 bytes = result.data(); 94 // Only one char was initially reserved per input character, so grow if
95 // necessary. Note that in the case of surrogate pairs and
96 // QuestionMarksForUnencodables the result length may be shorter than
97 // the input length.
98 targetLength += replacementLength - 1;
99 if (targetLength > result.size()) {
100 result.grow(targetLength);
101 bytes = result.data();
102 }
89 memcpy(bytes + resultLength, replacement, replacementLength); 103 memcpy(bytes + resultLength, replacement, replacementLength);
90 resultLength += replacementLength; 104 resultLength += replacementLength;
91 } 105 }
92 } 106 }
93 107
94 return CString(bytes, resultLength); 108 return CString(bytes, resultLength);
95 } 109 }
96 110
97 template <typename CharType> 111 template <typename CharType>
98 CString TextCodecUserDefined::encodeCommon(const CharType* characters, 112 CString TextCodecUserDefined::encodeCommon(const CharType* characters,
(...skipping 24 matching lines...) Expand all
123 return encodeCommon(characters, length, handling); 137 return encodeCommon(characters, length, handling);
124 } 138 }
125 139
126 CString TextCodecUserDefined::encode(const LChar* characters, 140 CString TextCodecUserDefined::encode(const LChar* characters,
127 size_t length, 141 size_t length,
128 UnencodableHandling handling) { 142 UnencodableHandling handling) {
129 return encodeCommon(characters, length, handling); 143 return encodeCommon(characters, length, handling);
130 } 144 }
131 145
132 } // namespace WTF 146 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/BUILD.gn ('k') | third_party/WebKit/Source/wtf/text/TextCodecUserDefinedTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698