Repository navigation
[Analyzer] Prevent behavioral change in built-in operators for IntPtr and UIntPtr #74022
Copy link
Copy link
Closed
dotnet/roslyn-analyzers
#6153Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Runtimeblocking-releasecode-analyzerMarks an issue that suggests a Roslyn analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerMarks an issue that suggests a Roslyn code fixerin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Milestone
Description
Related to #72348 (comment)
With numeric IntPtr feature, System.IntPtr and System.UIntPtr gained some built-in operators (conversions, unary and binary). Those would now throw when overflowing within checked context, which is causing behavioral change in .NET 7
Detect places where the addition, subtraction, and explicit cast operators were used on U?IntPtr, while taking the overflow checking context into account
- For
IntPtroperator +(IntPtr, int): Unchecked: same behavior; Checked: may overflow now when it didn't before; Wrap in unchecked context to prevent the overflow and preserve behavioroperator -(IntPtr, int): Unchecked: same behavior; Checked: may overflow now when it didn't before; Wrap in unchecked context to prevent the overflow and preserve behaviorexplicit operator IntPtr(long): Checked: same behavior; Unchecked: will not throw an overflow exception when it could have before in 32-bit contexts; Wrap in a checked context to ensure the exception will be thrown as before in 32-bit contextsexplicit operator void*(IntPtr): Unchecked: same behavior; Checked: may overflow now when it didn't before; Wrap in unchecked context to prevent the overflow and preserve behaviorexplicit operator IntPtr(void*): Unchecked: same behavior; Checked: may overflow now when it didn't before; Wrap in unchecked context to prevent the overflow and preserve behaviorexplicit operator int(IntPtr): Checked: same behavior; Unchecked: will not throw an overflow exception when it could have before in 64-bit contexts; Wrap in checked context to ensure the exception will be thrown as before in 64-bit contexts
- For
UIntPtroperator +(UIntPtr, int): Unchecked: same behavior; Checked: may overflow now when it didn't before; Wrap in unchecked context to prevent the overflow and preserve behavioroperator -(UIntPtr, int): Unchecked: same behavior; Checked: may overflow now when it didn't before; Wrap in unchecked context to prevent the overflow and preserve behaviorexplicit operator UIntPtr(ulong): Checked: same behavior; Unchecked: will not throw an overflow exception when it could have before in 32-bit contexts; Wrap in a checked context to ensure the exception will be thrown as before in 32-bit contextsexplicit operator uint(UIntPtr): Checked: same behavior; Unchecked: will not throw an overflow exception when it could have before in 64-bit contexts; Wrap in checked context to ensure the exception will be thrown as before in 64-bit contexts
- These analyzer should be triggered only if the underlying runtime supports numeric IntPtr feature, i.e. it could check if
System.Runtime.CompilerServices.RuntimeFeature.NumericIntPtris available in corelib as mentioned here
Suggested severity : "Warning"
Suggested category : "Usage" or "Reliability"
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Runtimeblocking-releasecode-analyzerMarks an issue that suggests a Roslyn analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixerMarks an issue that suggests a Roslyn code fixerin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Activity
buyaa-n commentedon Aug 16, 2022
Example:
filipnavara commentedon Aug 17, 2022
Behavioral change relative to what? Xamarin
n[u]inttypes? Previously you could not use subtraction and addition on[U]IntPtr.tannergooding commentedon Aug 17, 2022
IntPtrandUIntPtrhave exposed various "user-defined" operators since .NET Framework 4.0. These operators are called out in the top post and includeIntPtr + int,IntPtr - int,UIntPtr + int, andUIntPtr - int(as well as the conversions to/fromint,long,uint, andulong).[-]Prevent behavioral change in built-in operators for IntPtr and UIntPtr[/-][+][Analyzer] Prevent behavioral change in built-in operators for IntPtr and UIntPtr[/+]terrajobst commentedon Aug 23, 2022
Video
Looks good as proposed.
Severity: Warning
Category: Reliability
buyaa-n commentedon Sep 21, 2022
After testing the analyzer with runtime and few other repo we decided to degrade the severity to info level. Also the fix for findings not that simple, might better to convert the type
nint/nuintor to a real pointer, or wrap with unchecked/checked statements. So suggesting a correct code fix is not feasible, instead we should document the possible fixes well.