GNOME Bugzilla – Bug 642350
Crash if items in string[] wrapped by N_()
Last modified: 2017-03-02 10:29:55 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; }
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".
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.
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.
Attachment 341080 [details] pushed as a75f246 - methodcall: Don't try to remove N_/NC_ while they are properly handled in C
*** Bug 730120 has been marked as a duplicate of this bug. ***