[vala/0.48] codegen: The actual struct size is required for calloc (POSIX)



commit 6d33bee1a15cdb60605bd02e2cf11b2fcbf545a3
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Mon Sep 7 14:07:55 2020 +0200

    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

 codegen/valaccodestructmodule.vala | 6 +++++-
 tests/basic-types/arrays.vala      | 8 ++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
---
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 5d2bea15f..6376949df 100644
--- a/tests/basic-types/arrays.vala
+++ b/tests/basic-types/arrays.vala
@@ -224,6 +224,11 @@ struct Bar {
        public int bar;
 }
 
+struct Manam {
+       Bar array[1024];
+       Bar manam;
+}
+
 void test_struct_array () {
        assert (FOO_ARRAY_CONST[0].bar == 42);
 
@@ -231,6 +236,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]) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]