After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 643824 - Slicing of stack-allocated arrays reports incorrect array length
Slicing of stack-allocated arrays reports incorrect array length
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Arrays
0.11.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2011-03-03 22:46 UTC by gawhelan
Modified: 2011-07-17 16:36 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description gawhelan 2011-03-03 22:46:40 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
Comment 1 Andreas Brauchli 2011-03-06 12:12:14 UTC
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
Comment 2 gawhelan 2011-03-06 22:20:22 UTC
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
Comment 3 Luca Bruno 2011-07-17 16:36:31 UTC
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.