From 02597ca9d2b99e2a827ade6f04b4988ca5bfe37c Mon Sep 17 00:00:00 2001 From: fbrouille <150549-fbrouille@users.noreply.gitlab.gnome.org> Date: Mon, 7 Jul 2025 11:55:56 +0000 Subject: [PATCH] gio: call g_file_enumerator_close in dispose `g_file_enumerator_finalize` should not call `g_file_enumerator_close` because object parts (i.e. subclass variable and/or resources) might already be freed, causing memory safety issues. A better place to call `g_file_enumerator_close` is `g_file_enumerator_dispose` because it is safe to access the object memory here. Fixes #3713 Signed-off-by: fbrouille <150549-fbrouille@users.noreply.gitlab.gnome.org> --- gio/gfileenumerator.c | 13 +---- gio/tests/file-enumerator.c | 103 ++++++++++++++++++++++++++++++++++++ gio/tests/meson.build | 1 + 3 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 gio/tests/file-enumerator.c diff --git a/gio/gfileenumerator.c b/gio/gfileenumerator.c index f543cd1c6..ae406a124 100644 --- a/gio/gfileenumerator.c +++ b/gio/gfileenumerator.c @@ -127,20 +127,10 @@ g_file_enumerator_dispose (GObject *object) enumerator->priv->container = NULL; } - G_OBJECT_CLASS (g_file_enumerator_parent_class)->dispose (object); -} - -static void -g_file_enumerator_finalize (GObject *object) -{ - GFileEnumerator *enumerator; - - enumerator = G_FILE_ENUMERATOR (object); - if (!enumerator->priv->closed) g_file_enumerator_close (enumerator, NULL, NULL); - G_OBJECT_CLASS (g_file_enumerator_parent_class)->finalize (object); + G_OBJECT_CLASS (g_file_enumerator_parent_class)->dispose (object); } static void @@ -150,7 +140,6 @@ g_file_enumerator_class_init (GFileEnumeratorClass *klass) gobject_class->set_property = g_file_enumerator_set_property; gobject_class->dispose = g_file_enumerator_dispose; - gobject_class->finalize = g_file_enumerator_finalize; klass->next_files_async = g_file_enumerator_real_next_files_async; klass->next_files_finish = g_file_enumerator_real_next_files_finish; diff --git a/gio/tests/file-enumerator.c b/gio/tests/file-enumerator.c new file mode 100644 index 000000000..9d43285ec --- /dev/null +++ b/gio/tests/file-enumerator.c @@ -0,0 +1,103 @@ +/* GLib testing framework examples and tests + * + * Copyright 2025 GNOME Foundation, Inc. + * + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see . + */ + +#include + +#define TYPE_TEST_FILE_ENUMERATOR (test_file_enumerator_get_type ()) +#define TEST_FILE_ENUMERATOR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), TYPE_TEST_FILE_ENUMERATOR, TestFileEnumerator)) +#define IS_TEST_FILE_ENUMERATOR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), TYPE_TEST_FILE_ENUMERATOR)) + +typedef struct +{ + GFileEnumerator parent_instance; + unsigned int n_times_closed; // Number of times the enumerator has been closed +} TestFileEnumerator; + +typedef struct +{ + GFileEnumeratorClass parent_class; +} TestFileEnumeratorClass; + +GType test_file_enumerator_get_type (void) G_GNUC_CONST; + +G_DEFINE_TYPE (TestFileEnumerator, test_file_enumerator, G_TYPE_FILE_ENUMERATOR) + +static gboolean +test_file_enumerator_close (GFileEnumerator *enumerator, + GCancellable *cancellable, + GError **error) +{ + TestFileEnumerator *test = TEST_FILE_ENUMERATOR (enumerator); + ++test->n_times_closed; + return TRUE; +} + +static void +test_file_enumerator_init (TestFileEnumerator *enumerator) +{ + enumerator->n_times_closed = 0; +} + +static void +test_file_enumerator_class_init (TestFileEnumeratorClass *klass) +{ + GFileEnumeratorClass *enumerator_class; + + enumerator_class = G_FILE_ENUMERATOR_CLASS (klass); + enumerator_class->close_fn = test_file_enumerator_close; +} + +static void +test_close_on_dispose (void) +{ + GFile *dir; + TestFileEnumerator *enumerator; + + dir = g_file_new_for_path (g_get_tmp_dir ()); + + enumerator = g_object_new (TYPE_TEST_FILE_ENUMERATOR, + "container", dir, + NULL); + + // Check enumerator is not closed initially + g_assert_cmpuint (enumerator->n_times_closed, ==, 0); + + g_object_run_dispose (G_OBJECT (enumerator)); + + // Check enumerator is closed after 1st dispose + g_assert_cmpuint (enumerator->n_times_closed, ==, 1); + + g_object_run_dispose (G_OBJECT (enumerator)); + + // Check enumerator is not closed twice after 2nd dispose + g_assert_cmpuint (enumerator->n_times_closed, ==, 1); + + g_object_unref (enumerator); + g_object_unref (dir); +} + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/file-enumerator/close-on-dispose", test_close_on_dispose); + return g_test_run (); +} diff --git a/gio/tests/meson.build b/gio/tests/meson.build index 4663a7182..9fecbf569 100644 --- a/gio/tests/meson.build +++ b/gio/tests/meson.build @@ -98,6 +98,7 @@ gio_tests = { # FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/3148 'can_fail' : host_system in ['darwin', 'windows', 'gnu'], }, + 'file-enumerator' : {}, 'inet-address' : {}, 'io-stream' : {}, 'max-version' : {'install' : false},