@@ -173,8 +173,13 @@ cursor_clear(PyObject *op)
173173    Py_CLEAR (self -> row_factory );
174174    if  (self -> statement ) {
175175        /* Reset the statement if the user has not closed the cursor */ 
176-         stmt_reset (self -> statement );
176+         int   rc   =   stmt_reset (self -> statement );
177177        Py_CLEAR (self -> statement );
178+         if  (rc  !=  SQLITE_OK ) {
179+             pysqlite_state  * state  =  pysqlite_get_state_by_type (Py_TYPE (self ));
180+             PyErr_SetString (state -> InternalError , "cannot reset statement" );
181+             PyErr_FormatUnraisable ("Exception ignored in cursor_clear()" );
182+         }
178183    }
179184
180185    return  0 ;
@@ -834,7 +839,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
834839
835840    if  (self -> statement ) {
836841        // Reset pending statements on this cursor. 
837-         (void )stmt_reset (self -> statement );
842+         if  (stmt_reset (self -> statement ) !=  SQLITE_OK ) {
843+             goto reset_failure ;
844+         }
838845    }
839846
840847    PyObject  * stmt  =  get_statement_from_cache (self , operation );
@@ -858,7 +865,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
858865        }
859866    }
860867
861-     (void )stmt_reset (self -> statement );
868+     if  (stmt_reset (self -> statement ) !=  SQLITE_OK ) {
869+         goto reset_failure ;
870+     }
862871    self -> rowcount  =  self -> statement -> is_dml  ? 0L  : -1L ;
863872
864873    /* We start a transaction implicitly before a DML statement. 
@@ -940,7 +949,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
940949            if  (self -> statement -> is_dml ) {
941950                self -> rowcount  +=  (long )sqlite3_changes (self -> connection -> db );
942951            }
943-             stmt_reset (self -> statement );
952+             if  (stmt_reset (self -> statement ) !=  SQLITE_OK ) {
953+                 goto reset_failure ;
954+             }
944955        }
945956        Py_XDECREF (parameters );
946957    }
@@ -964,9 +975,11 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
964975    self -> locked  =  0 ;
965976
966977    if  (PyErr_Occurred ()) {
967-         if  (self -> statement ) {
968-             (void )stmt_reset (self -> statement );
978+         if  (self -> statement  &&  stmt_reset (self -> statement ) !=  SQLITE_OK ) {
969979            Py_CLEAR (self -> statement );
980+             PyObject  * exc  =  PyErr_GetRaisedException ();
981+             PyErr_SetString (state -> InternalError , "cannot reset statement" );
982+             PyErr_SetRaisedException (exc );
970983        }
971984        self -> rowcount  =  -1L ;
972985        return  NULL ;
@@ -975,6 +988,20 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
975988        Py_CLEAR (self -> statement );
976989    }
977990    return  Py_NewRef ((PyObject  * )self );
991+ 
992+ reset_failure :
993+     /* suite to execute when stmt_reset() failed and no exception is set */ 
994+     assert (!PyErr_Occurred ());
995+ 
996+     Py_XDECREF (parameters );
997+     Py_XDECREF (parameters_iter );
998+     Py_XDECREF (parameters_list );
999+ 
1000+     self -> locked  =  0 ;
1001+     self -> rowcount  =  -1L ;
1002+     Py_CLEAR (self -> statement );
1003+     PyErr_SetString (state -> InternalError , "cannot reset statement" );
1004+     return  NULL ;
9781005}
9791006
9801007/*[clinic input] 
@@ -1117,14 +1144,20 @@ pysqlite_cursor_iternext(PyObject *op)
11171144        if  (self -> statement -> is_dml ) {
11181145            self -> rowcount  =  (long )sqlite3_changes (self -> connection -> db );
11191146        }
1120-         ( void ) stmt_reset (self -> statement );
1147+         int   rc   =   stmt_reset (self -> statement );
11211148        Py_CLEAR (self -> statement );
1149+         if  (rc  !=  SQLITE_OK ) {
1150+             goto reset_failure ;
1151+         }
11221152    }
11231153    else  if  (rc  !=  SQLITE_ROW ) {
1124-         set_error_from_db (self -> connection -> state , self -> connection -> db );
1125-         ( void ) stmt_reset (self -> statement );
1154+         rc   =   set_error_from_db (self -> connection -> state , self -> connection -> db );
1155+         int   reset_ok   =   stmt_reset (self -> statement );
11261156        Py_CLEAR (self -> statement );
11271157        Py_DECREF (row );
1158+         if  (rc  ==  SQLITE_OK  &&  reset_ok  !=  SQLITE_OK ) {
1159+             goto reset_failure ;
1160+         }
11281161        return  NULL ;
11291162    }
11301163    if  (!Py_IsNone (self -> row_factory )) {
@@ -1134,6 +1167,12 @@ pysqlite_cursor_iternext(PyObject *op)
11341167        Py_SETREF (row , new_row );
11351168    }
11361169    return  row ;
1170+ 
1171+ reset_failure :
1172+     assert (!PyErr_Occurred ());
1173+     PyErr_SetString (self -> connection -> state -> InternalError ,
1174+                     "cannot reset statement" );
1175+     return  NULL ;
11371176}
11381177
11391178/*[clinic input] 
@@ -1292,7 +1331,11 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self)
12921331    }
12931332
12941333    if  (self -> statement ) {
1295-         (void )stmt_reset (self -> statement );
1334+         if  (stmt_reset (self -> statement ) !=  SQLITE_OK ) {
1335+             PyErr_SetString (self -> connection -> state -> InternalError ,
1336+                             "cannot close cursor" );
1337+             return  NULL ;
1338+         }
12961339        Py_CLEAR (self -> statement );
12971340    }
12981341
0 commit comments