stefano-m / lua-dbus_proxy

Simple API around GLib's GIO:GDBusProxy built on top of lgi
https://stefano-m.github.io/lua-dbus_proxy/
Apache License 2.0
17 stars 7 forks source link

Fix/duplicated method property names #9

Closed stefano-m closed 3 years ago

stefano-m commented 3 years ago

See #8

It may happen that a Proxy object exposes properties and methods with the same name. This may cause crashes or undefined behaviour.

Let's put aside the weirdness of having methods and properties with the same name: apparently that's allowed, and systemd has that.

What happened was that at object construction, we first set its metatable and then generatedd the fields (for each interface: methods, signals, accessors in this order). Now, if an accessor with name X is created before a method with the same name, at the point where we assign the method to the object, we will call __newindex on the metatable. This will look at the accessors first and will find one, then attempting to call the 'setter' method on that accessor. If the accessor is read-only, the code crashes, otherwise the behaviour is undefined.

The first fix is to set the metatable after we generate the methods to avoid such clashes. However, this means that now we don't have direct access to the property, that is 'proxy.X' will always return the method. The property still exists though and can be called indirectly as 'proxy.accessors.X.getter(proxy)' or 'proxy.accessors.X.setter(proxy, value)', which frankly is not acceptable.

To work around that, we tentatively (that is, if we don't overwrite anything else) add an underscore to the property name, so we can see both the method 'proxy.X', and the property 'proxy._X'.

codecov[bot] commented 3 years ago

Codecov Report

Merging #9 (35b6774) into master (cd75d7c) will decrease coverage by 1.74%. The diff coverage is 50.00%.

@@            Coverage Diff             @@
##           master       #9      +/-   ##
==========================================
- Coverage   94.33%   92.59%   -1.75%     
==========================================
  Files           5        5              
  Lines         265      270       +5     
==========================================
  Hits          250      250              
- Misses         15       20       +5