Slicing fixed size (stack allocated) arrays ignores the array length.
That is, in
int abc[5] = {1, 2, 3, 4, 5};
var slice = abc[2:5];
The array slice
will have slice.length = 5
and the memcpy
will read beyond abc
's bounds.
A fix that works for me is:
diff --git a/tests/basic-types/arrays-fixed-assignment.vala b/tests/basic-types/arrays-fixed-assignment.vala
index 0ec0df437..4234e9884 100644
--- a/tests/basic-types/arrays-fixed-assignment.vala
+++ b/tests/basic-types/arrays-fixed-assignment.vala
@@ -24,4 +24,11 @@ void main () {
string bar[3] = { "foo", "bar", "baz" };
assert (bar[1] == "bar");
}
+ {
+ string bar[4] = { "foo", "bar", "baz", "buzz" };
+ var foo = bar[1:3];
+ assert (foo.length == 2);
+ assert (foo[0] == "bar");
+ assert (foo[1] == "baz");
+ }
}
diff --git a/vala/valasliceexpression.vala b/vala/valasliceexpression.vala
index 8eca80998..6c23e8e6c 100644
--- a/vala/valasliceexpression.vala
+++ b/vala/valasliceexpression.vala
@@ -149,6 +149,11 @@ public class Vala.SliceExpression : Expression {
value_type.value_owned = false;
value_type.check (context);
+ unowned ArrayType array_type = (ArrayType) value_type;
+ if (array_type.fixed_length) {
+ array_type.length = new BinaryExpression(BinaryOperator.MINUS, stop, start);
+ }
+
/* check if the index is of type integer */
if (!(start.value_type is IntegerType || start.value_type is EnumValueType)) {
error = true;