Alexandre Julliard : kernelbase: Fix checks for valid locale in LCMapStringW/Ex.

Alexandre Julliard julliard at winehq.org
Wed Jun 1 15:44:31 CDT 2022


Module: wine
Branch: master
Commit: c2a455a20d7b5124bab0086df75178d8c53f27ef
URL:    https://source.winehq.org/git/wine.git/?a=commit;h=c2a455a20d7b5124bab0086df75178d8c53f27ef

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Wed Jun  1 11:19:24 2022 +0200

kernelbase: Fix checks for valid locale in LCMapStringW/Ex.

Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/kernel32/tests/locale.c | 14 +++++---
 dlls/kernelbase/locale.c     | 86 +++++++++++++++++---------------------------
 2 files changed, 43 insertions(+), 57 deletions(-)

diff --git a/dlls/kernel32/tests/locale.c b/dlls/kernel32/tests/locale.c
index 4aada71bbb0..125d0c1f2ca 100644
--- a/dlls/kernel32/tests/locale.c
+++ b/dlls/kernel32/tests/locale.c
@@ -2669,10 +2669,13 @@ static void test_LCMapStringW(void)
 
     SetLastError(0xdeadbeef);
     ret = LCMapStringW((LCID)-1, LCMAP_LOWERCASE, upper_case, -1, buf, ARRAY_SIZE(buf));
-    todo_wine {
     ok(!ret, "LCMapStringW should fail with bad lcid\n");
     ok(GetLastError() == ERROR_INVALID_PARAMETER, "unexpected error code %ld\n", GetLastError());
-    }
+
+    SetLastError(0xdeadbeef);
+    ret = LCMapStringW((LCID)0xdead, LCMAP_HIRAGANA, upper_case, -1, buf, ARRAY_SIZE(buf));
+    ok(!ret, "LCMapStringW should fail with bad lcid\n");
+    ok(GetLastError() == ERROR_INVALID_PARAMETER, "unexpected error code %ld\n", GetLastError());
 
     test_lcmapstring_unicode(LCMapStringW_wrapper, "LCMapStringW:");
 }
@@ -2698,10 +2701,13 @@ static void test_LCMapStringEx(void)
     SetLastError(0xdeadbeef);
     ret = pLCMapStringEx(invalidW, LCMAP_LOWERCASE,
                          upper_case, -1, buf, ARRAY_SIZE(buf), NULL, NULL, 0);
-    todo_wine {
     ok(!ret, "LCMapStringEx should fail with bad locale name\n");
     ok(GetLastError() == ERROR_INVALID_PARAMETER, "unexpected error code %ld\n", GetLastError());
-    }
+
+    SetLastError(0xdeadbeef);
+    ret = pLCMapStringEx(invalidW, LCMAP_HIRAGANA,
+                         upper_case, -1, buf, ARRAY_SIZE(buf), NULL, NULL, 0);
+    ok(ret, "LCMapStringEx should not fail with bad locale name\n");
 
     /* test reserved parameters */
     ret = pLCMapStringEx(LOCALE_NAME_USER_DEFAULT, LCMAP_LOWERCASE,
diff --git a/dlls/kernelbase/locale.c b/dlls/kernelbase/locale.c
index 02532a7e10b..5a6022addc9 100644
--- a/dlls/kernelbase/locale.c
+++ b/dlls/kernelbase/locale.c
@@ -672,7 +672,12 @@ static const struct sortguid *get_language_sort( const WCHAR *name )
         name = locale_strings + user_locale->sname + 1;
     }
 
-    if (!(entry = find_lcname_entry( name ))) return NULL;
+    if (!(entry = find_lcname_entry( name )))
+    {
+        WARN( "unknown locale %s\n", debugstr_w(name) );
+        SetLastError( ERROR_INVALID_PARAMETER );
+        return NULL;
+    }
     if ((ret = locale_sorts[entry - lcnames_index])) return ret;
 
     lcid = entry->id;
@@ -686,22 +691,17 @@ static const struct sortguid *get_language_sort( const WCHAR *name )
             if (!RegQueryValueExW( key, name, NULL, &type, (BYTE *)guidstr, &size ) && type == REG_SZ)
             {
                 RtlInitUnicodeString( &str, guidstr );
-                if (!RtlGUIDFromString( &str, &guid ))
-                {
-                    ret = find_sortguid( &guid );
-                    goto done;
-                }
+                if (!RtlGUIDFromString( &str, &guid )) ret = find_sortguid( &guid );
                 break;
             }
             if (!name[0]) break;
             name = locale_strings + (SORTIDFROMLCID( lcid ) ? locale->sname : locale->sparent) + 1;
             if (!(locale = get_locale_by_name( name, &lcid ))) break;
         }
+        RegCloseKey( key );
     }
-    ret = &sort.guids[0];
-done:
-    RegCloseKey( key );
-    if (ret) locale_sorts[entry - lcnames_index] = ret;
+    if (!ret) ret = &sort.guids[0];
+    locale_sorts[entry - lcnames_index] = ret;
     return ret;
 }
 
