Array resize can fail to adjust the recorded length
The following program segfaults (valac 0.48.11):
delegate void Recurser (int n);
void main () {
var a = new int[0];
Recurser f = null;
f = (n) => {
a += 1;
a.resize (0);
if (n > 0)
f (n - 1);
};
f (2);
}
The problem is that when the array a
is resized, its size
member is not updated. Valac seems to think it won't be needed again. If I change the recursion to a loop:
void main () {
var a = new int[0];
for (uint i = 0; i < 2; i++) {
a += 1;
a.resize (0);
}
}
then correct code is generated.
If I remove the loop:
void main () {
var a = new int[0];
a += 1;
a.resize (0);
}
then again the update to size
is removed, but this is fine because indeed the array is not used again.