Error storing dictionary with string keys as GSettings on macOS
When storing dictionaries with string keys via GSettings on macOS there is an error.
The issue is that dictionaries with string keys are treated differently from other dictionaries (i.e. with integer keys). The code that handles the string keys is flawed. It uses a GVariantIter to retrieve the key and the value of the GVariant dict. However, it assumes that it receives a GVariant pointer for both the string key and the value. This assumption is wrong as g_variant_iter_loop (&iter, "{s*}", &name, &value)
directly returns a char pointer for name and not a GVariant containing the string value.
In the current code, this bug leads to a BAD_ACCESS exception as soon as we call g_variant_get_string (name, NULL)
(for obvious reasons).
MRE:
main.c
#include <glib.h>
#include <gio/gio.h>
int main()
{
GSettings *settings = g_settings_new("org.glib.test");
GVariantBuilder *builder;
GVariant *child = g_variant_new_string("bar");
// Case 1
builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
g_variant_builder_add(builder, "{sv}", "foo", child);
GVariant *builtVariant = g_variant_builder_end(builder);
g_variant_builder_unref(builder);
g_print("%s", g_variant_print(builtVariant, TRUE));
g_settings_set_value(settings, "test", builtVariant);
}
test.gschema.xml
<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
<schema path="/org/glib/test/" id="org.glib.test">
<key name="test" type="a{sv}">
<default>[]</default>
<summary>X</summary>
<description>X</description>
</key>
</schema>
</schemalist>
I created !2325 (merged) to address this issue. However, I have zero experience with this project so please feel free to do any necessary changes to the MR!
Cheers, Maurice