fs: exfat: Fix conversion overflow errors
authorMarek Vasut <marex@denx.de>
Mon, 17 Mar 2025 03:12:47 +0000 (04:12 +0100)
committerTom Rini <trini@konsulko.com>
Thu, 3 Apr 2025 02:00:59 +0000 (20:00 -0600)
Fix the following conversion overflow errors. The UTF8-to-UTF16
conversion is done through 32bit wchar_t, but U-Boot codebase is
built with -fshort-wchar which limits wchar_t to 16bit. Replace
the built-in wchar_t with u32 to assure the intermediate type is
32bit.

"
fs/exfat/utf.c: In function ‘utf8_to_wchar’:
fs/exfat/utf.c:165:23: warning: overflow in conversion from ‘int’ to ‘wchar_t’ {aka ‘short unsigned int’} changes value from ‘(int)(short unsigned int)*input << 18 & 1835008’ to ‘0’ [-Woverflow]
  165 |                 *wc = ((wchar_t) input[0] & 0x07) << 18;
      |                       ^
fs/exfat/utf.c:170:23: warning: overflow in conversion from ‘int’ to ‘wchar_t’ {aka ‘short unsigned int’} changes value from ‘(int)(short unsigned int)*input << 24 & 50331648’ to ‘0’ [-Woverflow]
  170 |                 *wc = ((wchar_t) input[0] & 0x03) << 24;
      |                       ^
fs/exfat/utf.c:175:23: warning: overflow in conversion from ‘int’ to ‘wchar_t’ {aka ‘short unsigned int’} changes value from ‘(int)(short unsigned int)*input << 30 & 1073741824’ to ‘0’ [-Woverflow]
  175 |                 *wc = ((wchar_t) input[0] & 0x01) << 30;
      |                       ^
"

Signed-off-by: Marek Vasut <marex@denx.de>
fs/exfat/utf.c

index b1d09e7..8a28e76 100644 (file)
@@ -23,7 +23,7 @@
 #include "exfat.h"
 #include <errno.h>
 
-static char* wchar_to_utf8(char* output, wchar_t wc, size_t outsize)
+static char* wchar_to_utf8(char* output, u32 wc, size_t outsize)
 {
        if (wc <= 0x7f)
        {
@@ -82,14 +82,14 @@ static char* wchar_to_utf8(char* output, wchar_t wc, size_t outsize)
        return output;
 }
 
-static const le16_t* utf16_to_wchar(const le16_t* input, wchar_t* wc,
+static const le16_t* utf16_to_wchar(const le16_t* input, u32* wc,
                size_t insize)
 {
        if ((le16_to_cpu(input[0]) & 0xfc00) == 0xd800)
        {
                if (insize < 2 || (le16_to_cpu(input[1]) & 0xfc00) != 0xdc00)
                        return NULL;
-               *wc = ((wchar_t) (le16_to_cpu(input[0]) & 0x3ff) << 10);
+               *wc = ((u32) (le16_to_cpu(input[0]) & 0x3ff) << 10);
                *wc |= (le16_to_cpu(input[1]) & 0x3ff);
                *wc += 0x10000;
                return input + 2;
@@ -108,7 +108,7 @@ int exfat_utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
        const le16_t* iend = input + insize;
        char* optr = output;
        const char* oend = output + outsize;
-       wchar_t wc;
+       u32 wc;
 
        while (iptr < iend)
        {
@@ -136,7 +136,7 @@ int exfat_utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
        return 0;
 }
 
-static const char* utf8_to_wchar(const char* input, wchar_t* wc,
+static const char* utf8_to_wchar(const char* input, u32* wc,
                size_t insize)
 {
        size_t size;
@@ -147,32 +147,32 @@ static const char* utf8_to_wchar(const char* input, wchar_t* wc,
 
        if ((input[0] & 0x80) == 0)
        {
-               *wc = (wchar_t) input[0];
+               *wc = (u32) input[0];
                return input + 1;
        }
        else if ((input[0] & 0xe0) == 0xc0)
        {
-               *wc = ((wchar_t) input[0] & 0x1f) << 6;
+               *wc = ((u32) input[0] & 0x1f) << 6;
                size = 2;
        }
        else if ((input[0] & 0xf0) == 0xe0)
        {
-               *wc = ((wchar_t) input[0] & 0x0f) << 12;
+               *wc = ((u32) input[0] & 0x0f) << 12;
                size = 3;
        }
        else if ((input[0] & 0xf8) == 0xf0)
        {
-               *wc = ((wchar_t) input[0] & 0x07) << 18;
+               *wc = ((u32) input[0] & 0x07) << 18;
                size = 4;
        }
        else if ((input[0] & 0xfc) == 0xf8)
        {
-               *wc = ((wchar_t) input[0] & 0x03) << 24;
+               *wc = ((u32) input[0] & 0x03) << 24;
                size = 5;
        }
        else if ((input[0] & 0xfe) == 0xfc)
        {
-               *wc = ((wchar_t) input[0] & 0x01) << 30;
+               *wc = ((u32) input[0] & 0x01) << 30;
                size = 6;
        }
        else
@@ -192,7 +192,7 @@ static const char* utf8_to_wchar(const char* input, wchar_t* wc,
        return input + size;
 }
 
-static le16_t* wchar_to_utf16(le16_t* output, wchar_t wc, size_t outsize)
+static le16_t* wchar_to_utf16(le16_t* output, u32 wc, size_t outsize)
 {
        if (wc <= 0xffff) /* if character is from BMP */
        {
@@ -216,7 +216,7 @@ int exfat_utf8_to_utf16(le16_t* output, const char* input, size_t outsize,
        const char* iend = input + insize;
        le16_t* optr = output;
        const le16_t* oend = output + outsize;
-       wchar_t wc;
+       u32 wc;
 
        while (iptr < iend)
        {