From: Rico Tzschichholz Date: Wed, 3 Feb 2021 12:40:23 +0000 (+0100) Subject: vala: Don't allow GLib.Value casting to nullable struct/simple types X-Git-Tag: 0.51.1~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4d023541e4b013a05d55a258044b6a20e6fb7e01;p=thirdparty%2Fvala.git vala: Don't allow GLib.Value casting to nullable struct/simple types --- diff --git a/tests/Makefile.am b/tests/Makefile.am index bc5888bbe..b8b15b2cf 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -870,6 +870,7 @@ TESTS = \ semantic/assignment-same-variable.vala \ semantic/assignment-signal-incompatible-method.test \ semantic/assignment-signal-incompatible-type.test \ + semantic/cast-gvalue-unsupported.test \ semantic/cast-gvariant-unsupported.test \ semantic/chainup-gobject-incompatible-type-property.test \ semantic/chainup-gobject-unknown-property.test \ diff --git a/tests/semantic/cast-gvalue-unsupported.test b/tests/semantic/cast-gvalue-unsupported.test new file mode 100644 index 000000000..ef60d90ca --- /dev/null +++ b/tests/semantic/cast-gvalue-unsupported.test @@ -0,0 +1,6 @@ +Invalid Code + +void main () { + Value v = Value (typeof (int)); + var i = (int?) v; +} diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala index cc3dbca9e..9aff511fa 100644 --- a/vala/valacastexpression.vala +++ b/vala/valacastexpression.vala @@ -215,6 +215,10 @@ public class Vala.CastExpression : Expression { && is_gvalue (context, inner.value_type) && !is_gvalue (context, value_type)) { // GValue unboxing returns unowned value value_type.value_owned = false; + if (value_type.nullable && value_type.type_symbol != null && !value_type.type_symbol.is_reference_type ()) { + error = true; + Report.error (source_reference, "Casting of `GLib.Value' to `%s' is not supported", value_type.to_qualified_string ()); + } } inner.target_type = inner.value_type.copy ();