From 38d61fbff037687ea4772e6df85c7e22a74b335e Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Sun, 18 Apr 2021 21:02:21 +0200 Subject: vala: Properly check GLib.Object naming convention for properties --- tests/Makefile.am | 2 ++ tests/objects/property-class-invalid-name.test | 8 +++++++ tests/objects/property-interface-invalid-name.test | 8 +++++++ vala/valaproperty.vala | 28 ++++++++++++++++++++++ vala/valasemanticanalyzer.vala | 5 ---- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tests/objects/property-class-invalid-name.test create mode 100644 tests/objects/property-interface-invalid-name.test diff --git a/tests/Makefile.am b/tests/Makefile.am index 5b2610dd6..fb1133555 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -480,6 +480,7 @@ TESTS = \ objects/property-read-only-auto.vala \ objects/property-read-only-member-write.test \ objects/property-read-only-write.test \ + objects/property-class-invalid-name.test \ objects/property-construct-only.vala \ objects/property-construct-only-write.test \ objects/property-construct-only-write-after.test \ @@ -487,6 +488,7 @@ TESTS = \ objects/property-delegate.vala \ objects/property-delegate-owned.vala \ objects/property-gboxed-nullable.vala \ + objects/property-interface-invalid-name.test \ objects/property-real-struct-assignment.vala \ objects/property-real-struct-no-accessor.test \ objects/property-simple-type-struct-nullable.vala \ diff --git a/tests/objects/property-class-invalid-name.test b/tests/objects/property-class-invalid-name.test new file mode 100644 index 000000000..b1fa27966 --- /dev/null +++ b/tests/objects/property-class-invalid-name.test @@ -0,0 +1,8 @@ +Invalid Code + +class Foo : Object { + public string 1foo { get; set; } +} + +void main () { +} diff --git a/tests/objects/property-interface-invalid-name.test b/tests/objects/property-interface-invalid-name.test new file mode 100644 index 000000000..e23273d49 --- /dev/null +++ b/tests/objects/property-interface-invalid-name.test @@ -0,0 +1,8 @@ +Invalid Code + +interface IFoo : Object { + public abstract string _foo { get; set; } +} + +void main () { +} diff --git a/vala/valaproperty.vala b/vala/valaproperty.vala index f07f137e0..3d2cbf78a 100644 --- a/vala/valaproperty.vala +++ b/vala/valaproperty.vala @@ -413,6 +413,14 @@ public class Vala.Property : Symbol, Lockable { checked = true; + if (context.profile == Profile.GOBJECT && parent_symbol is ObjectTypeSymbol + && ((ObjectTypeSymbol) parent_symbol).is_subtype_of (context.analyzer.object_type)) { + if (!is_valid_name (name)) { + error = true; + Report.error (source_reference, "Name `%s' is not valid for a GLib.Object property", name); + } + } + if (this_parameter != null) { this_parameter.check (context); } @@ -543,4 +551,24 @@ public class Vala.Property : Symbol, Lockable { return !error; } + + // Ported from glib's "g_param_spec_is_valid_name" + static bool is_valid_name (string name) { + char* p; + + if ((name[0] < 'A' || name[0] > 'Z') + && (name[0] < 'a' || name[0] > 'z')) { + return false; + } + + for (p = name; *p != '\0'; p++) { + char c = *p; + if (c != '-' && c != '_' && (c < '0' || c > '9') + && (c < 'A' || c > 'Z') && (c < 'a' || c > 'z')) { + return false; + } + } + + return true; + } } diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala index 9d2d7ea07..378c301bd 100644 --- a/vala/valasemanticanalyzer.vala +++ b/vala/valasemanticanalyzer.vala @@ -465,11 +465,6 @@ public class Vala.SemanticAnalyzer : CodeVisitor { return false; } - if (!prop.name[0].isalpha ()) { - // GObject requires properties to start with a letter - return false; - } - if (type_sym is Interface && !prop.is_abstract && !prop.external && !prop.external_package) { // GObject does not support non-abstract interface properties, // however we assume external properties always are GObject properties -- cgit v1.2.1