Skip to content

libibus: make an IBusText own an updated IBusAttrList reference - #2890

Merged
fujiwarat merged 2 commits into
ibus:mainfrom
cthbleachbit:ibustext-owning-ref-ibusattrlist
May 28, 2026
Merged

fujiwarat merged 2 commits into
ibus:mainfrom
cthbleachbit:ibustext-owning-ref-ibusattrlist

Conversation

@cthbleachbit

@cthbleachbit cthbleachbit commented Mar 27, 2026 •

Copy link
Copy Markdown

For all usages of IBusAttrList, the list is an owned reference within a
IBusText struct. ibus_panel_convert_text() and
ibus_input_context_convert_text() violate the invariant by assigning a
floating IBusAttrList reference to an IBusText without sinking it.

When GJS attempts to create a JS representation of an existing GObject,
it unconditionally sinks the incoming reference. For a non-floating
reference this up-refs the object.

For this floating IBusAttrLit, g_object_ref_sink converts it to a
strong reference in-place leaving ref-count at 1. At this point the
IBusAttrList is referenced by both the enclosing IBusText and the
newly created GJS wrapper. When one of them is ref-downed the object
is recycled, leaving the other reference dangling.

To fix this we just need to maintain the list as a non-floating ref.
We already have a ibus_text_set_attributes() that does the intended
ref sinking, so just use that instead of manually assigning pointers.

Fixes #2889.

@cthbleachbit
cthbleachbit force-pushed the ibustext-owning-ref-ibusattrlist branch 2 times, most recently from 5c9b040 to b52b51b Compare March 27, 2026 04:52
@cthbleachbit
cthbleachbit marked this pull request as ready for review March 27, 2026 05:17
@cthbleachbit
cthbleachbit force-pushed the ibustext-owning-ref-ibusattrlist branch from b52b51b to 9ecbd94 Compare April 2, 2026 02:47
@fujiwarat
fujiwarat self-requested a review April 26, 2026 06:15
@fujiwarat
fujiwarat force-pushed the ibustext-owning-ref-ibusattrlist branch 3 times, most recently from d586888 to 6cccd40 Compare May 26, 2026 09:30
@fujiwarat

Copy link
Copy Markdown
Member

I added a test case of test-deserialize.py and it works for IBusInputContext but it causes a small memory failure of GVariant with IBusPanelService and the Python failure is detected in both autotool test-deserialize.py.log and meson testlog.txt at present.

/usr/lib64/python3.14/site-packages/gi/overrides/GLib.py:221: Warning: g_atomic_ref_count_dec: assertion 'old_value > 0' failed
  self.unref()

I may delete the IBusPanelService test in the future.

@fujiwarat

Copy link
Copy Markdown
Member

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new Python test suite (test-deserialize.py) to verify deserialization of input contexts and panels, updating the Autotools and Meson build configurations accordingly. It also refactors attribute handling in ibusinputcontext.c and ibuspanelservice.c to use ibus_text_set_attributes. The review feedback highlights critical memory leak issues: first, the GObject Introspection annotation for ibus_text_set_attributes should be (transfer none) instead of (transfer full) to prevent leaks in language bindings; second, the GDBusMethodInvocation object must be unreffed during dry-run tests to avoid leaking it. Additionally, the reviewer suggests minor cleanups in the new Python test, such as removing unnecessary semicolons and deleting an unused helper method with a typo.

Comment thread src/ibustext.h Resolved
Comment thread src/ibuspanelservice.c
Comment on lines +1267 to +1269
is_dry_test = !g_strcmp0 (object_path, IBUS_PATH_PANEL "/DryTest");
if (!is_dry_test)
g_dbus_method_invocation_return_value (invocation, NULL);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When is_dry_test is true, g_dbus_method_invocation_return_value is not called. However, invocation is still passed as a parameter and must be completed or unreffed to avoid leaking the GDBusMethodInvocation object. Consider calling g_object_unref on invocation in the else branch.

        is_dry_test = !g_strcmp0 (object_path, IBUS_PATH_PANEL "/DryTest");
        if (!is_dry_test)
            g_dbus_method_invocation_return_value (invocation, NULL);
        else
            g_object_unref (invocation);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally your suggestion is right but the invocation is generated by Gio.DBusMethodInvocation() in bindings/pygobject/test-deserialize.py and there is no constructor so your suggestion does not work because g_dbus_method_invocation_finalize() would be failed because of no invocation->connection if we call g_object_unref().

Comment thread bindings/pygobject/test-deserialize.py Resolved
Comment thread bindings/pygobject/test-deserialize.py Resolved
Comment thread bindings/pygobject/test-deserialize.py Resolved
@fujiwarat
fujiwarat force-pushed the ibustext-owning-ref-ibusattrlist branch 2 times, most recently from e9a4db9 to f82697f Compare May 27, 2026 04:27
cthbleachbit and others added 2 commits May 28, 2026 11:35
For all usages of IBusAttrList, the list is an owned reference within a
IBusText struct. `ibus_panel_convert_text()` and
`ibus_input_context_convert_text()` violate the invarient by assigning a
**floating** IBusAttrList reference to anIBusText without sinking it.

When GJS attempts to create a JS representation of an existing GObject,
it unconditionally sinks the incoming reference.[1] For a non-floating
reference this up-refs the object.

For this floating IBusAttrLit, `g_object_ref_sink` converts it to a
strong reference in-place leaving ref-count at 1. At this point the
`IBusAttrList` is referenced by both the enclosing `IBusText` and the
newly created GJS wrapper. When one of them is ref-downed the object
is recycled, leaving the other reference dangling.

To fix this we just need to maintain the list as a non-floating ref.
We already have a `ibus_text_set_attributes()` that does the intended
ref sinking, so just use that instead of manually assigning pointers.

[1]: https://gitlab.gnome.org/GNOME/gjs/-/blob/db450465ab0161e131a92992c2509507aa6152aa/gi/object.cpp#L3597

Closes: ibus#2889
Add a test case to deserialize IBusText

Closes: ibus#2889
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation fault / double free in Jetbrains IDE crashes entire Gnome DE

2 participants