[Mono-bugs] [Bug 369049] New: vbnc: miscompile passing valuetype to ByRef Object parameter

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Mar 11 06:11:51 EDT 2008


           Summary: vbnc: miscompile passing valuetype to ByRef Object
           Product: Mono: Compilers
           Version: 1.2.6
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Minor
          Priority: P5 - None
         Component: Basic
        AssignedTo: rkvinge at novell.com
        ReportedBy: andyhume32 at yahoo.co.uk
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---

Passing a value type to a ByRef Object parameter is accepted but miscompiled

Code of the form:
Option Strict On ' Or Off, see text 
Option Explicit On

Public Structure SomeStruct
  Public x As Integer
    Public y As Integer
End Structure

Public Class ByRefValueType
  Shared Sub Main()
      Dim ss As SomeStruct
      ss.x = 111
  End Sub

    Shared Sub MethodSomeStructByRef(ByRef obj As Object)
      Console.WriteLine("'{0}'", obj)
      Dim ss As SomeStruct = DirectCast(obj, SomeStruct)
      ss.x = 99999
      obj = ss
  End Sub

    Shared Function FooIsNull(ByVal o As Object) As Boolean
        Return o Is Nothing
    End Function
End Class

This currently fails with the following when run:

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object
  at (wrapper stelemref) System.Object:stelemref (object,intptr,object)
  at System.String.Format (System.String format, System.Object arg0) [0x00000]
  at System.IO.TextWriter.Write (System.String format, System.Object arg0)
[0x00000] in
  at System.IO.TextWriter.WriteLine (System.String format, System.Object arg0)
[0x00000] in
  at System.IO.SynchronizedWriter.WriteLine (System.String format,
System.Object value) [0x00008] in
  at System.Console.WriteLine (System.String format, System.Object arg0)
[0x00000] in
  at VtToByrefObject.MethodSomeStructByRef (System.Object& obj) [0x0000c] in
D:\Documents and Settings\alan\My
  at VtToByrefObject.Main () [0x00009] in D:\Documents and Settings\alan\My

With Strict=On, MSFT disallows the code with:
C:\VtToByrefObject.vb(12) : error BC32029: Option Strict On disallows narrowing
from type 'Object' to type 'SomeStruct' in copying the value of 'ByRef'
parameter 'obj' back to the  matching argument.


As implied by the expected error message above, when using the code with
Strict=Off, then the value type has to be boxed into the call, and on return
has to be unboxed and copied back onto the variable.  The correct result is
thus seen, on return ss.x contains 99999.  I guess that's as covered by the
second paragraph in vbls80.doc section 11.8.3.

Minor as very rare case.  Perhaps we just need to block such code??

Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list