ardalis / ardalis/SmartEnum

SmartFlagEnum does not support values larger than int.MaxValue

Open
#408 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2.4k
Forks
184
PR merge metrics
No merged PRs in 30d

Description

Given a enum class as follow:

``` csharp
[AllowUnsafeFlagEnumValues]
public sealed class FooEnum : SmartFlagEnum {
public static readonly FooEnum Flag32 = new(nameof(Flag32), 0x01u << 31);

public static readonly FooEnum Flag1 = new(nameof(Flag1), 0x01u << 0);

FooEnum(string name, uint value) : base(name, value) { }
}
```

The following code
``` csharp
FooEnum.FromValue(0b_0000_0001u)
```

throws exception:
```
Unhandled exception. System.OverflowException: Value was either too large or too small for an Int32.
at System.Number.ThrowOverflowOrFormatException(ParsingStatus status, ReadOnlySpan`1 value, TypeCode type)
at System.Int32.Parse(String s)
at Ardalis.SmartEnum.SmartFlagEngine`2.HighestFlagValue(IReadOnlyList`1 enumList)
at Ardalis.SmartEnum.SmartFlagEngine`2.CalculateHighestAllowedFlagValue(List`1 inputEnumList)
at Ardalis.SmartEnum.SmartFlagEngine`2.GetFlagEnumValues(TValue value, IEnumerable`1 allEnumList)
at Ardalis.SmartEnum.SmartFlagEnum`2.FromValue(TValue value)
```

Contributor guide

Open the contributing guide

Research direction

Start at SmartFlagEngine.HighestFlagValue and CalculateHighestAllowedFlagValue, using SmartFlagEnum.FromValue with the FooEnum uint reproduction. Trace how the flag value is parsed and verify that values above int.MaxValue no longer throw while matching flags are returned.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.