]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-154037: Fix race in the hash(Decimal) (#154045)
authorSergey B Kirpichev <skirpichev@gmail.com>
Thu, 30 Jul 2026 15:02:59 +0000 (18:02 +0300)
committerGitHub <noreply@github.com>
Thu, 30 Jul 2026 15:02:59 +0000 (15:02 +0000)
Modules/_decimal/_decimal.c

index 6fbce0ed85e695df1f5e165c18f37821f927e997..ac3ffb23248f342025a0f00bc7153f6db4a5be80 100644 (file)
@@ -5935,11 +5935,13 @@ static Py_hash_t
 dec_hash(PyObject *op)
 {
     PyDecObject *self = _PyDecObject_CAST(op);
-    if (self->hash == -1) {
-        self->hash = _dec_hash(self);
-    }
+    Py_hash_t hash = FT_ATOMIC_LOAD_SSIZE_RELAXED(self->hash);
 
-    return self->hash;
+    if (hash == -1) {
+        hash = _dec_hash(self);
+        FT_ATOMIC_STORE_SSIZE_RELAXED(self->hash, hash);
+    }
+    return hash;
 }
 
 /*[clinic input]