Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Double-to-Long overflow check #73016

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
If sourceValue > &H7000000000000000L Then
Dim temporary As Double = (sourceValue - &H7000000000000000L)

If temporary < &H7000000000000000L AndAlso UncheckedCLng(temporary) > &H1000000000000000L Then
If temporary < &H7000000000000000L AndAlso UncheckedCLng(temporary) < &H1000000000000000L Then
Return False
End If
Else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ End Class
New TypeAndValue(uint64Type, CULng(18)),
New TypeAndValue(decimalType, CDec(-11.3)),
New TypeAndValue(doubleType, CDbl(&HF000000000000000UL)),
New TypeAndValue(doubleType, CDbl(&H70000000000000F0L)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&H70000000000000F0L

Would it make sense to use the other value from the description, &H7000000000000400L, instead of this case?

Copy link
Contributor

@AlekseyTs AlekseyTs May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New TypeAndValue(doubleType, CDbl(&H70000000000000F0L)),

Please keep this value #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should keep CDbl(&H70000000000000F0L) because it's misleading: the integer &H70000000000000F0L can't be represented exactly by a Double. As I noted in the PR description, CDbl rounds it down to &H7000000000000000L, which is why it failed to catch this bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. Sounds reasonable.

New TypeAndValue(doubleType, CDbl(&H8000000000000000L)),
New TypeAndValue(doubleType, CDbl(&H7FFFFFFFFFFFFC00L)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these cases be added to ConstantExpressionConversions2 as well, at line 1169?

New TypeAndValue(typeCodeType, Int32.MinValue),
New TypeAndValue(typeCodeType, Int32.MaxValue),
New TypeAndValue(typeCodeType, CInt(-3)),
Expand Down Expand Up @@ -1165,7 +1166,8 @@ End Class
New TypeAndValue(uint64Type, CULng(18)),
New TypeAndValue(decimalType, CDec(-11.3)),
New TypeAndValue(doubleType, CDbl(&HF000000000000000UL)),
New TypeAndValue(doubleType, CDbl(&H70000000000000F0L)),
Copy link
Contributor

@AlekseyTs AlekseyTs May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New TypeAndValue(doubleType, CDbl(&H70000000000000F0L)),

Please keep this value #Closed

New TypeAndValue(doubleType, CDbl(&H8000000000000000L)),
New TypeAndValue(doubleType, CDbl(&H7FFFFFFFFFFFFC00L)),
New TypeAndValue(typeCodeType, Int32.MinValue),
New TypeAndValue(typeCodeType, Int32.MaxValue),
New TypeAndValue(typeCodeType, CInt(-3)),
Expand Down Expand Up @@ -5095,5 +5097,46 @@ End Module
]]>)
End Sub

<WorkItem(73032, "https://github.com/dotnet/roslyn/issues/73032")>
<Fact()>
Public Sub ConvertLargeDoubleConstantsAndLiteralsToLong()
Dim compilation = CreateCompilationWithMscorlib40AndVBRuntime(
<compilation>
<file name="a.vb"><![CDATA[
Module Program
Sub Main()
System.Console.WriteLine(CType(CDbl(&H8000000000000000L), Long) = ConvertToLong(CDbl(&H8000000000000000L)))
System.Console.WriteLine(CType(CDbl(&H8000000000000400L), Long) = ConvertToLong(CDbl(&H8000000000000400L)))
System.Console.WriteLine(CType(-9.0E+18, Long) = ConvertToLong(-9.0E+18))
System.Console.WriteLine(CType(CDbl(&H8FFFFFFFFFFFFC00L), Long) = ConvertToLong(CDbl(&H8FFFFFFFFFFFFC00L)))
System.Console.WriteLine(CType(CDbl(&H9000000000000000L), Long) = ConvertToLong(CDbl(&H9000000000000000L)))
System.Console.WriteLine(CType(CDbl(&H7000000000000000L), Long) = ConvertToLong(CDbl(&H7000000000000000L)))
System.Console.WriteLine(CType(CDbl(&H7000000000000400L), Long) = ConvertToLong(CDbl(&H7000000000000400L)))
System.Console.WriteLine(CType(9.0E+18, Long) = ConvertToLong(9.0E+18))
System.Console.WriteLine(CType(CDbl(&H7FFFFFFFFFFFFC00L), Long) = ConvertToLong(CDbl(&H7FFFFFFFFFFFFC00L)))
End Sub

Function ConvertToLong(x as Double) As Long
Return CType(x, Long)
End Function
End Module
]]></file>
</compilation>, options:=TestOptions.ReleaseExe.WithOverflowChecks(True))

Dim expectedOutput = <![CDATA[
True
True
True
True
True
True
True
True
True
]]>

CompileAndVerify(compilation, expectedOutput:=expectedOutput).VerifyDiagnostics()
End Sub

End Class
End Namespace