From: Rico Tzschichholz Date: Mon, 15 Nov 2021 19:43:19 +0000 (+0100) Subject: codegen: Add and use CCodeConstantIdentifier for accessing constants X-Git-Tag: 0.48.20~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3726b5385f2a722a3a18cb428fbad56297ec1913;p=thirdparty%2Fvala.git codegen: Add and use CCodeConstantIdentifier for accessing constants If an address to a constant value is required then its identifier needs to be used instead of referring to a temporary copy. Fixes a regression of f1a8f2a4c6771124abd61fd0ebfa991c846575fe Found by -fsanitize=address --- diff --git a/ccode/Makefile.am b/ccode/Makefile.am index 631783f08..5db10bd77 100644 --- a/ccode/Makefile.am +++ b/ccode/Makefile.am @@ -27,6 +27,7 @@ libvalaccode_la_VALASOURCES = \ valaccodecomment.vala \ valaccodeconditionalexpression.vala \ valaccodeconstant.vala \ + valaccodeconstantidentifier.vala \ valaccodecontinuestatement.vala \ valaccodedeclaration.vala \ valaccodedeclarator.vala \ diff --git a/ccode/valaccodeconstantidentifier.vala b/ccode/valaccodeconstantidentifier.vala new file mode 100644 index 000000000..0495389f1 --- /dev/null +++ b/ccode/valaccodeconstantidentifier.vala @@ -0,0 +1,35 @@ +/* valaccodeconstantidentifier.vala + * + * Copyright (C) 2021 Rico Tzschichholz + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * Author: + * Rico Tzschichholz + */ + +using GLib; + +/** + * Represents a constant identifier in the C code. + */ +public class Vala.CCodeConstantIdentifier : CCodeIdentifier { + /** + * The name of this constant identifier. + */ + public CCodeConstantIdentifier (string name) { + base (name); + } +} diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index dd753a1cc..eafe71009 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -1427,7 +1427,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { } public static bool is_constant_ccode_expression (CCodeExpression cexpr) { - if (cexpr is CCodeConstant) { + if (cexpr is CCodeConstant || cexpr is CCodeConstantIdentifier) { return true; } else if (cexpr is CCodeCastExpression) { var ccast = (CCodeCastExpression) cexpr; @@ -6147,7 +6147,9 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { result.value_type.nullable = false; if (!result.lvalue || !result.value_type.equals (value.value_type)) { result.cvalue = get_implicit_cast_expression (result.cvalue, value.value_type, result.value_type, node); - result = (GLibValue) store_temp_value (result, node); + if (!(result.cvalue is CCodeConstantIdentifier)) { + result = (GLibValue) store_temp_value (result, node); + } } result.cvalue = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, result.cvalue); result.lvalue = false; diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala index d0a8ed109..7e052b6a1 100644 --- a/codegen/valaccodememberaccessmodule.vala +++ b/codegen/valaccodememberaccessmodule.vala @@ -185,8 +185,10 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule { s = current_method.get_full_name (); } set_cvalue (expr, new CCodeConstant ("\"%s\"".printf (s))); - } else { + } else if (c.type_reference.is_non_null_simple_type ()) { set_cvalue (expr, new CCodeConstant (get_ccode_name (c))); + } else { + set_cvalue (expr, new CCodeConstantIdentifier (get_ccode_name (c))); } if (array_type != null) { diff --git a/tests/Makefile.am b/tests/Makefile.am index e5096e405..f33c60674 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -100,6 +100,7 @@ TESTS = \ basic-types/bug788775.vala \ constants/array-type-invalid.test \ constants/glog.vala \ + constants/member-access.vala \ constants/strings.vala \ namespace/unique.vala \ arrays/cast-silent-invalid.test \ diff --git a/tests/constants/member-access.vala b/tests/constants/member-access.vala new file mode 100644 index 000000000..077a36474 --- /dev/null +++ b/tests/constants/member-access.vala @@ -0,0 +1,26 @@ +struct Foo { + public int i; +} + +const Foo FOO = { 23 }; +const Foo FAZ = { 42 }; + +class Bar { + public unowned Foo? foo; + + public Bar (Foo _foo) { + foo = _foo; + } +} + +void main () { + Bar bar; + { + bar = new Bar (FOO); + } + assert (bar.foo.i == 23); + { + bar.foo = FAZ; + } + assert (bar.foo.i == 42); +}