]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Break possible endless loop in SymbolResolver.get_type_for_struct()
authorFlorian Brosch <flo.brosch@gmail.com>
Wed, 10 Sep 2014 11:07:11 +0000 (13:07 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 18 Dec 2018 08:06:02 +0000 (09:06 +0100)
Required to deal with invalid code containing base struct cycles.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/444

vala/valasymbolresolver.vala

index 55fa94f629ed44a1640a1c9671404e72f17d4db2..7f07332b3d138e801c57ed9e83b6c65cc18b5b2c 100644 (file)
@@ -91,7 +91,7 @@ public class Vala.SymbolResolver : CodeVisitor {
                        if (base_type != null) {
                                if (base_type.is_subtype_of (st)) {
                                        st.error = true;
-                                       Report.error (base_type.source_reference, "Base struct cycle (`%s' and `%s')".printf (st.get_full_name (), base_type.get_full_name ()));
+                                       Report.error (st.source_reference, "Base struct cycle (`%s' and `%s')".printf (st.get_full_name (), base_type.get_full_name ()));
                                        return;
                                }
                        }
@@ -266,10 +266,33 @@ public class Vala.SymbolResolver : CodeVisitor {
                }
        }
 
+       bool has_base_struct_cycle (Struct st, Struct loop_st) {
+               if (!(st.base_type is UnresolvedType)) {
+                       return false;
+               }
+
+               var sym = resolve_symbol (((UnresolvedType) st.base_type).unresolved_symbol);
+               unowned Struct? base_struct = sym as Struct;
+               if (base_struct == null) {
+                       return false;
+               }
+
+               if (base_struct == loop_st) {
+                       return true;
+               }
+
+               return has_base_struct_cycle (base_struct, loop_st);
+       }
+
        DataType get_type_for_struct (Struct st, Struct base_struct) {
                if (st.base_type != null) {
                        // make sure that base type is resolved
 
+                       if (has_base_struct_cycle (st, st)) {
+                               // recursive declaration in base type
+                               return new StructValueType (st);
+                       }
+
                        if (current_scope == st.scope) {
                                // recursive declaration in generic base type
                                return new StructValueType (st);