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

Commit

Permalink
Merged PR 95709: Fix validating RegionInfo names with long string
Browse files Browse the repository at this point in the history
We validate the length of the region info name before we proceed to avoid running into buffer overruns.
  • Loading branch information
tarekgh committed Nov 27, 2017
1 parent 9934e36 commit 8ce622d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
Expand Up @@ -276,6 +276,7 @@ private String[] GetShortTimeFormats()
// region name match the requested region name
private static CultureData GetCultureDataFromRegionName(String regionName)
{
Debug.Assert(!GlobalizationMode.Invariant);
Debug.Assert(regionName != null);

const uint LOCALE_SUPPLEMENTAL = 0x00000002;
Expand Down
35 changes: 21 additions & 14 deletions src/mscorlib/src/System/Globalization/CultureData.cs
Expand Up @@ -369,7 +369,7 @@ internal static CultureData GetCultureDataForRegion(String cultureName, bool use
}

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

Expand All @@ -429,7 +429,7 @@ internal static CultureInfo[] GetCultures(CultureTypes types)
// Remove the enum as it is an no-op.
types &= (~CultureTypes.WindowsOnlyCultures);
}

if (GlobalizationMode.Invariant)
{
// in invariant mode we always return invariant culture only from the enumeration
Expand Down Expand Up @@ -539,7 +539,7 @@ private static CultureData CreateCultureWithInvariantData()
invariant._iDefaultOemCodePage = 437; // default oem code page ID (OCP or OEM)
invariant._iDefaultMacCodePage = 10000; // default macintosh code page
invariant._iDefaultEbcdicCodePage = 037; // default EBCDIC code page

if (GlobalizationMode.Invariant)
{
invariant._sLocalizedDisplayName = invariant._sNativeDisplayName;
Expand Down Expand Up @@ -631,7 +631,11 @@ private static unsafe string NormalizeCultureName(string name, out bool isNeutra
isNeutralName = true;
int i = 0;

Debug.Assert(name.Length <= LOCALE_NAME_MAX_LENGTH);
if (name.Length > LOCALE_NAME_MAX_LENGTH)
{
// Theoretically we shouldn't hit this exception.
throw new ArgumentException(SR.Format(SR.Argument_InvalidId, nameof(name)));
}

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

if (changed)
return new string(pName, 0, name.Length);

return name;
}

private static CultureData CreateCultureData(string cultureName, bool useUserOverride)
{
if (GlobalizationMode.Invariant)
{
CultureInfo.VerifyCultureName(cultureName, true);
if (cultureName.Length > LOCALE_NAME_MAX_LENGTH || !CultureInfo.VerifyCultureName(cultureName, false))
{
return null;
}
CultureData cd = CreateCultureWithInvariantData();
cd._bUseOverrides = useUserOverride;
cd._sName = NormalizeCultureName(cultureName, out cd._bNeutral);
Expand Down Expand Up @@ -749,7 +756,7 @@ internal static CultureData GetCultureData(int culture, bool bUseUserOverride)

if (culture == CultureInfo.LOCALE_INVARIANT)
return Invariant;

if (GlobalizationMode.Invariant)
{
// LCID is not supported in the InvariantMode
Expand Down Expand Up @@ -894,7 +901,7 @@ internal String SLOCALIZEDDISPLAYNAME
}
else
{
// Usually the UI culture shouldn't be different than what we got from WinRT except
// Usually the UI culture shouldn't be different than what we got from WinRT except
// if DefaultThreadCurrentUICulture was set
CultureInfo ci;

Expand Down Expand Up @@ -1065,7 +1072,7 @@ internal String SLOCALIZEDLANGUAGE
{
if (_sLocalizedLanguage == null)
{
// Usually the UI culture shouldn't be different than what we got from WinRT except
// Usually the UI culture shouldn't be different than what we got from WinRT except
// if DefaultThreadCurrentUICulture was set
CultureInfo ci;

Expand Down Expand Up @@ -1153,7 +1160,7 @@ internal string SLOCALIZEDCOUNTRY
}
catch (Exception)
{
// do nothing. we'll fallback
// do nothing. we'll fallback
}

if (_sLocalizedCountry == null)
Expand Down Expand Up @@ -2390,8 +2397,8 @@ internal void GetNFIValues(NumberFormatInfo nfi)
// This is ONLY used for caching names and shouldn't be used for anything else
internal static string AnsiToLower(string testString)
{
int index = 0;
int index = 0;

while (index<testString.Length && (testString[index]<'A' || testString[index]>'Z' ))
{
index++;
Expand All @@ -2400,7 +2407,7 @@ internal static string AnsiToLower(string testString)
{
return testString; // we didn't really change the string
}

StringBuilder sb = new StringBuilder(testString.Length);
for (int i=0; i<index; i++)
{
Expand Down

0 comments on commit 8ce622d

Please sign in to comment.