Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null sentinel to null-terminated array of pointers #70

Open
Gothem opened this issue Aug 24, 2024 · 1 comment
Open

Add null sentinel to null-terminated array of pointers #70

Gothem opened this issue Aug 24, 2024 · 1 comment
Labels
bug Something isn't working gir The issue is related to GIR files upstream The issue resides upstream
Milestone

Comments

@Gothem
Copy link

Gothem commented Aug 24, 2024

Currently the bindings define them as [*]*T pointers (for example in gio.DBusSignalInfo), when in reality they should be [*:null]?*T.

This prevents Segfaults when programs expect the arrays to terminate with null.

@ianprime0509 ianprime0509 added bug Something isn't working upstream The issue resides upstream gir The issue is related to GIR files labels Aug 25, 2024
@ianprime0509 ianprime0509 added this to the v0.3.0 milestone Aug 25, 2024
@ianprime0509
Copy link
Owner

Thanks for the report! Unfortunately this looks like an upstream issue with the GIR: here's what the signals field of DBusInterfaceInfo looks like as of the GNOME 46 SDK (I think this is what you're referring to):

      <field name="signals" writable="1">
        <doc xml:space="preserve"
             filename="gio/gdbusintrospection.h"
             line="143">A pointer to a %NULL-terminated array of pointers to #GDBusSignalInfo structures or %NULL if there are no signals.</doc>
        <array c:type="GDBusSignalInfo**">
          <type name="DBusSignalInfo" c:type="GDBusSignalInfo*"/>
        </array>
      </field>

Although the documentation mentions this is a null-terminated array, the array type here is missing zero-terminated="1" which would allow the translate-gir logic to create the appropriate type.

A case where this is marked up correctly in GIR is the return value of g_desktop_app_info_get_string_list:

        <return-value transfer-ownership="full">
          <doc xml:space="preserve"
               filename="gio/gdesktopappinfo.c"
               line="5106">
 a %NULL-terminated string array or %NULL if the specified
 key cannot be found. The array should be freed with g_strfreev().</doc>
          <array length="1" zero-terminated="1" c:type="gchar**">
            <type name="utf8"/>
          </array>
        </return-value>

This case is correctly translated to [*:null]?[*:0]u8:

zig-gobject/src/translate.zig

Lines 1932 to 1938 in 787a447

try testTranslateArrayType("[*:null]?[*:0]u8", .{
.c_type = "gchar**",
.zero_terminated = true,
.element = &.{
.simple = .{ .name = .{ .ns = null, .local = "utf8" }, .c_type = null },
},
}, .{});

So there are a few things I'm planning to do based on this issue:

  1. Look for other cases (based on the documentation) where the same problem exists, and work around them all by patching the upstream GIR files via gir-fixes.
  2. Add more test coverage for translateArray just to make sure zero-terminated="1" works in other possible cases (in addition to the one mentioned above with a string array).
  3. Open a tracking issue for the upstream GIR fixes that need to be made, and look into sending the corrections upstream so the gir-fixes patches can eventually be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working gir The issue is related to GIR files upstream The issue resides upstream
Projects
None yet
Development

No branches or pull requests

2 participants