@@ -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