From 066220a75c020b818aab9c2f5c3a7db835fa871a Mon Sep 17 00:00:00 2001 From: Mathieu Othacehe Date: Wed, 9 Nov 2022 16:12:52 +0100 Subject: [PATCH 1/1] Remove the finalizer on device pointers. Fixes: * parted/device.scm (%device-destroy): Remove it. (pointer->device!): Do not set a finalizer. --- parted/device.scm | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/parted/device.scm b/parted/device.scm index 56a774b..be7f0ac 100644 --- a/parted/device.scm +++ b/parted/device.scm @@ -43,20 +43,23 @@ device-get-minimum-alignment device-get-optimum-alignment)) -;; Record all devices, so that pointer finalizers are only set once, -;; even if get-device returns an already known pointer. Use the -;; pointer as key and the associated as value. -(define %devices (make-weak-value-hash-table)) - -(define %device-destroy - (libparted->pointer "ped_device_destroy")) - +;; Record all devices, so that we do not end up with different +;; objects aliasing the same underlying C pointer. Use the pointer as key and +;; the associated as value. +(define %devices (make-hash-table)) + +;; %DEVICES was a weak hash-table and we used to set a finalizer on POINTER. +;; This is inevitably causing double free issues for the following reason: +;; +;; When goes out of scope and is removed from the %DEVICES table, the +;; finalizer that is set on the underlying C pointer is still registered but +;; possibly not called as finalization happens is a separate thread. If a +;; subsequent call to ped_device_get returns the same C pointer, another +;; finalizer will be registered. This means that the finalization function +;; can be called twice on the same pointer, causing a double free issue. (define (pointer->device! pointer) - ;; Check if a finalizer is already registered for this pointer. (or (hash-ref %devices pointer) (let ((device (pointer->device pointer))) - ;; Contrary to its name, this "adds" a finalizer. - (set-pointer-finalizer! pointer %device-destroy) (hash-set! %devices pointer device) device))) -- 2.38.0