@@ -123,10 +123,13 @@ MaybeLocal<Object> ToBufferEndian(Environment* env, MaybeStackBuffer<T>* buf) {
123123
124124void CopySourceBuffer (MaybeStackBuffer<UChar>* dest,
125125 const char * data,
126- const size_t length,
127126 const size_t length_in_chars) {
128127 dest->AllocateSufficientStorage (length_in_chars);
129128 char * dst = reinterpret_cast <char *>(**dest);
129+ // The destination holds length_in_chars UChar units. Copy that many whole
130+ // units and ignore a trailing odd byte; copying the raw byte length would
131+ // write one byte past the buffer when the source length is not even.
132+ const size_t length = length_in_chars * sizeof (UChar);
130133 memcpy (dst, data, length);
131134 if constexpr (IsBigEndian ()) {
132135 CHECK (nbytes::SwapBytes16 (dst, length));
@@ -199,7 +202,7 @@ MaybeLocal<Object> TranscodeFromUcs2(Environment* env,
199202 to.set_subst_chars (sub.c_str ());
200203
201204 const size_t length_in_chars = source_length / sizeof (UChar);
202- CopySourceBuffer (&sourcebuf, source, source_length, length_in_chars);
205+ CopySourceBuffer (&sourcebuf, source, length_in_chars);
203206 MaybeStackBuffer<char > destbuf (length_in_chars);
204207 const uint32_t len = ucnv_fromUChars (to.conv (), *destbuf, length_in_chars,
205208 *sourcebuf, length_in_chars, status);
0 commit comments