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 642350 - Crash if items in string[] wrapped by N_()
Crash if items in string[] wrapped by N_()
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Code Generator
0.11.x
Other Linux
: Normal normal
: ---
Assigned To: Vala maintainers
Vala maintainers
: 730120 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2011-02-15 06:33 UTC by Derek Dai
Modified: 2017-03-02 10:29 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
methodcall: Don't try to remove N_/NC_ while they are properly handled in C (4.15 KB, patch)
2016-11-30 18:31 UTC, Rico Tzschichholz
committed Details | Review

Description Derek Dai 2011-02-15 06:33:15 UTC
// Vala code
int main()
{
	const string[] x = {
			N_("Bla")
	};

	stdout.printf("%s", x[0]);

	return 0;
}

Will generate code below

// C code generated by valac
gint _vala_main (void) {
	gint result = 0;
	gchar** _tmp0_ = NULL;
	gchar** x;
	gint x_length1;
	gint _x_size_;
	_tmp0_ = g_new0 (gchar*, 1 + 1);
	_tmp0_[0] = "Bla";
	x = _tmp0_;
	x_length1 = 1;
	_x_size_ = 1;
	fprintf (stdout, "%s", x[0]);
	result = 0;
	x = (_vala_array_free (x, x_length1, (GDestroyNotify) g_free), NULL);
	return result;
}

_vala_array_free() will memory blocks in x item by item, but due to '_tmp0_[0] = "Bla"', application crashes.

But if we declare x as const, everything will be fine
// Vala code
int main()
{
	const string[] x = {
			N_("Bla")
	};

	stdout.printf("%s", x[0]);

	return 0;
}
Comment 1 Nor Jaidi Tuah 2011-02-16 03:04:56 UTC
This bug is still present in the recently released 0.11.6

It has nothing to do with array, as the bug also appears in this:

// Vala code
struct Foo {
    public string foo;
    public string bar;
}

void main() {
   Foo f = Foo() {foo = "foo", bar = N_("bar")};
}


where the compiler gives:

// generated C code
void _vala_main (void) {
	gchar* _tmp0_;
	Foo _tmp1_ = {0};
	Foo _tmp2_ = {0};
	Foo f;
	_tmp0_ = g_strdup ("foo");
	memset (&_tmp1_, 0, sizeof (Foo));
	_tmp1_.foo = _tmp0_;
	_tmp1_.bar = "bar";
	_tmp2_ = _tmp1_;
	f = _tmp2_;
	foo_destroy (&f);
}

This simple program crashes trying to destroy the static "bar".
Comment 2 Nor Jaidi Tuah 2011-02-16 03:30:32 UTC
The simple workaround of putting const that works with this:

    const string[] x = {
            N_("Bla")
    };

doesn't work with this:

   // compile error
   const Foo f = Foo() {foo = "foo", bar = N_("bar")};

So, this bug is rather serious.
Comment 3 Rico Tzschichholz 2016-11-30 18:31:46 UTC
Created attachment 341080 [details] [review]
methodcall: Don't try to remove N_/NC_ while they are properly handled in C

This avoids messing around with the ownership and properly invokes copying
if needed.
Comment 4 Rico Tzschichholz 2016-12-01 09:59:27 UTC
Attachment 341080 [details] pushed as a75f246 - methodcall: Don't try to remove N_/NC_ while they are properly handled in C
Comment 5 Rico Tzschichholz 2017-03-02 10:29:55 UTC
*** Bug 730120 has been marked as a duplicate of this bug. ***