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>
This commit is contained in:
fbrouille
2025-07-07 11:55:56 +00:00
committed by Philip Withnall
parent 804553b126
commit 02597ca9d2
3 changed files with 105 additions and 12 deletions

View File

@@ -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;

103
gio/tests/file-enumerator.c Normal file
View File

@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <gio/gio.h>
#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 ();
}

View File

@@ -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},