@@ -696,6 +696,7 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
696
696
const char * script_cstr ;
697
697
sqlite3_stmt * statement ;
698
698
int rc ;
699
+ Py_ssize_t sql_len ;
699
700
PyObject * result ;
700
701
701
702
if (!check_cursor (self )) {
@@ -705,10 +706,17 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
705
706
self -> reset = 0 ;
706
707
707
708
if (PyUnicode_Check (script_obj )) {
708
- script_cstr = PyUnicode_AsUTF8 (script_obj );
709
+ script_cstr = PyUnicode_AsUTF8AndSize (script_obj , & sql_len );
709
710
if (!script_cstr ) {
710
711
return NULL ;
711
712
}
713
+
714
+ int max_length = sqlite3_limit (self -> connection -> db ,
715
+ SQLITE_LIMIT_LENGTH , -1 );
716
+ if (sql_len >= max_length ) {
717
+ PyErr_SetString (pysqlite_DataError , "query string is too large" );
718
+ return NULL ;
719
+ }
712
720
} else {
713
721
PyErr_SetString (PyExc_ValueError , "script argument must be unicode." );
714
722
return NULL ;
@@ -722,12 +730,14 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
722
730
Py_DECREF (result );
723
731
724
732
while (1 ) {
733
+ const char * tail ;
734
+
725
735
Py_BEGIN_ALLOW_THREADS
726
736
rc = sqlite3_prepare_v2 (self -> connection -> db ,
727
737
script_cstr ,
728
- - 1 ,
738
+ ( int ) sql_len + 1 ,
729
739
& statement ,
730
- & script_cstr );
740
+ & tail );
731
741
Py_END_ALLOW_THREADS
732
742
if (rc != SQLITE_OK ) {
733
743
_pysqlite_seterror (self -> connection -> db );
@@ -755,9 +765,11 @@ pysqlite_cursor_executescript(pysqlite_Cursor *self, PyObject *script_obj)
755
765
goto error ;
756
766
}
757
767
758
- if (* script_cstr == (char )0 ) {
768
+ if (* tail == (char )0 ) {
759
769
break ;
760
770
}
771
+ sql_len -= (tail - script_cstr );
772
+ script_cstr = tail ;
761
773
}
762
774
763
775
error :
0 commit comments