Skip to content

Commit 0c1536d

Browse files
Fix overflow exception (#3142)
* refactor(TimestampTagStyles): replace ASCII integer values with character literals (#1) * fix: validate min/max value against OverflowException
1 parent 7a74f78 commit 0c1536d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Discord.Net.Core/Extensions/ObjectExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ namespace Discord
88
{
99
internal static class ObjectExtensions
1010
{
11+
private const long Int53Max = (1L << 53) - 1;
12+
private const long Int53Min = -Int53Max;
13+
14+
public static (double Min, double Max) GetSupportedNumericalRange(this object o)
15+
=> Type.GetTypeCode(o.GetType()) switch
16+
{
17+
TypeCode.Byte => (byte.MinValue, byte.MaxValue),
18+
TypeCode.SByte => (sbyte.MinValue, sbyte.MaxValue),
19+
TypeCode.Int16 => (short.MinValue, short.MaxValue),
20+
TypeCode.UInt16 => (ushort.MinValue, ushort.MaxValue),
21+
TypeCode.Int32 => (int.MinValue, int.MaxValue),
22+
TypeCode.UInt32 => (uint.MinValue, uint.MaxValue),
23+
_ => (Int53Min, Int53Max)
24+
};
25+
1126
public static bool IsNumericType(this object o)
1227
{
1328
switch (Type.GetTypeCode(o.GetType()))

src/Discord.Net.Interactions/Builders/ModuleClassBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,10 @@ private static void BuildSlashParameter(SlashCommandParameterBuilder builder, Pa
466466
builder.IsRequired = !paramInfo.IsOptional;
467467
builder.DefaultValue = paramInfo.DefaultValue;
468468

469+
var supportedNumericalRange = paramInfo.GetSupportedNumericalRange();
470+
builder.MinValue = supportedNumericalRange.Min;
471+
builder.MaxValue = supportedNumericalRange.Max;
472+
469473
foreach (var attribute in attributes)
470474
{
471475
switch (attribute)
@@ -497,9 +501,15 @@ private static void BuildSlashParameter(SlashCommandParameterBuilder builder, Pa
497501
builder.WithAutocompleteHandler(autocomplete.AutocompleteHandlerType, services);
498502
break;
499503
case MaxValueAttribute maxValue:
504+
if (maxValue.Value > supportedNumericalRange.Max)
505+
throw new ArgumentOutOfRangeException($"{nameof(maxValue)} cannot be greater than {supportedNumericalRange.Max}.");
506+
500507
builder.MaxValue = maxValue.Value;
501508
break;
502509
case MinValueAttribute minValue:
510+
if (minValue.Value < supportedNumericalRange.Min)
511+
throw new ArgumentOutOfRangeException($"{nameof(minValue)} cannot be less than {supportedNumericalRange.Min}.");
512+
503513
builder.MinValue = minValue.Value;
504514
break;
505515
case MinLengthAttribute minLength:

0 commit comments

Comments
 (0)