From: Rico Tzschichholz Date: Mon, 7 Sep 2020 12:07:55 +0000 (+0200) Subject: codegen: The actual struct size is required for calloc (POSIX) X-Git-Tag: 0.50.0~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bdc95e7e5308abc25b31cd73e3320394ab158625;p=thirdparty%2Fvala.git codegen: The actual struct size is required for calloc (POSIX) Found by -Werror=array-bounds on ppc64el with gcc 10.2 and musl 1.2 basic_types_arrays.c:1268:2: error: 'memcpy' forming offset [8, 23] is out of the bounds [0, 8] [-Werror=array-bounds] 1268 | memcpy (dup, self, sizeof (Foo)); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Fixes https://gitlab.gnome.org/GNOME/vala/issues/1068 --- diff --git a/codegen/valaccodestructmodule.vala b/codegen/valaccodestructmodule.vala index 66593a212..2098e88e5 100644 --- a/codegen/valaccodestructmodule.vala +++ b/codegen/valaccodestructmodule.vala @@ -197,9 +197,13 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule { } else if (context.profile == Profile.POSIX) { // calloc needs stdlib.h cfile.add_include ("stdlib.h"); + + var sizeof_call = new CCodeFunctionCall (new CCodeIdentifier ("sizeof")); + sizeof_call.add_argument (new CCodeConstant (get_ccode_name (st))); + var creation_call = new CCodeFunctionCall (new CCodeIdentifier ("calloc")); creation_call.add_argument (new CCodeConstant ("1")); - creation_call.add_argument (new CCodeIdentifier ("sizeof (%s*)".printf (get_ccode_name (st)))); + creation_call.add_argument (sizeof_call); ccode.add_assignment (new CCodeIdentifier ("dup"), creation_call); } diff --git a/tests/basic-types/arrays.vala b/tests/basic-types/arrays.vala index d2b8c55f8..8f5e1c78f 100644 --- a/tests/basic-types/arrays.vala +++ b/tests/basic-types/arrays.vala @@ -240,6 +240,11 @@ struct Bar { public int bar; } +struct Manam { + Bar array[1024]; + Bar manam; +} + void test_struct_array () { assert (FOO_ARRAY_CONST[0].bar == 42); @@ -247,6 +252,9 @@ void test_struct_array () { var bar = new Bar[23]; bar[7] = b; assert (b in bar); + + Manam? manam = {}; + Manam? manam_copy = manam; } void give_fixed_array (out int i[3]) {