]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: The actual struct size is required for calloc (POSIX)
authorRico Tzschichholz <ricotz@ubuntu.com>
Mon, 7 Sep 2020 12:07:55 +0000 (14:07 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 7 Sep 2020 12:24:57 +0000 (14:24 +0200)
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

codegen/valaccodestructmodule.vala
tests/basic-types/arrays.vala

index 66593a21233ee2541bdde130a8ecbb51ebe3e2d7..2098e88e53b831982254e4d5d96573bf4df334aa 100644 (file)
@@ -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);
                }
 
index d2b8c55f8ae702f7e8d5b8ab3851d448ca445112..8f5e1c78f8c8d5216886a81cd5c85466dd1026a1 100644 (file)
@@ -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]) {