From: Rico Tzschichholz Date: Mon, 24 Jan 2022 09:17:46 +0000 (+0100) Subject: codegen: Access of stack allocated struct is guaranteed to be non null X-Git-Tag: 0.55.2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca3da69b2bda18b8b081c2dae48f2ccff93f165a;p=thirdparty%2Fvala.git codegen: Access of stack allocated struct is guaranteed to be non null Found by -Werror=address with GCC 12 See https://gitlab.gnome.org/GNOME/vala/issues/1282 --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 9ebcfbac6..2e34003f1 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -5593,6 +5593,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { innercexpr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, innercexpr); } set_cvalue (expr, new CCodeCastExpression (innercexpr, get_ccode_name (expr.type_reference))); + //TODO Use get_non_null (expr.inner.target_value) + ((GLibValue) expr.target_value).non_null = expr.is_non_null (); if (expr.type_reference is DelegateType) { var target = get_delegate_target (expr.inner); diff --git a/tests/structs/bug777194.c-expected b/tests/structs/bug777194.c-expected index 927043755..055c37c70 100644 --- a/tests/structs/bug777194.c-expected +++ b/tests/structs/bug777194.c-expected @@ -238,7 +238,7 @@ _vala_main (void) _tmp4_ = tu; _tmp4__length1 = tu_length1; bar (_tmp4_, (gint) _tmp4__length1); - _tmp5_ = (((guint8*) (&fstack)) != NULL) ? _vala_array_dup1 ((guint8*) (&fstack), sizeof (Foo) / sizeof (guint8)) : ((guint8*) (&fstack)); + _tmp5_ = _vala_array_dup1 ((guint8*) (&fstack), sizeof (Foo) / sizeof (guint8)); _tmp5__length1 = sizeof (Foo) / sizeof (guint8); to = (g_free (to), NULL); to = _tmp5_; @@ -263,7 +263,7 @@ _vala_main (void) _tmp11__length1 = tu_length1; bar (_tmp11_, (gint) _tmp11__length1); _tmp12_ = fstack; - _tmp13_ = (((guint8*) (&_tmp12_)) != NULL) ? _vala_array_dup2 ((guint8*) (&_tmp12_), sizeof (Foo) / sizeof (guint8)) : ((guint8*) (&_tmp12_)); + _tmp13_ = _vala_array_dup2 ((guint8*) (&_tmp12_), sizeof (Foo) / sizeof (guint8)); _tmp13__length1 = sizeof (Foo) / sizeof (guint8); to = (g_free (to), NULL); to = _tmp13_; @@ -310,7 +310,7 @@ _vala_main (void) _tmp22_ = tu; _tmp22__length1 = tu_length1; _vala_assert (_tmp22__length1 == 4, "tu.length == 4"); - _tmp23_ = (((guint8*) (&i32)) != NULL) ? _vala_array_dup4 ((guint8*) (&i32), sizeof (gint32) / sizeof (guint8)) : ((guint8*) (&i32)); + _tmp23_ = _vala_array_dup4 ((guint8*) (&i32), sizeof (gint32) / sizeof (guint8)); _tmp23__length1 = sizeof (gint32) / sizeof (guint8); to = (g_free (to), NULL); to = _tmp23_; @@ -332,7 +332,7 @@ _vala_main (void) _tmp26_ = tu; _tmp26__length1 = tu_length1; _vala_assert (_tmp26__length1 == 8, "tu.length == 8"); - _tmp27_ = (((guint8*) (&u64)) != NULL) ? _vala_array_dup5 ((guint8*) (&u64), sizeof (guint64) / sizeof (guint8)) : ((guint8*) (&u64)); + _tmp27_ = _vala_array_dup5 ((guint8*) (&u64), sizeof (guint64) / sizeof (guint8)); _tmp27__length1 = sizeof (guint64) / sizeof (guint8); to = (g_free (to), NULL); to = _tmp27_; diff --git a/vala/valaaddressofexpression.vala b/vala/valaaddressofexpression.vala index ff542835b..dbe9cff1a 100644 --- a/vala/valaaddressofexpression.vala +++ b/vala/valaaddressofexpression.vala @@ -82,6 +82,10 @@ public class Vala.AddressofExpression : Expression { return inner.is_accessible (sym); } + public override bool is_non_null () { + return inner.is_non_null (); + } + public override bool check (CodeContext context) { if (checked) { return !error; diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index d27a4266a..f083da911 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -203,7 +203,12 @@ public class Vala.MemberAccess : Expression { if (c != null) { return (c is EnumValue || !c.type_reference.nullable); } else if (l != null) { - return (l.variable_type is ArrayType && ((ArrayType) l.variable_type).inline_allocated); + unowned DataType type = l.variable_type; + if (type is ArrayType) { + return ((ArrayType) type).inline_allocated; + } else { + return type.is_real_non_null_struct_type () || type.is_non_null_simple_type (); + } } else if (m != null) { return (m.binding == MemberBinding.STATIC || prototype_access); } else {