@@ -914,7 +914,9 @@ func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
914914 if (args ) {
915915 pysqlite_CallbackContext * ctx = sqlite3_user_data (context );
916916 assert (ctx != NULL );
917+ Py_INCREF (ctx );
917918 py_retval = PyObject_CallObject (ctx -> callable , args );
919+ Py_DECREF (ctx );
918920 Py_DECREF (args );
919921 }
920922
@@ -942,6 +944,8 @@ step_callback(sqlite3_context *context, int argc, sqlite3_value **params)
942944
943945 pysqlite_CallbackContext * ctx = sqlite3_user_data (context );
944946 assert (ctx != NULL );
947+ // Hold a reference to 'ctx' to prevent concurrent mutations.
948+ Py_INCREF (ctx );
945949
946950 aggregate_instance = (PyObject * * )sqlite3_aggregate_context (context , sizeof (PyObject * ));
947951 if (aggregate_instance == NULL ) {
@@ -971,6 +975,7 @@ step_callback(sqlite3_context *context, int argc, sqlite3_value **params)
971975 }
972976
973977 function_result = PyObject_CallObject (stepmethod , args );
978+ Py_CLEAR (ctx );
974979 Py_DECREF (args );
975980
976981 if (!function_result ) {
@@ -979,6 +984,7 @@ step_callback(sqlite3_context *context, int argc, sqlite3_value **params)
979984 }
980985
981986error :
987+ Py_XDECREF (ctx );
982988 Py_XDECREF (stepmethod );
983989 Py_XDECREF (function_result );
984990
@@ -1011,8 +1017,10 @@ final_callback(sqlite3_context *context)
10111017
10121018 pysqlite_CallbackContext * ctx = sqlite3_user_data (context );
10131019 assert (ctx != NULL );
1020+ Py_INCREF (ctx );
10141021 function_result = PyObject_CallMethodNoArgs (* aggregate_instance ,
10151022 ctx -> state -> str_finalize );
1023+ Py_DECREF (ctx );
10161024 Py_DECREF (* aggregate_instance );
10171025
10181026 ok = 0 ;
@@ -1163,6 +1171,8 @@ inverse_callback(sqlite3_context *context, int argc, sqlite3_value **params)
11631171
11641172 pysqlite_CallbackContext * ctx = sqlite3_user_data (context );
11651173 assert (ctx != NULL );
1174+ // Hold a reference to 'ctx' to prevent concurrent mutations.
1175+ Py_INCREF (ctx );
11661176
11671177 int size = sizeof (PyObject * );
11681178 PyObject * * cls = (PyObject * * )sqlite3_aggregate_context (context , size );
@@ -1191,9 +1201,11 @@ inverse_callback(sqlite3_context *context, int argc, sqlite3_value **params)
11911201 "user-defined aggregate's 'inverse' method raised error" );
11921202 goto exit ;
11931203 }
1204+ Py_CLEAR (ctx );
11941205 Py_DECREF (res );
11951206
11961207exit :
1208+ Py_XDECREF (ctx );
11971209 Py_XDECREF (method );
11981210 PyGILState_Release (gilstate );
11991211}
@@ -1217,7 +1229,10 @@ value_callback(sqlite3_context *context)
12171229 assert (cls != NULL );
12181230 assert (* cls != NULL );
12191231
1232+ Py_INCREF (ctx );
12201233 PyObject * res = PyObject_CallMethodNoArgs (* cls , ctx -> state -> str_value );
1234+ Py_DECREF (ctx );
1235+
12211236 if (res == NULL ) {
12221237 int attr_err = PyErr_ExceptionMatches (PyExc_AttributeError );
12231238 set_sqlite_error (context , attr_err
@@ -1360,10 +1375,11 @@ authorizer_callback(void *ctx_vp, int action, const char *arg1,
13601375
13611376 assert (ctx_vp != NULL );
13621377 pysqlite_CallbackContext * ctx = pysqlite_CallbackContext_CAST (ctx_vp );
1363- PyObject * callable = ctx -> callable ;
1364- ret = PyObject_CallFunction (callable , "issss" , action , arg1 , arg2 , dbname ,
1365- access_attempt_source );
1378+ // Hold a reference to 'ctx' to prevent concurrent mutations.
1379+ Py_INCREF (ctx );
13661380
1381+ ret = PyObject_CallFunction (ctx -> callable , "issss" , action , arg1 , arg2 ,
1382+ dbname , access_attempt_source );
13671383 if (ret == NULL ) {
13681384 print_or_clear_traceback (ctx );
13691385 rc = SQLITE_DENY ;
@@ -1381,6 +1397,7 @@ authorizer_callback(void *ctx_vp, int action, const char *arg1,
13811397 }
13821398 Py_DECREF (ret );
13831399 }
1400+ Py_DECREF (ctx );
13841401
13851402 PyGILState_Release (gilstate );
13861403 return rc ;
@@ -1396,8 +1413,10 @@ progress_callback(void *ctx_vp)
13961413
13971414 assert (ctx_vp != NULL );
13981415 pysqlite_CallbackContext * ctx = pysqlite_CallbackContext_CAST (ctx_vp );
1399- PyObject * callable = ctx -> callable ;
1400- ret = PyObject_CallNoArgs (callable );
1416+ // Hold a reference to 'ctx' to prevent concurrent mutations.
1417+ Py_INCREF (ctx );
1418+
1419+ ret = PyObject_CallNoArgs (ctx -> callable );
14011420 if (!ret ) {
14021421 /* abort query if error occurred */
14031422 rc = -1 ;
@@ -1409,7 +1428,7 @@ progress_callback(void *ctx_vp)
14091428 if (rc < 0 ) {
14101429 print_or_clear_traceback (ctx );
14111430 }
1412-
1431+ Py_DECREF ( ctx );
14131432 PyGILState_Release (gilstate );
14141433 return rc ;
14151434}
@@ -1455,7 +1474,9 @@ trace_callback(unsigned int type, void *ctx_vp, void *stmt, void *sql)
14551474 sqlite3_free ((void * )expanded_sql );
14561475 }
14571476 if (py_statement ) {
1477+ Py_INCREF (ctx );
14581478 PyObject * ret = PyObject_CallOneArg (ctx -> callable , py_statement );
1479+ Py_DECREF (ctx );
14591480 Py_DECREF (py_statement );
14601481 Py_XDECREF (ret );
14611482 }
@@ -1889,6 +1910,7 @@ collation_callback(void *context, int text1_length, const void *text1_data,
18891910{
18901911 PyGILState_STATE gilstate = PyGILState_Ensure ();
18911912
1913+ pysqlite_CallbackContext * ctx = NULL ;
18921914 PyObject * string1 = 0 ;
18931915 PyObject * string2 = 0 ;
18941916 PyObject * retval = NULL ;
@@ -1910,8 +1932,11 @@ collation_callback(void *context, int text1_length, const void *text1_data,
19101932 goto finally ;
19111933 }
19121934
1913- pysqlite_CallbackContext * ctx = pysqlite_CallbackContext_CAST (context );
1935+ ctx = pysqlite_CallbackContext_CAST (context );
19141936 assert (ctx != NULL );
1937+ // Hold a reference to 'ctx' to prevent concurrent mutations.
1938+ Py_INCREF (ctx );
1939+
19151940 PyObject * args [] = { NULL , string1 , string2 }; // Borrowed refs.
19161941 size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET ;
19171942 retval = PyObject_Vectorcall (ctx -> callable , args + 1 , nargsf , NULL );
@@ -1931,8 +1956,10 @@ collation_callback(void *context, int text1_length, const void *text1_data,
19311956 else if (longval < 0 )
19321957 result = -1 ;
19331958 }
1959+ Py_CLEAR (ctx );
19341960
19351961finally :
1962+ Py_XDECREF (ctx );
19361963 Py_XDECREF (string1 );
19371964 Py_XDECREF (string2 );
19381965 Py_XDECREF (retval );
0 commit comments