From 43da188b3b24514f901d58d8bfc1dbdbf2d9d9a1 Mon Sep 17 00:00:00 2001
From: Leon Styhre <leon@leonstyhre.com>
Date: Sat, 16 Jan 2021 17:22:12 +0100
Subject: [PATCH] Fixed an issue where a leading Unicode character in the game
 name could crash the application.

---
 es-app/src/guis/GuiGamelistOptions.cpp           | 16 +++++++++++++---
 .../src/views/gamelist/ISimpleGameListView.cpp   | 10 +++++++++-
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/es-app/src/guis/GuiGamelistOptions.cpp b/es-app/src/guis/GuiGamelistOptions.cpp
index 206e2f564..9df1d8a08 100644
--- a/es-app/src/guis/GuiGamelistOptions.cpp
+++ b/es-app/src/guis/GuiGamelistOptions.cpp
@@ -92,10 +92,20 @@ GuiGamelistOptions::GuiGamelistOptions(
                     ViewController::FAVORITE_CHAR))
                 isFavorite = true;
 
-            if (mFavoritesSorting && file->getFavorite() && isFavorite)
+            if (mFavoritesSorting && file->getFavorite() && isFavorite) {
                 mCurrentFirstCharacter = ViewController::FAVORITE_CHAR;
-            else
-                mCurrentFirstCharacter = toupper(file->getSortName().front());
+            }
+            else {
+                unsigned char checkCharType = file->getSortName().front();
+                if (checkCharType <= 0x7F) // Normal ASCII character.
+                    mCurrentFirstCharacter = toupper(file->getSortName().front());
+                else if (checkCharType >= 0xF0) // Four-byte Unicode character.
+                    mCurrentFirstCharacter = file->getSortName().substr(0, 4);
+                else if (checkCharType >= 0xE0) // Three-byte Unicode character.
+                    mCurrentFirstCharacter = file->getSortName().substr(0, 3);
+                else if (checkCharType >= 0xC0) // Two-byte Unicode character.
+                    mCurrentFirstCharacter = file->getSortName().substr(0, 2);
+            }
         }
 
         mJumpToLetterList = std::make_shared<LetterList>(mWindow, getHelpStyle(),
diff --git a/es-app/src/views/gamelist/ISimpleGameListView.cpp b/es-app/src/views/gamelist/ISimpleGameListView.cpp
index 337e253d7..7ccd9f476 100644
--- a/es-app/src/views/gamelist/ISimpleGameListView.cpp
+++ b/es-app/src/views/gamelist/ISimpleGameListView.cpp
@@ -489,7 +489,15 @@ void ISimpleGameListView::generateFirstLetterIndex(const std::vector<FileData*>&
             hasFavorites = true;
         }
         else {
-            firstChar = toupper((*it)->getSortName().front());
+            unsigned char checkCharType = (*it)->getSortName().front();
+            if (checkCharType <= 0x7F) // Normal ASCII character.
+                firstChar = toupper((*it)->getSortName().front());
+            else if (checkCharType >= 0xF0) // Four-byte Unicode character.
+                firstChar = (*it)->getSortName().substr(0, 4);
+            else if (checkCharType >= 0xE0) // Three-byte Unicode character.
+                firstChar = (*it)->getSortName().substr(0, 3);
+            else if (checkCharType >= 0xC0) // Two-byte Unicode character.
+                firstChar = (*it)->getSortName().substr(0, 2);
             mFirstLetterIndex.push_back(firstChar);
         }
     }