Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • vala vala
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 770
    • Issues 770
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 73
    • Merge requests 73
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Releases
  • Packages and registries
    • Packages and registries
    • Container Registry
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • External wiki
    • External wiki
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • GNOMEGNOME
  • valavala
  • Issues
  • #928
Closed
Open
Issue created Mar 11, 2020 by Emill@Emill

resize on unowned arrays should not be allowed

Vala code:

void func(int[] arr) {
    arr.resize(2000);
    stdout.printf("Length after resize: %d\n", arr.length);
}

void main() {
    int[] arr = new int[1];
    func(arr);
    stdout.printf("Length in main: %d\n", arr.length);
}

Generated C code:

void
func (gint* arr,
      gint arr_length1)
{
	gint _tmp0_;
	FILE* _tmp1_;
	_tmp0_ = 2000;
	arr = g_renew (gint, arr, 2000);
	(_tmp0_ > arr_length1) ? memset (arr + arr_length1, 0, sizeof (gint) * (_tmp0_ - arr_length1)) : NULL;
	arr_length1 = _tmp0_;
	_tmp1_ = stdout;
	fprintf (_tmp1_, "Length after resize: %d\n", arr_length1);
}

void
_vala_main (void)
{
	gint* arr = NULL;
	gint* _tmp0_;
	gint arr_length1;
	gint _arr_size_;
	FILE* _tmp1_;
	_tmp0_ = g_new0 (gint, 1);
	arr = _tmp0_;
	arr_length1 = 1;
	_arr_size_ = arr_length1;
	func (arr, (gint) arr_length1);
	_tmp1_ = stdout;
	fprintf (_tmp1_, "Length in main: %d\n", arr_length1);
	arr = (g_free (arr), NULL);
}

Output:

Length after resize: 2000
Length in main: 1

Valgrind also reports a memory leak.

Since resize ultimately calls realloc, the allocation is moved and since an unowned reference doesn't include the pointer to the owned reference variable, it's impossible to support reallocations for this case.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking