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 543870 - Strange behaviour with ?: operator
Strange behaviour with ?: operator
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Code Generator
0.3.x
Other All
: Normal normal
: ---
Assigned To: Jürg Billeter
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2008-07-20 15:46 UTC by Frederik Zipp
Modified: 2008-11-29 12:21 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
doublefreetest: string addition (664 bytes, text/plain)
2008-07-26 19:27 UTC, Andrew Flegg
Details

Description Frederik Zipp 2008-07-20 15:46:45 UTC
// doesn't work

using Gtk;

void main () {
	TreePath a = new TreePath.from_string ("42");
	message (a.to_string ());
	TreePath b = (a != null) ? a.copy () : null;
	message (b.to_string ());
}

// works

using Gtk;

void main () {
	TreePath a = new TreePath.from_string ("42");
	message (a.to_string ());
	TreePath b;
	if (a != null) b = a.copy (); else b = null;
	message (b.to_string ());
}

------------------------------------------------------
The first one generates a runtime error:

GLib-ERROR **: /build/buildd/glib2.0-2.16.4/glib/gmem.c:156: failed to allocate 1614494016 bytes
aborting...
Aborted
Comment 1 Andrew Flegg 2008-07-26 19:27:24 UTC
Created attachment 115335 [details]
doublefreetest: string addition

This program, when compiled and run against vala 0.3.4 demonstrates a possibly related problem with the ternary operator:

--------8<--------
*** glibc detected *** ./doublefreetest: double free or corruption
(fasttop): 0x0000000000606900 ***
======= Backtrace: =========
/lib/libc.so.6[0x7f930d76108a]
/lib/libc.so.6(cfree+0x8c)[0x7f930d764c1c]
./doublefreetest[0x400c75]
./doublefreetest[0x400ab2]
./doublefreetest[0x400afc]
/lib/libc.so.6(__libc_start_main+0xf4)[0x7f930d70b1c4]
./doublefreetest[0x400a09]
-------->8--------


This minimal program also results in the same error:

--------8<--------
string get_string () {
       return "foo";
}

void main () {
       string s = true ? get_string () : "bar";
}
-------->8--------
Comment 2 Emil L 2008-10-06 19:45:33 UTC
The bug still exists in 0.3.5.
This doesn't work:

string get_string () {
       return "foo";
}

void main () {
       string s = true ? get_string () : "bar";
}

However, this works:
string get_string () {
       return "foo";
}

void main () {
       string s = true ? "bar" : get_string ();
}

The problem is in the C-code:
This doesn't work:
static void _main (void) {
	char* _tmp0;
	char* _tmp1;
	char* s;
	_tmp0 = NULL;
	_tmp1 = NULL;
	s = (_tmp1 = (TRUE ? (_tmp0 = get_string ()) : "bar"), (_tmp0 = (g_free (_tmp0), NULL)), _tmp1);
	s = (g_free (s), NULL);
}
But if I use the same syntax from the C-code that worked, I figured out that this should be the right code:
static void _main (void) {
	char* _tmp0;
	char* _tmp1;
	char* _tmp2;
	char* s;
	_tmp0 = NULL;
	_tmp1 = NULL;
	_tmp2 = NULL;
	s = (_tmp2 = (_tmp1 = (TRUE ? (_tmp0 = get_string()) : "bar"), (_tmp1 == NULL ? NULL : g_strdup (_tmp1))), (_tmp0 = (g_free (_tmp0), NULL)), _tmp2);
	s = (g_free (s), NULL);
}


This must be very easy to fix and this bug is really annoying. It was more than two months since this bug was reported :)

Another annoying thing is that if I try to compile this code:
true ? "foo" + "bar" : "foo" + "bar"
the valac is segfaulting. You have to type:
true ? ("foo" + "bar") : ("foo" + "bar")
Comment 3 Jürg Billeter 2008-11-29 12:21:00 UTC
2008-11-29  Jürg Billeter  <j@bitron.ch>

	* vala/Makefile.am:
	* vala/valablock.vala:
	* vala/valacodenode.vala:
	* vala/valaconditionalexpression.vala:
	* vala/valadeclarationstatement.vala:
	* vala/valaexpression.vala:
	* vala/valanullchecker.vala:
	* vala/valastatementlist.vala:
	* gobject/valaccodebasemodule.vala:
	* gobject/valaccodegenerator.vala:
	* gobject/valaccodemodule.vala:

	Convert ternary conditionals into if statements,
	fixes bug 543870 and bug 554594

Fixed in r2083.