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: 19d4027824 ('refstr: inline nm_ref_string_{ref,unref}()')

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1847
(cherry picked from commit 5f7a027f59)
This commit is contained in:
Thomas Haller 2024-01-25 15:16:31 +01:00 committed by Íñigo Huguet
parent 40a77de88e
commit 0904bab5e2

View file

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