]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-105927: _ssl GET_SOCKET() uses _PyWeakref_GET_REF() (#106002)
authorVictor Stinner <vstinner@python.org>
Fri, 23 Jun 2023 01:02:02 +0000 (03:02 +0200)
committerGitHub <noreply@github.com>
Fri, 23 Jun 2023 01:02:02 +0000 (03:02 +0200)
Modules/_ssl.c

index 9ce61a651c2cbb8eba07552e7b4d586e2f462f11..4254fde0f5190bab548df4bcb32be47cfbc7d201 100644 (file)
@@ -383,10 +383,20 @@ typedef enum {
 #define ERRSTR1(x,y,z) (x ":" y ": " z)
 #define ERRSTR(x) ERRSTR1("_ssl.c", Py_STRINGIFY(__LINE__), x)
 
-/* Get the socket from a PySSLSocket, if it has one */
+// Get the socket from a PySSLSocket, if it has one.
+// Return a borrowed reference.
 static inline PySocketSockObject* GET_SOCKET(PySSLSocket *obj) {
     if (obj->Socket) {
-        return (PySocketSockObject *)PyWeakref_GetObject(obj->Socket);
+        PyObject *sock = _PyWeakref_GET_REF(obj->Socket);
+        if (sock != NULL) {
+            // GET_SOCKET() returns a borrowed reference
+            Py_DECREF(sock);
+        }
+        else {
+            // dead weak reference
+            sock = Py_None;
+        }
+        return (PySocketSockObject *)sock;  // borrowed reference
     }
     else {
         return NULL;