RedfishPkg/BaseUcs2Utf8Lib: Fix out of bounds shift in UTF8ToUCS2Char

Missing masks leads to shift out of bounds. Also there is no need to
construct CHAR16 using cast to CHAR8 buffer, better to use native endian
by assigning data directly into Ucs2Char variable

Signed-off-by: Savva Mitrofanov <savvamtr@gmail.com>
Reviewed-by: Marvin Häuser <mhaeuser@posteo.de>
This commit is contained in:
Savva Mitrofanov 2022-10-26 00:00:46 +06:00 committed by Mikhail Krichanov
parent 34ed115cd4
commit f47aa892ca
1 changed files with 4 additions and 10 deletions

View File

@ -173,14 +173,12 @@ UTF8ToUCS2Char (
) )
{ {
UINT8 Utf8Size; UINT8 Utf8Size;
CHAR8 *Ucs2Buffer;
CHAR8 TempChar1; CHAR8 TempChar1;
CHAR8 TempChar2; CHAR8 TempChar2;
CHAR8 TempChar3; CHAR8 TempChar3;
ASSERT (Utf8Buffer != NULL && Ucs2Char != NULL); ASSERT (Utf8Buffer != NULL && Ucs2Char != NULL);
ZeroMem (Ucs2Char, sizeof (CHAR16)); *Ucs2Char = 0;
Ucs2Buffer = (CHAR8 *)Ucs2Char;
Utf8Size = GetUTF8SizeForUCS2 (Utf8Buffer); Utf8Size = GetUTF8SizeForUCS2 (Utf8Buffer);
switch (Utf8Size) { switch (Utf8Size) {
@ -194,8 +192,7 @@ UTF8ToUCS2Char (
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
*Ucs2Buffer = TempChar1; *Ucs2Char = (CHAR16)TempChar1;
*(Ucs2Buffer + 1) = 0;
break; break;
case 2: case 2:
@ -213,8 +210,7 @@ UTF8ToUCS2Char (
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
*Ucs2Buffer = (TempChar1 << 6) + (TempChar2 & 0x3F); *Ucs2Char = (TempChar1 & 0x1F) << 6 | (TempChar2 & 0x3F);
*(Ucs2Buffer + 1) = (TempChar1 >> 2) & 0x07;
break; break;
case 3: case 3:
@ -237,9 +233,7 @@ UTF8ToUCS2Char (
return EFI_INVALID_PARAMETER; return EFI_INVALID_PARAMETER;
} }
*Ucs2Buffer = (TempChar2 << 6) + (TempChar3 & 0x3F); *Ucs2Char = (TempChar1 & 0x0F) << 12 | (TempChar2 & 0x3F) << 6 | (TempChar3 & 0x3F);
*(Ucs2Buffer + 1) = (TempChar1 << 4) + ((TempChar2 >> 2) & 0x0F);
break; break;
default: default: