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

Commit 8ce622d

Browse files
committed
Merged PR 95709: Fix validating RegionInfo names with long string
We validate the length of the region info name before we proceed to avoid running into buffer overruns.
1 parent 9934e36 commit 8ce622d

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ private String[] GetShortTimeFormats()
276276
// region name match the requested region name
277277
private static CultureData GetCultureDataFromRegionName(String regionName)
278278
{
279+
Debug.Assert(!GlobalizationMode.Invariant);
279280
Debug.Assert(regionName != null);
280281

281282
const uint LOCALE_SUPPLEMENTAL = 0x00000002;

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

Lines changed: 21 additions & 14 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;
@@ -631,7 +631,11 @@ private static unsafe string NormalizeCultureName(string name, out bool isNeutra
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+
}
635639

636640
char *pName = stackalloc char[LOCALE_NAME_MAX_LENGTH];
637641
bool changed = false;
@@ -673,15 +677,18 @@ private static unsafe string NormalizeCultureName(string name, out bool isNeutra
673677

674678
if (changed)
675679
return new string(pName, 0, name.Length);
676-
680+
677681
return name;
678682
}
679683

680684
private static CultureData CreateCultureData(string cultureName, bool useUserOverride)
681685
{
682686
if (GlobalizationMode.Invariant)
683687
{
684-
CultureInfo.VerifyCultureName(cultureName, true);
688+
if (cultureName.Length > LOCALE_NAME_MAX_LENGTH || !CultureInfo.VerifyCultureName(cultureName, false))
689+
{
690+
return null;
691+
}
685692
CultureData cd = CreateCultureWithInvariantData();
686693
cd._bUseOverrides = useUserOverride;
687694
cd._sName = NormalizeCultureName(cultureName, out cd._bNeutral);
@@ -749,7 +756,7 @@ internal static CultureData GetCultureData(int culture, bool bUseUserOverride)
749756

750757
if (culture == CultureInfo.LOCALE_INVARIANT)
751758
return Invariant;
752-
759+
753760
if (GlobalizationMode.Invariant)
754761
{
755762
// LCID is not supported in the InvariantMode
@@ -894,7 +901,7 @@ internal String SLOCALIZEDDISPLAYNAME
894901
}
895902
else
896903
{
897-
// Usually the UI culture shouldn't be different than what we got from WinRT except
904+
// Usually the UI culture shouldn't be different than what we got from WinRT except
898905
// if DefaultThreadCurrentUICulture was set
899906
CultureInfo ci;
900907

@@ -1065,7 +1072,7 @@ internal String SLOCALIZEDLANGUAGE
10651072
{
10661073
if (_sLocalizedLanguage == null)
10671074
{
1068-
// Usually the UI culture shouldn't be different than what we got from WinRT except
1075+
// Usually the UI culture shouldn't be different than what we got from WinRT except
10691076
// if DefaultThreadCurrentUICulture was set
10701077
CultureInfo ci;
10711078

@@ -1153,7 +1160,7 @@ internal string SLOCALIZEDCOUNTRY
11531160
}
11541161
catch (Exception)
11551162
{
1156-
// do nothing. we'll fallback
1163+
// do nothing. we'll fallback
11571164
}
11581165

11591166
if (_sLocalizedCountry == null)
@@ -2390,8 +2397,8 @@ internal void GetNFIValues(NumberFormatInfo nfi)
23902397
// This is ONLY used for caching names and shouldn't be used for anything else
23912398
internal static string AnsiToLower(string testString)
23922399
{
2393-
int index = 0;
2394-
2400+
int index = 0;
2401+
23952402
while (index<testString.Length && (testString[index]<'A' || testString[index]>'Z' ))
23962403
{
23972404
index++;
@@ -2400,7 +2407,7 @@ internal static string AnsiToLower(string testString)
24002407
{
24012408
return testString; // we didn't really change the string
24022409
}
2403-
2410+
24042411
StringBuilder sb = new StringBuilder(testString.Length);
24052412
for (int i=0; i<index; i++)
24062413
{

0 commit comments

Comments
 (0)