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 648082 - Vala should warn when locally allocated data is returned unowned
Vala should warn when locally allocated data is returned unowned
Status: RESOLVED OBSOLETE
Product: vala
Classification: Core
Component: Structs
0.13.x
Other Linux
: Normal enhancement
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2011-04-18 09:46 UTC by Sam Thursfield
Modified: 2018-05-22 14:01 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Sam Thursfield 2011-04-18 09:46:33 UTC
Hi!
Here's a little test case, first of all.

public unowned Date first () {
	/* In the real code, this value is returned by an external function
	 * so is actually unowned in this scope
	 */
	Date d = GLib.Date();
	d.set_dmy (5, DateMonth.APRIL, 2011);
	return d;
}

public unowned Date? second () {
	/* Date is stored on the stack and then a pointer to
	 * the stack returned :(
	 */
	return first();
}

int main(string args[]) {
	Date d = second();
	assert (d.valid());

	return 0;
}

Here's an extract from the C code:

GDate* test_second (void) {
	GDate* result = NULL;
	GDate _tmp0_ = {0};
	GDate _tmp1_ = {0};
	test_first (&_tmp0_);
	_tmp1_ = _tmp0_;
	result = &_tmp1_;
	return result;
}

.. so main() is returned a pointer to a now invalid stack object :(
Comment 1 Luca Bruno 2011-04-22 20:26:54 UTC
This is not a vala bug, it's a wrong usage of the unowned keyword. You firstly allocate something, then free it because it goes out of scope, then return a dangling pointer to it. Am I missing anything?
Just don't use unowned.
Comment 2 Sam Thursfield 2011-04-25 13:53:01 UTC
Sorry yes. The bug is that I think there should be a warning when I do this, because currently the compiler happily generates code containing a subtle memory corruption bug (gcc doesn't warn either).
Comment 3 GNOME Infrastructure Team 2018-05-22 14:01:30 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/195.