bugDotGNU Portable.NET - Bugs: bug #11998, Problems with casting arrays,...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #11998: Problems with casting arrays, isinst, and the as operator

Submitter:  Marcus Urban <mathpup>
Submitted:  Wed 16 Feb 2005 07:23:05 AM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  Fixed
Privacy:  Public Assigned to:  None
Open/Closed:  Closed

Sat 19 Feb 2005 09:47:57 AM UTC, comment #3: 

Fixed in cvs 02/19/2005




fixed in cvs

Klaus Treichel <ktreichel>
Group administrator
Thu 17 Feb 2005 08:13:27 AM UTC, comment #2: 

The workaround is to cast the object directly without as.
the test file shows that this works, and the data is preserved.
using System;

public class Testing
{
public static void Main()
{

String foo = "fooo";
Console.WriteLine(foo == null );

object z= foo as object;
Console.WriteLine(z == null );

Array x = new string[1];
x[0]=foo;
Console.WriteLine(x == null );

Console.WriteLine("raw");
Console.WriteLine(x);

Console.WriteLine("as object");
Console.WriteLine(x as object[]);

Console.WriteLine("cast ");
Console.WriteLine((object[])x);

Console.WriteLine("cast2 ");
object[] y = (object[])x;
Console.WriteLine(y == null );
Console.WriteLine(y);
Console.WriteLine(y[0]);

Console.WriteLine("cast as ");
object[] z2 = x as object[];
Console.WriteLine(z2 == null );

}
}


James Michael DuPont <mdupont>
Thu 17 Feb 2005 07:55:39 AM UTC, comment #1: 

OOPS. The test prints "True" on Pnet and "False" under MS.

Marcus Urban <mathpup>
Wed 16 Feb 2005 07:23:05 AM UTC, original submission:  

There is something wrong with the with the C# "as" and "is" operators, which seem to be compiled into CIL "isinst" instructions.

The following sequence prints "False" on Pnet but true on MS:

Array x = new string[1];
object[] y = x as object[];
Console.WriteLine(y == null );

I was trying to debug this myself, but I do not think that I have enough knowledge of the runtime engine internals. Also, I do not quite understand the correspondence between CVM instructions and similarly-named CIL instructions. I looked around to cvm_ptr.c and found this:

VMCASE(COP_ISINST):
{
/* Determine if the object on the stack top is an
   instance of a particular class */
classInfo = CVM_ARG_PTR(ILClass *);
if(stacktop[-1].ptrValue != 0 &&
   !ILClassInheritsFrom(GetObjectClass(stacktop[-1].ptrValue), classInfo))
{
stacktop[-1].ptrValue = 0;
}
MODIFY_PC_AND_STACK(CVM_LEN_PTR, 0);
}

However, according to the CIL spec, ISINST is supposed to attempt casting from source-type to result-type, and replace the top element of the stack with the result (if casting is successful) or null (if casting fails). I do not quite understand what it going on here because it seems as though the COP_ISINST case should look more like COP_CASTCLASS case.

I'm very confused about all this, but I think that someone more familiar with the runtime could fix it easily.

Marcus Urban <mathpup>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files
file #2577:  array-cast.cs added by mathpup (165B - text/x-csharp - Test case)

 

Depends on the following items: None found

Items that depend on this one: None found

 

CC list is empty

 

There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

 

Follow 3 latest changes.

Date Changed by Updated Field Previous Value => Replaced by
2005-02-19 ktreichel Open/ClosedOpen Closed
2005-02-19 ktreichel StatusNone Fixed
2005-02-16 mathpup Attached File- Added array-cast.cs, #2202

Back to the top

Powered by Savane 3.13-02a9.
Corresponding source code