Debian Codegraph @

Modules/_sqlite/statement.c

raw
1 /* statement.c - the statement type
2 *
3 * Copyright (C) 2005-2010 Gerhard Häring <[email protected]>
4 *
5 * This file is part of pysqlite.
6 *
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24 #include "connection.h"
25 #include "statement.h"
26 #include "util.h"
27
28 #define _pysqlite_Statement_CAST(op) ((pysqlite_Statement *)(op))
29
30 /* prototypes */
31 static const char *lstrip_sql(const char *sql);
32
33 pysqlite_Statement *
34 pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
35 {
36 pysqlite_state *state = connection->state;
37 assert(PyUnicode_Check(sql));
38 Py_ssize_t size;
39 const char *sql_cstr = PyUnicode_AsUTF8AndSize(sql, &size);
40 if (sql_cstr == NULL) {
41 return NULL;
42 }
43
44 sqlite3 *db = connection->db;
45 int max_length = sqlite3_limit(db, SQLITE_LIMIT_SQL_LENGTH, -1);
46 if (size > max_length) {
47 PyErr_SetString(connection->DataError,
48 "query string is too large");
49 return NULL;
50 }
51 if (strlen(sql_cstr) != (size_t)size) {
52 PyErr_SetString(connection->ProgrammingError,
53 "the query contains a null character");
54 return NULL;
55 }
56
57 sqlite3_stmt *stmt;
58 const char *tail;
59 int rc;
60 Py_BEGIN_ALLOW_THREADS
61 rc = sqlite3_prepare_v2(db, sql_cstr, (int)size + 1, &stmt, &tail);
62 Py_END_ALLOW_THREADS
63
64 if (rc != SQLITE_OK) {
65 set_error_from_db(state, db);
66 return NULL;
67 }
68
69 if (lstrip_sql(tail) != NULL) {
70 PyErr_SetString(connection->ProgrammingError,
71 "You can only execute one statement at a time.");
72 goto error;
73 }
74
75 /* Determine if the statement is a DML statement.
76 SELECT is the only exception. See #9924. */
77 int is_dml = 0;
78 const char *p = lstrip_sql(sql_cstr);
79 if (p != NULL) {
80 is_dml = (PyOS_strnicmp(p, "insert", 6) == 0)
81 || (PyOS_strnicmp(p, "update", 6) == 0)
82 || (PyOS_strnicmp(p, "delete", 6) == 0)
83 || (PyOS_strnicmp(p, "replace", 7) == 0);
84 }
85
86 pysqlite_Statement *self = PyObject_GC_New(pysqlite_Statement,
87 state->StatementType);
88 if (self == NULL) {
89 goto error;
90 }
91
92 self->st = stmt;
93 self->is_dml = is_dml;
94
95 PyObject_GC_Track(self);
96 return self;
97
98 error:
99 (void)sqlite3_finalize(stmt);
100 return NULL;
101 }
102
103 static void
104 stmt_dealloc(PyObject *op)
105 {
106 pysqlite_Statement *self = _pysqlite_Statement_CAST(op);
107 PyTypeObject *tp = Py_TYPE(self);
108 PyObject_GC_UnTrack(op);
109 if (self->st) {
110 Py_BEGIN_ALLOW_THREADS
111 sqlite3_finalize(self->st);
112 Py_END_ALLOW_THREADS
113 self->st = 0;
114 }
115 tp->tp_free(self);
116 Py_DECREF(tp);
117 }
118
119 static int
120 stmt_traverse(PyObject *self, visitproc visit, void *arg)
121 {
122 Py_VISIT(Py_TYPE(self));
123 return 0;
124 }
125
126 /*
127 * Strip leading whitespace and comments from incoming SQL (null terminated C
128 * string) and return a pointer to the first non-whitespace, non-comment
129 * character.
130 *
131 * This is used to check if somebody tries to execute more than one SQL query
132 * with one execute()/executemany() command, which the DB-API don't allow.
133 *
134 * It is also used to harden DML query detection.
135 */
136 static inline const char *
137 lstrip_sql(const char *sql)
138 {
139 // This loop is borrowed from the SQLite source code.
140 for (const char *pos = sql; *pos; pos++) {
141 switch (*pos) {
142 case ' ':
143 case '\t':
144 case '\f':
145 case '\n':
146 case '\r':
147 // Skip whitespace.
148 break;
149 case '-':
150 // Skip line comments.
151 if (pos[1] == '-') {
152 pos += 2;
153 while (pos[0] && pos[0] != '\n') {
154 pos++;
155 }
156 if (pos[0] == '\0') {
157 return NULL;
158 }
159 continue;
160 }
161 return pos;
162 case '/':
163 // Skip C style comments.
164 if (pos[1] == '*') {
165 pos += 2;
166 while (pos[0] && (pos[0] != '*' || pos[1] != '/')) {
167 pos++;
168 }
169 if (pos[0] == '\0') {
170 return NULL;
171 }
172 pos++;
173 continue;
174 }
175 return pos;
176 default:
177 return pos;
178 }
179 }
180
181 return NULL;
182 }
183
184 static PyType_Slot stmt_slots[] = {
185 {Py_tp_dealloc, stmt_dealloc},
186 {Py_tp_traverse, stmt_traverse},
187 {0, NULL},
188 };
189
190 static PyType_Spec stmt_spec = {
191 .name = MODULE_NAME ".Statement",
192 .basicsize = sizeof(pysqlite_Statement),
193 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
194 Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION),
195 .slots = stmt_slots,
196 };
197
198 int
199 pysqlite_statement_setup_types(PyObject *module)
200 {
201 PyObject *type = PyType_FromModuleAndSpec(module, &stmt_spec, NULL);
202 if (type == NULL) {
203 return -1;
204 }
205 pysqlite_state *state = pysqlite_get_state(module);
206 state->StatementType = (PyTypeObject *)type;
207 return 0;
208 }