From 5b28d7b9c9bca78bcd8b46bfea94dd2e209ec1f4 Mon Sep 17 00:00:00 2001
From: Daniele Varrazzo <daniele.varrazzo@gmail.com>
Date: Fri, 28 Dec 2018 04:39:15 +0100
Subject: [PATCH] Dropped possible wrong code path in conn_decode

It shouldn't happen for both cdecoder and pydecoder to be null,
but just in case...
---
 psycopg/connection_int.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)

diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c
index a60c4a9b..7f6b7551 100644
--- a/psycopg/connection_int.c
+++ b/psycopg/connection_int.c
@@ -121,10 +121,6 @@ exit:
 PyObject *
 conn_decode(connectionObject *self, const char *str, Py_ssize_t len)
 {
-    PyObject *b = NULL;
-    PyObject *t = NULL;
-    PyObject *rv = NULL;
-
     if (len < 0) { len = strlen(str); }
 
     if (self) {
@@ -132,22 +128,28 @@ conn_decode(connectionObject *self, const char *str, Py_ssize_t len)
             return self->cdecoder(str, len, NULL);
         }
         else if (self->pydecoder) {
-            if (!(b = Bytes_FromStringAndSize(str, len))) { goto exit; }
+            PyObject *b = NULL;
+            PyObject *t = NULL;
+            PyObject *rv = NULL;
+
+            if (!(b = Bytes_FromStringAndSize(str, len))) { goto error; }
             if (!(t = PyObject_CallFunctionObjArgs(self->pydecoder, b, NULL))) {
-                goto exit;
+                goto error;
             }
-            rv = PyTuple_GetItem(t, 0);
-            Py_XINCREF(rv);
+            if (!(rv = PyTuple_GetItem(t, 0))) { goto error; }
+            Py_INCREF(rv);      /* PyTuple_GetItem gives a borrowes one */
+error:
+            Py_XDECREF(t);
+            Py_XDECREF(b);
+            return rv;
+        }
+        else {
+            return PyUnicode_FromStringAndSize(str, len);
         }
     }
     else {
         return PyUnicode_FromStringAndSize(str, len);
     }
-
-exit:
-    Py_XDECREF(t);
-    Py_XDECREF(b);
-    return rv;
 }
 
 /* conn_notice_callback - process notices */