Fix overflow exception (#3142)

* refactor(TimestampTagStyles): replace ASCII integer values with character literals (#1)

* fix: validate min/max value against OverflowException
This commit is contained in:
OverwrittenCode
2025-06-07 10:35:36 +01:00
committed by GitHub
parent 7a74f781ae
commit 0c1536d48a
2 changed files with 25 additions and 0 deletions

View File

@@ -8,6 +8,21 @@ namespace Discord
{ {
internal static class ObjectExtensions internal static class ObjectExtensions
{ {
private const long Int53Max = (1L << 53) - 1;
private const long Int53Min = -Int53Max;
public static (double Min, double Max) GetSupportedNumericalRange(this object o)
=> Type.GetTypeCode(o.GetType()) switch
{
TypeCode.Byte => (byte.MinValue, byte.MaxValue),
TypeCode.SByte => (sbyte.MinValue, sbyte.MaxValue),
TypeCode.Int16 => (short.MinValue, short.MaxValue),
TypeCode.UInt16 => (ushort.MinValue, ushort.MaxValue),
TypeCode.Int32 => (int.MinValue, int.MaxValue),
TypeCode.UInt32 => (uint.MinValue, uint.MaxValue),
_ => (Int53Min, Int53Max)
};
public static bool IsNumericType(this object o) public static bool IsNumericType(this object o)
{ {
switch (Type.GetTypeCode(o.GetType())) switch (Type.GetTypeCode(o.GetType()))

View File

@@ -466,6 +466,10 @@ namespace Discord.Interactions.Builders
builder.IsRequired = !paramInfo.IsOptional; builder.IsRequired = !paramInfo.IsOptional;
builder.DefaultValue = paramInfo.DefaultValue; builder.DefaultValue = paramInfo.DefaultValue;
var supportedNumericalRange = paramInfo.GetSupportedNumericalRange();
builder.MinValue = supportedNumericalRange.Min;
builder.MaxValue = supportedNumericalRange.Max;
foreach (var attribute in attributes) foreach (var attribute in attributes)
{ {
switch (attribute) switch (attribute)
@@ -497,9 +501,15 @@ namespace Discord.Interactions.Builders
builder.WithAutocompleteHandler(autocomplete.AutocompleteHandlerType, services); builder.WithAutocompleteHandler(autocomplete.AutocompleteHandlerType, services);
break; break;
case MaxValueAttribute maxValue: case MaxValueAttribute maxValue:
if (maxValue.Value > supportedNumericalRange.Max)
throw new ArgumentOutOfRangeException($"{nameof(maxValue)} cannot be greater than {supportedNumericalRange.Max}.");
builder.MaxValue = maxValue.Value; builder.MaxValue = maxValue.Value;
break; break;
case MinValueAttribute minValue: case MinValueAttribute minValue:
if (minValue.Value < supportedNumericalRange.Min)
throw new ArgumentOutOfRangeException($"{nameof(minValue)} cannot be less than {supportedNumericalRange.Min}.");
builder.MinValue = minValue.Value; builder.MinValue = minValue.Value;
break; break;
case MinLengthAttribute minLength: case MinLengthAttribute minLength: