Bug in CLR: vector casting error RRS feed

  • General discussion

  • I've found what seems to be a bug in the CLR. If you write and compile the following IL code:


    int32[...] ArrayCast (class System.Array arrObj)


     castclass int32[...]

     ret }


    and you then pass this function a non-vector, 1-dimensional array (type int32[...]) it will cast it and return without a problem. But If you pass it any other array, it comes up with InvalidCastException, as expected, EXCEPT if you pass it a vector array (type int32[]) when it 'casts' it and returns. You end up with an int32[] array in an int32[...] variable, even though this should be unsafe and unveifiable (and wrong!).

    Interestingly enough, the reverse is not true. If you change the two 'int32[...]'s in the above code to 'int32[]', you will find that the function will throw given any array except an int32[].

    (Also, if you use 'isinst' instead of 'castclass', you will get the same results except that where the former will throw an exception, the latter will return null. It will still have the above mentioned bug.)

    Monday, April 28, 2008 5:38 PM

All replies

  • IL verification isn't fool proof, I've seen other examples.  You can report this at the Connect site.
    Tuesday, April 29, 2008 12:57 AM
  • Verification isn't the point. The fact is that the code runs incorrectly. It returns an invalid value when should really throw an exception.
    Tuesday, April 29, 2008 11:27 AM