GNOME Bugzilla – Bug 648082
Vala should warn when locally allocated data is returned unowned
Last modified: 2018-05-22 14:01:30 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 :(
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.
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).
-- 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.