Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modulemd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ c_tests = {
'service_level' : [ 'tests/test-modulemd-service-level.c' ],
'translation' : [ 'tests/test-modulemd-translation.c' ],
'translation_entry' : [ 'tests/test-modulemd-translation-entry.c' ],
'variant_deep_copy' : [ 'tests/test-modulemd-variant_deep_copy.c' ],
'obsoletes' : [ 'tests/test-modulemd-obsoletes.c' ],
'quoting' : [ 'tests/test-modulemd-quoting.c' ],
}
Expand Down
8 changes: 6 additions & 2 deletions modulemd/modulemd-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,13 @@ modulemd_variant_deep_copy (GVariant *variant)
{
const GVariantType *data_type = g_variant_get_type (variant);
gsize data_size = g_variant_get_size (variant);
gpointer data = g_malloc0 (data_size);
gpointer data = NULL;

g_variant_store (variant, data);
if (data_size > 0)
{
data = g_malloc0 (data_size);
g_variant_store (variant, data);
}

return g_variant_ref_sink (g_variant_new_from_data (
data_type, data, data_size, FALSE, destroy_variant_data, data));
Expand Down
69 changes: 69 additions & 0 deletions modulemd/tests/test-modulemd-variant_deep_copy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This file is part of libmodulemd
* Copyright (C) 2025 Red Hat, Inc.
*
* Fedora-License-Identifier: MIT
* SPDX-2.0-License-Identifier: MIT
* SPDX-3.0-License-Identifier: MIT
*
* This program is free software.
* For more information on the license, see COPYING.
* For more information on free software, see <https://www.gnu.org/philosophy/free-sw.en.html>.
*/

#include <glib.h>
#include <glib/gprintf.h>
#include <locale.h>
#include <stdlib.h>

#include "private/modulemd-util.h"

/*
* modulemd_variant_deep_copy() triggered a GLib critical warning
* from glib >= 2.84.1 when parsing a /data/xmd modulemd-stream-v2 element
* with {} value (an empty flow mapping). This test exhibits that code path
* and relies on G_DEBUG=fatal-criticals environment variable to crash the
* test.
* <https://github.com/fedora-modularity/libmodulemd/issues/623>.
*/
static void
test_empty_a_sv (void)
{
g_autoptr (GVariantDict) dictionary =
NULL; /* g_variant_dict_end() does not free */
g_autoptr (GVariant) input = NULL;
g_autoptr (GVariant) output = NULL;

/* Build a GVariant with an empty dictionary, results to an "a{sv}" of
* zero size. */
dictionary = g_variant_dict_new (NULL);
input = g_variant_dict_end (dictionary);

/* Exhibit the library. */
output = modulemd_variant_deep_copy (input);
g_assert_true (output != NULL);

/* Compare the content. */
g_assert_true (g_variant_get_type (output) == g_variant_get_type (input));
g_assert_true (g_variant_get_size (output) == g_variant_get_size (input));
}


int
main (int argc, char *argv[])
{
setlocale (LC_ALL, "");
g_test_init (&argc, &argv, NULL);
g_test_set_nonfatal_assertions ();

if (!g_setenv ("G_DEBUG", "fatal-criticals", TRUE))
{
g_fprintf (stderr, "Failed to set G_DEBUG environment variable.\n");
exit (EXIT_FAILURE);
}

g_test_add_func ("/modulemd/util/variant_deep_copy/empty_a{sv}",
test_empty_a_sv);

return g_test_run ();
}
Loading