GNOME Bugzilla – Bug 587683
Casting Arrays generates bogus code
Last modified: 2009-08-16 21:21:34 UTC
Casting Arrays can result in code that crashes the application. The following minimal vala example shows the problem (vala-git as of today): int main (string[] args) { char[] signed_array; uchar[] unsigned_array; signed_array = new char[20]; unsigned_array = (uchar[]) signed_array; return 0; } The generated code contains the following code: _vala_array_dup1 (_tmp1_, -1) but (probably) vala_array_dup1 takes an unsigned long integer as parameter for the array size, resulting in the attempt to malloc 2^32-1 bytes, which obviously fails: GLib-ERROR **: /build/buildd-glib2.0_2.20.1-2-i386-hGzT8z/glib2.0-2.20.1/glib/gmem.c:136: failed to allocate 4294967295 bytes aborting... Aborted Compilation of the vala-code above does not yield any warnings.
A similiar problem also happens with weak references. While this example does not crash it gives unexpected results for the array length: int main (string[] args) { char[] signed_array; weak uchar[] unsigned_array; signed_array = new char[20]; unsigned_array = (uchar[]) signed_array; stdout.printf ("array sizes: signed: %d, unsigned: %d\n", signed_array.length, unsigned_array.length); return 0; }
commit edcd069d6b0b7b9e1ff2797d45ccbafe910a1357 Author: Jürg Billeter <j@bitron.ch> Date: Wed Jul 29 23:29:09 2009 +0200 Retain array length across casts