Skip to content

Commit fd3ba42

Browse files
Frediano Zigliosmfrench
authored andcommitted
Convert properly UTF-8 to UTF-16
wchar_t is currently 16bit so converting a utf8 encoded characters not in plane 0 (>= 0x10000) to wchar_t (that is calling char2uni) lead to a -EINVAL return. This patch detect utf8 in cifs_strtoUTF16 and add special code calling utf8s_to_utf16s. Signed-off-by: Frediano Ziglio <frediano.ziglio@citrix.com> Acked-by: Jeff Layton <jlayton@redhat.com> Signed-off-by: Steve French <smfrench@gmail.com>
1 parent b7a1062 commit fd3ba42

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

fs/cifs/cifs_unicode.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,27 @@ cifs_strtoUTF16(__le16 *to, const char *from, int len,
203203
int i;
204204
wchar_t wchar_to; /* needed to quiet sparse */
205205

206+
/* special case for utf8 to handle no plane0 chars */
207+
if (!strcmp(codepage->charset, "utf8")) {
208+
/*
209+
* convert utf8 -> utf16, we assume we have enough space
210+
* as caller should have assumed conversion does not overflow
211+
* in destination len is length in wchar_t units (16bits)
212+
*/
213+
i = utf8s_to_utf16s(from, len, UTF16_LITTLE_ENDIAN,
214+
(wchar_t *) to, len);
215+
216+
/* if success terminate and exit */
217+
if (i >= 0)
218+
goto success;
219+
/*
220+
* if fails fall back to UCS encoding as this
221+
* function should not return negative values
222+
* currently can fail only if source contains
223+
* invalid encoded characters
224+
*/
225+
}
226+
206227
for (i = 0; len && *from; i++, from += charlen, len -= charlen) {
207228
charlen = codepage->char2uni(from, len, &wchar_to);
208229
if (charlen < 1) {
@@ -215,6 +236,7 @@ cifs_strtoUTF16(__le16 *to, const char *from, int len,
215236
put_unaligned_le16(wchar_to, &to[i]);
216237
}
217238

239+
success:
218240
put_unaligned_le16(0, &to[i]);
219241
return i;
220242
}

0 commit comments

Comments
 (0)