NetworkManager/backport-glib-aux-fix-atomic-pattern-in-nm_ref_string_unref.patch

38 lines
1.3 KiB
Diff

From 5f7a027f59f0dfd7b759c44106a9e3f3da7a8a57 Mon Sep 17 00:00:00 2001
From: Thomas Haller <thaller@redhat.com>
Date: Thu, 25 Jan 2024 15:16:31 +0100
Subject: [PATCH] glib-aux: fix atomic pattern in nm_ref_string_unref()
It's simply not valid to read the ref-count without an atomic.
The compiler might optimize out the assignment to "r" and read the
_ref_count field multiple times. Thereby, we might at first appear
to be larger than > 1, and later pass 1 to compare-and-exchange.
We need an atomic get here.
Fixes: 19d402782488 ('refstr: inline nm_ref_string_{ref,unref}()')
Conflict:NA
Reference:https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1847
---
src/libnm-glib-aux/nm-ref-string.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/libnm-glib-aux/nm-ref-string.h b/src/libnm-glib-aux/nm-ref-string.h
index c7cfe87fd0..45313b394e 100644
--- a/src/libnm-glib-aux/nm-ref-string.h
+++ b/src/libnm-glib-aux/nm-ref-string.h
@@ -83,7 +83,8 @@ nm_ref_string_unref(NMRefString *rstr)
/* fast-path: first try to decrement the ref-count without bringing it
* to zero. */
- r = rstr->_ref_count;
+ r = g_atomic_int_get(&rstr->_ref_count);
+
if (G_LIKELY(r > 1 && g_atomic_int_compare_and_exchange(&rstr->_ref_count, r, r - 1)))
return;
--
GitLab