From: Rico Tzschichholz Date: Sun, 23 Jan 2022 17:46:48 +0000 (+0100) Subject: codegen: Access of inline allocated array is guaranteed to be non null X-Git-Tag: 0.54.7~24 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=66eca5a3dea1348e40feb1b636452e5b2db15ce7;p=thirdparty%2Fvala.git codegen: Access of inline allocated array is guaranteed to be non null and improve null check for container in slice expression Found by -Werror=address with GCC 12 See https://gitlab.gnome.org/GNOME/vala/issues/1282 --- diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala index 632b26046..2b3021a5f 100644 --- a/codegen/valaccodearraymodule.vala +++ b/codegen/valaccodearraymodule.vala @@ -210,6 +210,7 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule { var splicelen = new CCodeBinaryExpression (CCodeBinaryOperator.MINUS, cstop, cstart); set_cvalue (expr, cstartpointer); + ((GLibValue) expr.target_value).non_null = get_non_null (expr.container.target_value); // Make sure no previous length values are preserved set_array_length (expr, splicelen); } diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 0fb50df34..7dc89eaae 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -4831,6 +4831,13 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { return store_temp_value (new GLibValue (type, ccall), node); } else { + CCodeExpression ccallarg; + if (node is SliceExpression) { + ccallarg = cexpr; + cexpr = get_cvalue (((SliceExpression) node).container); + } else { + ccallarg = cexpr; + } var cnotnull = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, cexpr, new CCodeConstant ("NULL")); if (type is GenericType) { // dup functions are optional for type parameters @@ -4840,9 +4847,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { if (type is GenericType) { // cast from gconstpointer to gpointer as GBoxedCopyFunc expects gpointer - ccall.add_argument (new CCodeCastExpression (cexpr, get_ccode_name (pointer_type))); + ccall.add_argument (new CCodeCastExpression (ccallarg, get_ccode_name (pointer_type))); } else { - ccall.add_argument (cexpr); + ccall.add_argument (ccallarg); } if (type is ArrayType) { diff --git a/tests/arrays/slice-fixed-length.c-expected b/tests/arrays/slice-fixed-length.c-expected index be50c1cc2..0a6d31126 100644 --- a/tests/arrays/slice-fixed-length.c-expected +++ b/tests/arrays/slice-fixed-length.c-expected @@ -225,7 +225,7 @@ _vala_main (void) gint _foo_size_; const gchar* _tmp6_; const gchar* _tmp7_; - _tmp5_ = ((bar + 1) != NULL) ? _vala_array_dup1 (bar + 1, 3 - 1) : (bar + 1); + _tmp5_ = _vala_array_dup1 (bar + 1, 3 - 1); _tmp5__length1 = 3 - 1; foo = _tmp5_; foo_length1 = _tmp5__length1; @@ -262,7 +262,7 @@ _vala_main (void) const gchar* _tmp11_; const gchar* _tmp12_; begin = 1; - _tmp10_ = ((bar + begin) != NULL) ? _vala_array_dup2 (bar + begin, 3 - begin) : (bar + begin); + _tmp10_ = _vala_array_dup2 (bar + begin, 3 - begin); _tmp10__length1 = 3 - begin; foo = _tmp10_; foo_length1 = _tmp10__length1; @@ -282,7 +282,7 @@ _vala_main (void) gint _foo_size_; const gchar* _tmp14_; const gchar* _tmp15_; - _tmp13_ = ((bar + 1) != NULL) ? _vala_array_dup3 (bar + 1, 3 - 1) : (bar + 1); + _tmp13_ = _vala_array_dup3 (bar + 1, 3 - 1); _tmp13__length1 = 3 - 1; foo = _tmp13_; foo_length1 = _tmp13__length1; @@ -319,7 +319,7 @@ _vala_main (void) const gchar* _tmp19_; const gchar* _tmp20_; end = 3; - _tmp18_ = ((bar + 1) != NULL) ? _vala_array_dup4 (bar + 1, end - 1) : (bar + 1); + _tmp18_ = _vala_array_dup4 (bar + 1, end - 1); _tmp18__length1 = end - 1; foo = _tmp18_; foo_length1 = _tmp18__length1; @@ -347,7 +347,7 @@ _vala_main (void) { gchar** _tmp21_; gint _tmp21__length1; - _tmp21_ = ((bar + 1) != NULL) ? _vala_array_dup5 (bar + 1, 3 - 1) : (bar + 1); + _tmp21_ = _vala_array_dup5 (bar + 1, 3 - 1); _tmp21__length1 = 3 - 1; minim (_tmp21_, (gint) _tmp21__length1); } @@ -356,7 +356,7 @@ _vala_main (void) gchar** _tmp22_; gint _tmp22__length1; begin = 1; - _tmp22_ = ((bar + begin) != NULL) ? _vala_array_dup6 (bar + begin, 3 - begin) : (bar + begin); + _tmp22_ = _vala_array_dup6 (bar + begin, 3 - begin); _tmp22__length1 = 3 - begin; minim (_tmp22_, (gint) _tmp22__length1); } @@ -365,7 +365,7 @@ _vala_main (void) gchar** _tmp23_; gint _tmp23__length1; end = 3; - _tmp23_ = ((bar + 1) != NULL) ? _vala_array_dup7 (bar + 1, end - 1) : (bar + 1); + _tmp23_ = _vala_array_dup7 (bar + 1, end - 1); _tmp23__length1 = end - 1; minim (_tmp23_, (gint) _tmp23__length1); } diff --git a/tests/basic-types/arrays.c-expected b/tests/basic-types/arrays.c-expected index ec9664488..363187e53 100644 --- a/tests/basic-types/arrays.c-expected +++ b/tests/basic-types/arrays.c-expected @@ -471,7 +471,7 @@ test_integer_array (void) _vala_assert (_tmp22_ == 23, "b[1] == 23"); _tmp23_ = a; _tmp23__length1 = a_length1; - _tmp24_ = ((_tmp23_ + 1) != NULL) ? _vala_array_dup2 (_tmp23_ + 1, 3 - 1) : (_tmp23_ + 1); + _tmp24_ = (_tmp23_ != NULL) ? _vala_array_dup2 (_tmp23_ + 1, 3 - 1) : _tmp23_; _tmp24__length1 = 3 - 1; c = _tmp24_; c_length1 = _tmp24__length1; @@ -483,7 +483,7 @@ test_integer_array (void) _vala_assert (_tmp26_ == 11, "c[1] == 11"); _tmp27_ = a; _tmp27__length1 = a_length1; - _tmp28_ = ((_tmp27_ + 0) != NULL) ? _vala_array_dup3 (_tmp27_ + 0, 0 - 0) : (_tmp27_ + 0); + _tmp28_ = (_tmp27_ != NULL) ? _vala_array_dup3 (_tmp27_ + 0, 0 - 0) : _tmp27_; _tmp28__length1 = 0 - 0; c0 = _tmp28_; c0_length1 = _tmp28__length1; @@ -494,7 +494,7 @@ test_integer_array (void) _tmp29__length1 = a_length1; _tmp30_ = a; _tmp30__length1 = a_length1; - _tmp31_ = ((_tmp30_ + 1) != NULL) ? _vala_array_dup4 (_tmp30_ + 1, _tmp30__length1 - 1) : (_tmp30_ + 1); + _tmp31_ = (_tmp30_ != NULL) ? _vala_array_dup4 (_tmp30_ + 1, _tmp30__length1 - 1) : _tmp30_; _tmp31__length1 = _tmp30__length1 - 1; c1 = _tmp31_; c1_length1 = _tmp31__length1; @@ -506,7 +506,7 @@ test_integer_array (void) _vala_assert (_tmp33_ == 11, "c1[1] == 11"); _tmp34_ = a; _tmp34__length1 = a_length1; - _tmp35_ = ((_tmp34_ + 0) != NULL) ? _vala_array_dup5 (_tmp34_ + 0, 2 - 0) : (_tmp34_ + 0); + _tmp35_ = (_tmp34_ != NULL) ? _vala_array_dup5 (_tmp34_ + 0, 2 - 0) : _tmp34_; _tmp35__length1 = 2 - 0; c2 = _tmp35_; c2_length1 = _tmp35__length1; @@ -520,7 +520,7 @@ test_integer_array (void) _tmp38__length1 = a_length1; _tmp39_ = a; _tmp39__length1 = a_length1; - _tmp40_ = ((_tmp39_ + 0) != NULL) ? _vala_array_dup6 (_tmp39_ + 0, _tmp39__length1 - 0) : (_tmp39_ + 0); + _tmp40_ = (_tmp39_ != NULL) ? _vala_array_dup6 (_tmp39_ + 0, _tmp39__length1 - 0) : _tmp39_; _tmp40__length1 = _tmp39__length1 - 0; c3 = _tmp40_; c3_length1 = _tmp40__length1; diff --git a/tests/posix/arrays.c-expected b/tests/posix/arrays.c-expected index 7b76fb9d8..4b5382a56 100644 --- a/tests/posix/arrays.c-expected +++ b/tests/posix/arrays.c-expected @@ -478,7 +478,7 @@ test_integer_array (void) assert (_tmp22_ == 23); _tmp23_ = a; _tmp23__length1 = a_length1; - _tmp24_ = ((_tmp23_ + 1) != NULL) ? _vala_array_dup2 (_tmp23_ + 1, 3 - 1) : (_tmp23_ + 1); + _tmp24_ = (_tmp23_ != NULL) ? _vala_array_dup2 (_tmp23_ + 1, 3 - 1) : _tmp23_; _tmp24__length1 = 3 - 1; c = _tmp24_; c_length1 = _tmp24__length1; @@ -490,7 +490,7 @@ test_integer_array (void) assert (_tmp26_ == 11); _tmp27_ = a; _tmp27__length1 = a_length1; - _tmp28_ = ((_tmp27_ + 0) != NULL) ? _vala_array_dup3 (_tmp27_ + 0, 0 - 0) : (_tmp27_ + 0); + _tmp28_ = (_tmp27_ != NULL) ? _vala_array_dup3 (_tmp27_ + 0, 0 - 0) : _tmp27_; _tmp28__length1 = 0 - 0; c0 = _tmp28_; c0_length1 = _tmp28__length1; @@ -501,7 +501,7 @@ test_integer_array (void) _tmp29__length1 = a_length1; _tmp30_ = a; _tmp30__length1 = a_length1; - _tmp31_ = ((_tmp30_ + 1) != NULL) ? _vala_array_dup4 (_tmp30_ + 1, _tmp30__length1 - 1) : (_tmp30_ + 1); + _tmp31_ = (_tmp30_ != NULL) ? _vala_array_dup4 (_tmp30_ + 1, _tmp30__length1 - 1) : _tmp30_; _tmp31__length1 = _tmp30__length1 - 1; c1 = _tmp31_; c1_length1 = _tmp31__length1; @@ -513,7 +513,7 @@ test_integer_array (void) assert (_tmp33_ == 11); _tmp34_ = a; _tmp34__length1 = a_length1; - _tmp35_ = ((_tmp34_ + 0) != NULL) ? _vala_array_dup5 (_tmp34_ + 0, 2 - 0) : (_tmp34_ + 0); + _tmp35_ = (_tmp34_ != NULL) ? _vala_array_dup5 (_tmp34_ + 0, 2 - 0) : _tmp34_; _tmp35__length1 = 2 - 0; c2 = _tmp35_; c2_length1 = _tmp35__length1; @@ -527,7 +527,7 @@ test_integer_array (void) _tmp38__length1 = a_length1; _tmp39_ = a; _tmp39__length1 = a_length1; - _tmp40_ = ((_tmp39_ + 0) != NULL) ? _vala_array_dup6 (_tmp39_ + 0, _tmp39__length1 - 0) : (_tmp39_ + 0); + _tmp40_ = (_tmp39_ != NULL) ? _vala_array_dup6 (_tmp39_ + 0, _tmp39__length1 - 0) : _tmp39_; _tmp40__length1 = _tmp39__length1 - 0; c3 = _tmp40_; c3_length1 = _tmp40__length1; diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index 27fda1d46..249219e6c 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -198,8 +198,11 @@ public class Vala.MemberAccess : Expression { public override bool is_non_null () { unowned Constant? c = symbol_reference as Constant; + unowned LocalVariable? l = symbol_reference as LocalVariable; 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); } else { return false; }