Passing inline-allocated array as heap-allocated ref array parameter should not be allowed
Vala code:
void func(ref int[] arr) {
stdout.printf("%d %d\n", arr[0], arr.length);
}
void main() {
int arr[1] = {1};
func(ref arr);
}
Generated C-code:
void
func (gint** arr,
gint* arr_length1)
{
FILE* _tmp0_;
gint _tmp1_;
_tmp0_ = stdout;
_tmp1_ = (*arr)[0];
fprintf (_tmp0_, "%d %d\n", _tmp1_, *arr_length1);
}
void
_vala_main (void)
{
gint arr[1] = {0};
gint _tmp0_[1] = {0};
_tmp0_[0] = 1;
memcpy (arr, _tmp0_, 1 * sizeof (gint));
func (&arr, (gint*) 1);
}
gcc warnings:
test.c: In function ‘_vala_main’:
test.c:30:8: warning: passing argument 1 of ‘func’ from incompatible pointer type [-Wincompatible-pointer-types]
30 | func (&arr, (gint*) 1);
| ^~~~
| |
| gint (*)[1] {aka int (*)[1]}
test.c:13:14: note: expected ‘gint **’ {aka ‘int **’} but argument is of type ‘gint (*)[1]’ {aka ‘int (*)[1]’}
13 | func (gint** arr,
| ~~~~~~~^~~
Running the program segfaults.
As the callee might reallocate/reassign the array, it should not be allowed to pass inline-allocated array to this kind of parameter.