Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit dce9085

Browse files
tarekghjkotas
authored andcommitted
Port servicing fix (#15802)
* Port the servicing fix This is porting the servcing fix 8ce622d with a small change to get rid of unsafe code in CultureData per Jan recommendation * remove un-needed lines * rename the variable name as it is not a pointer anymore
1 parent a233648 commit dce9085

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

src/mscorlib/src/System/Globalization/CultureData.Windows.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace System.Globalization
1919
using StringList = List<string>;
2020
#else
2121
using StringList = LowLevelList<string>;
22-
#endif
22+
#endif
2323

2424
internal partial class CultureData
2525
{
@@ -273,10 +273,11 @@ private String[] GetShortTimeFormats()
273273
return result;
274274
}
275275

276-
// Enumerate all system cultures and then try to find out which culture has
276+
// Enumerate all system cultures and then try to find out which culture has
277277
// region name match the requested region name
278278
private static CultureData GetCultureDataFromRegionName(String regionName)
279279
{
280+
Debug.Assert(!GlobalizationMode.Invariant);
280281
Debug.Assert(regionName != null);
281282

282283
const uint LOCALE_SUPPLEMENTAL = 0x00000002;
@@ -327,7 +328,7 @@ private string GetRegionDisplayName(string isoCountryCode)
327328
#if ENABLE_WINRT
328329
return WinRTInterop.Callbacks.GetRegionDisplayName(isoCountryCode);
329330
#else
330-
// If the current UI culture matching the OS UI language, we'll get the display name from the OS.
331+
// If the current UI culture matching the OS UI language, we'll get the display name from the OS.
331332
// otherwise, we use the native name as we don't carry resources for the region display names anyway.
332333
if (CultureInfo.CurrentUICulture.Name.Equals(CultureInfo.UserDefaultUICulture.Name))
333334
{
@@ -692,7 +693,7 @@ private string GetThreeLetterWindowsLanguageName(string cultureName)
692693
private static CultureInfo[] EnumCultures(CultureTypes types)
693694
{
694695
Debug.Assert(!GlobalizationMode.Invariant);
695-
696+
696697
uint flags = 0;
697698

698699
#pragma warning disable 618
@@ -736,7 +737,7 @@ private static CultureInfo[] EnumCultures(CultureTypes types)
736737
cultures[i] = new CultureInfo(context.strings[i]);
737738
}
738739

739-
return cultures;
740+
return cultures;
740741
}
741742

742743
private string GetConsoleFallbackName(string cultureName)

src/mscorlib/src/System/Globalization/CultureData.cs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ internal static CultureData GetCultureDataForRegion(String cultureName, bool use
369369
}
370370

371371
// If not found in the hard coded table we'll have to find a culture that works for us
372-
if (retVal == null || (retVal.IsNeutralCulture == true))
372+
if (!GlobalizationMode.Invariant && (retVal == null || (retVal.IsNeutralCulture == true)))
373373
{
374374
retVal = GetCultureDataFromRegionName(cultureName);
375375
}
@@ -415,7 +415,7 @@ internal static CultureInfo[] GetCultures(CultureTypes types)
415415
CultureTypes.ReplacementCultures | CultureTypes.WindowsOnlyCultures |
416416
CultureTypes.FrameworkCultures)) != 0)
417417
{
418-
throw new ArgumentOutOfRangeException(nameof(types),
418+
throw new ArgumentOutOfRangeException(nameof(types),
419419
SR.Format(SR.ArgumentOutOfRange_Range, CultureTypes.NeutralCultures, CultureTypes.FrameworkCultures));
420420
}
421421

@@ -429,7 +429,7 @@ internal static CultureInfo[] GetCultures(CultureTypes types)
429429
// Remove the enum as it is an no-op.
430430
types &= (~CultureTypes.WindowsOnlyCultures);
431431
}
432-
432+
433433
if (GlobalizationMode.Invariant)
434434
{
435435
// in invariant mode we always return invariant culture only from the enumeration
@@ -539,7 +539,7 @@ private static CultureData CreateCultureWithInvariantData()
539539
invariant._iDefaultOemCodePage = 437; // default oem code page ID (OCP or OEM)
540540
invariant._iDefaultMacCodePage = 10000; // default macintosh code page
541541
invariant._iDefaultEbcdicCodePage = 037; // default EBCDIC code page
542-
542+
543543
if (GlobalizationMode.Invariant)
544544
{
545545
invariant._sLocalizedDisplayName = invariant._sNativeDisplayName;
@@ -626,27 +626,32 @@ internal static CultureData GetCultureData(String cultureName, bool useUserOverr
626626
return culture;
627627
}
628628

629-
private static unsafe string NormalizeCultureName(string name, out bool isNeutralName)
629+
private static string NormalizeCultureName(string name, out bool isNeutralName)
630630
{
631631
isNeutralName = true;
632632
int i = 0;
633633

634-
Debug.Assert(name.Length <= LOCALE_NAME_MAX_LENGTH);
634+
if (name.Length > LOCALE_NAME_MAX_LENGTH)
635+
{
636+
// Theoretically we shouldn't hit this exception.
637+
throw new ArgumentException(SR.Format(SR.Argument_InvalidId, nameof(name)));
638+
}
639+
640+
Span<char> normalizedName = stackalloc char[name.Length];
635641

636-
char *pName = stackalloc char[LOCALE_NAME_MAX_LENGTH];
637642
bool changed = false;
638643

639644
while (i < name.Length && name[i] != '-' && name[i] != '_')
640645
{
641646
if (name[i] >= 'A' && name[i] <= 'Z')
642647
{
643648
// lowercase characters before '-'
644-
pName[i] = (char) (((int)name[i]) + 0x20);
649+
normalizedName[i] = (char) (((int)name[i]) + 0x20);
645650
changed = true;
646651
}
647652
else
648653
{
649-
pName[i] = name[i];
654+
normalizedName[i] = name[i];
650655
}
651656
i++;
652657
}
@@ -661,27 +666,30 @@ private static unsafe string NormalizeCultureName(string name, out bool isNeutra
661666
{
662667
if (name[i] >= 'a' && name[i] <= 'z')
663668
{
664-
pName[i] = (char) (((int)name[i]) - 0x20);
669+
normalizedName[i] = (char) (((int)name[i]) - 0x20);
665670
changed = true;
666671
}
667672
else
668673
{
669-
pName[i] = name[i];
674+
normalizedName[i] = name[i];
670675
}
671676
i++;
672677
}
673678

674679
if (changed)
675-
return new string(pName, 0, name.Length);
676-
680+
return new string(normalizedName);
681+
677682
return name;
678683
}
679684

680685
private static CultureData CreateCultureData(string cultureName, bool useUserOverride)
681686
{
682687
if (GlobalizationMode.Invariant)
683688
{
684-
CultureInfo.VerifyCultureName(cultureName, true);
689+
if (cultureName.Length > LOCALE_NAME_MAX_LENGTH || !CultureInfo.VerifyCultureName(cultureName, false))
690+
{
691+
return null;
692+
}
685693
CultureData cd = CreateCultureWithInvariantData();
686694
cd._bUseOverrides = useUserOverride;
687695
cd._sName = NormalizeCultureName(cultureName, out cd._bNeutral);
@@ -749,7 +757,7 @@ internal static CultureData GetCultureData(int culture, bool bUseUserOverride)
749757

750758
if (culture == CultureInfo.LOCALE_INVARIANT)
751759
return Invariant;
752-
760+
753761
if (GlobalizationMode.Invariant)
754762
{
755763
// LCID is not supported in the InvariantMode
@@ -894,7 +902,7 @@ internal String SLOCALIZEDDISPLAYNAME
894902
}
895903
else
896904
{
897-
// Usually the UI culture shouldn't be different than what we got from WinRT except
905+
// Usually the UI culture shouldn't be different than what we got from WinRT except
898906
// if DefaultThreadCurrentUICulture was set
899907
CultureInfo ci;
900908

@@ -1065,7 +1073,7 @@ internal String SLOCALIZEDLANGUAGE
10651073
{
10661074
if (_sLocalizedLanguage == null)
10671075
{
1068-
// Usually the UI culture shouldn't be different than what we got from WinRT except
1076+
// Usually the UI culture shouldn't be different than what we got from WinRT except
10691077
// if DefaultThreadCurrentUICulture was set
10701078
CultureInfo ci;
10711079

@@ -1153,7 +1161,7 @@ internal string SLOCALIZEDCOUNTRY
11531161
}
11541162
catch (Exception)
11551163
{
1156-
// do nothing. we'll fallback
1164+
// do nothing. we'll fallback
11571165
}
11581166

11591167
if (_sLocalizedCountry == null)
@@ -2390,8 +2398,8 @@ internal void GetNFIValues(NumberFormatInfo nfi)
23902398
// This is ONLY used for caching names and shouldn't be used for anything else
23912399
internal static string AnsiToLower(string testString)
23922400
{
2393-
int index = 0;
2394-
2401+
int index = 0;
2402+
23952403
while (index<testString.Length && (testString[index]<'A' || testString[index]>'Z' ))
23962404
{
23972405
index++;
@@ -2400,7 +2408,7 @@ internal static string AnsiToLower(string testString)
24002408
{
24012409
return testString; // we didn't really change the string
24022410
}
2403-
2411+
24042412
StringBuilder sb = new StringBuilder(testString.Length);
24052413
for (int i=0; i<index; i++)
24062414
{

0 commit comments

Comments
 (0)