GNOME Bugzilla – Bug 643824
Slicing of stack-allocated arrays reports incorrect array length
Last modified: 2011-07-17 16:36:31 UTC
The length of sliced stack-allocated arrays is reported incorrectly. Sample code: /* array-test.vala */ int main (string[] args) { int[] a = new int[5]; print ("%d\n", a[1:3].length); int b[5]; print ("%d\n", b[1:3].length); return 0; } Expected output: $ ./array-test 2 2 Actual output: $ ./array-test 2 5 This makes it impossible to use stack-allocated arrays with functions such as GLib.OutputStream.write
generated c-code is: [...] g_print ("%d\n", 3 - 1); g_print ("%d\n", 5); [...] More problems arise when trying: (generates C code which doesn't compile) int[] aslice = a[1:3]; int bslice[2] = b[1:3]; or the worst is: int cslice[] = b[1:3]; which generates an array on the stack of missing size and then tries to free it gint cslice[]; [...] cslice = (g_free (cslice), NULL); also while assigning cslice the whole array b is dup'd _tmp3_ = (_tmp4_ = b + 1, (_tmp4_ == NULL) ? ((gpointer) _tmp4_) : _vala _array_dup2 (_tmp4_, 5)); cslice = _tmp3_; int *aslice = a[1:3]; is the only one that works and generates gint *aslice = a + 1; obviously ignoring the length
In general the compiler seems to generates broken code for any initialization of non-const stack-allocated arrays, i.e. const int[] a = {1, 2, 3}; /* compiles*/ int[] a = {1, 2, 3}; /* compiles */ const a[] = {1, 2, 3}; /* compiles */ int a[] = {1, 2, 3}; /* does not compile */ I raised this issue on the mailing list recently: http://mail.gnome.org/archives/vala-list/2011-March/msg00010.html I think it is probably a separate bug to this one though so I created a separate bug report: https://bugzilla.gnome.org/show_bug.cgi?id=644046
commit e83585bd7cafcf09e0cb94082642f4711a99fc23 Author: Luca Bruno <lucabru@src.gnome.org> Date: Sun Jun 12 09:42:52 2011 +0200 codegen: Fix access to arrays with fixed length Fixes bug 652380. Cannot reproduce this bug, it should be fixed with this commit some time ago. Closing the bug, please reopen if needed.