@@ -4484,12 +4484,7 @@ INT WINAPI CompareStringEx( const WCHAR *locale, DWORD flags, const WCHAR *str1,
         return 0;
     }
 
-    if (!(sortid = get_language_sort( locale )))
-    {
-        FIXME( "unknown locale %s\n", debugstr_w(locale) );
-        SetLastError( ERROR_INVALID_PARAMETER );
-        return 0;
-    }
+    if (!(sortid = get_language_sort( locale ))) return 0;
 
     if (!str1 || !str2)
     {
@@ -4971,12 +4966,7 @@ INT WINAPI DECLSPEC_HOTPATCH FindNLSStringEx( const WCHAR *locale, DWORD flags,
         SetLastError( ERROR_INVALID_PARAMETER );
         return -1;
     }
-    if (!(sortid = get_language_sort( locale )))
-    {
-        WARN( "unknown locale %s\n", debugstr_w(locale) );
-        SetLastError( ERROR_INVALID_PARAMETER );
-        return -1;
-    }
+    if (!(sortid = get_language_sort( locale ))) return -1;
 
     if (srclen == -1) srclen = lstrlenW(src);
     if (valuelen == -1) valuelen = lstrlenW(value);
@@ -5634,11 +5624,8 @@ BOOL WINAPI DECLSPEC_HOTPATCH GetNLSVersionEx( NLS_FUNCTION func, const WCHAR *l
         SetLastError( ERROR_INSUFFICIENT_BUFFER );
         return FALSE;
     }
-    if (!(sortid = get_language_sort( locale )))
-    {
-        SetLastError( ERROR_INVALID_PARAMETER );
-        return 0;
-    }
+
+    if (!(sortid = get_language_sort( locale ))) return FALSE;
 
     info->dwNLSVersion = info->dwDefinedVersion = sort.version;
     if (info->dwNLSVersionInfoSize >= sizeof(*info))
@@ -6329,13 +6316,10 @@ INT WINAPI DECLSPEC_HOTPATCH LCMapStringEx( const WCHAR *locale, DWORD flags, co
 
     if (!dstlen) dst = NULL;
 
-    if (flags & (LCMAP_LINGUISTIC_CASING | LCMAP_SORTKEY) && !(sortid = get_language_sort( locale )))
+    if (flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE | LCMAP_SORTKEY))
     {
-        FIXME( "unknown locale %s\n", debugstr_w(locale) );
-        SetLastError( ERROR_INVALID_PARAMETER );
-        return 0;
+        if (!(sortid = get_language_sort( locale ))) return 0;
     }
-
     if (flags & LCMAP_SORTKEY)
     {
         if (src == dst)
@@ -6574,31 +6558,27 @@ INT WINAPI DECLSPEC_HOTPATCH LCMapStringW( LCID lcid, DWORD flags, const WCHAR *
                                            WCHAR *dst, int dstlen )
 {
     const WCHAR *locale = LOCALE_NAME_USER_DEFAULT;
+    const NLS_LOCALE_LCID_INDEX *entry;
 
-    if (flags & (LCMAP_LINGUISTIC_CASING | LCMAP_SORTKEY))
+    switch (lcid)
     {
-        const NLS_LOCALE_LCID_INDEX *entry;
-
-        switch (lcid)
+    case LOCALE_NEUTRAL:
+    case LOCALE_USER_DEFAULT:
+    case LOCALE_SYSTEM_DEFAULT:
+    case LOCALE_CUSTOM_DEFAULT:
+    case LOCALE_CUSTOM_UNSPECIFIED:
+    case LOCALE_CUSTOM_UI_DEFAULT:
+        break;
+    default:
+        if (lcid == user_lcid || lcid == system_lcid) break;
+        if (!(entry = find_lcid_entry( lcid )))
         {
-        case LOCALE_NEUTRAL:
-        case LOCALE_USER_DEFAULT:
-        case LOCALE_SYSTEM_DEFAULT:
-        case LOCALE_CUSTOM_DEFAULT:
-        case LOCALE_CUSTOM_UNSPECIFIED:
-        case LOCALE_CUSTOM_UI_DEFAULT:
-            break;
-        default:
-            if (lcid == user_lcid || lcid == system_lcid) break;
-            if (!(entry = find_lcid_entry( lcid )))
-            {
-                WARN( "unknown locale %04lx\n", lcid );
-                SetLastError( ERROR_INVALID_PARAMETER );
-                return 0;
-            }
-            locale = locale_strings + entry->name + 1;
-            break;
+            WARN( "unknown locale %04lx\n", lcid );
+            SetLastError( ERROR_INVALID_PARAMETER );
+            return 0;
         }
+        locale = locale_strings + entry->name + 1;
+        break;
     }
 
     return LCMapStringEx( locale, flags, src, srclen, dst, dstlen, NULL, NULL, 0 );




More information about the wine-cvs mailing list