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 1 commit
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,42 @@ 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))
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.

System.Console.WriteLine(CType(CDbl(&H8000000000000000L), Long))

Instead of outputting the values, I think we should compare them with result of conversions performed at runtime. For example it is not obvious to me that the values in the output are correct. I think the overflow check for VB is set to true by default, but it would be good to set this option explicitly (WithOverflowChecks). #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.

In each case, the Long argument to CDbl is the expected result of calling CType. So I could write

System.Console.WriteLine(CType(CDbl(&H8000000000000000L), Long) = &H8000000000000000L)

and verify that the output is True. Is that acceptable?

Copy link
Contributor

@AlekseyTs AlekseyTs May 8, 2024

Choose a reason for hiding this comment

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

This is going to rely on compile time constant folding for equality, which would be desirable to avoid. The goal is to compare result of compile time constant folding with result that doesn't rely on constant folding. I have something like the following in mind:

 System.Console.WriteLine(CType(CDbl(&H8000000000000000L), Long) = ConvertToLong(CDbl(&H8000000000000000L)))


Function ConvertToLong(x as Double) As Long
    Return CType(x, Long)
End Function

System.Console.WriteLine(CType(CDbl(&H8000000000000400L), Long))
System.Console.WriteLine(CType(-9.0E+18, Long))
System.Console.WriteLine(CType(CDbl(&H8FFFFFFFFFFFFC00L), Long))
System.Console.WriteLine(CType(CDbl(&H9000000000000000L), Long))
System.Console.WriteLine(CType(CDbl(&H7000000000000000L), Long))
System.Console.WriteLine(CType(CDbl(&H7000000000000400L), Long))
System.Console.WriteLine(CType(9.0E+18, Long))
System.Console.WriteLine(CType(CDbl(&H7FFFFFFFFFFFFC00L), Long))
End Sub
End Module
]]></file>
</compilation>, options:=TestOptions.ReleaseExe)

Dim expectedOutput = <![CDATA[
-9223372036854775808
-9223372036854774784
-9000000000000000000
-8070450532247929856
-8070450532247928832
8070450532247928832
8070450532247929856
9000000000000000000
9223372036854774784
]]>

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

End Class
End Namespace