summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2012-02-01 20:20:12 (GMT)
committerPetri Lehtinen <petri@digip.org>2012-02-01 20:20:13 (GMT)
commit0518f470b1bb99b0b8878224942ebf1e18d1e74f (patch)
treeb29a19f36e098881236b45dabc286f1799d08e88 /Modules
parent6ab9813605213dafaea23e2907d25467b6a52178 (diff)
downloadcpython-0518f470b1bb99b0b8878224942ebf1e18d1e74f.zip
cpython-0518f470b1bb99b0b8878224942ebf1e18d1e74f.tar.gz
cpython-0518f470b1bb99b0b8878224942ebf1e18d1e74f.tar.bz2
sqlite3: Handle strings with embedded zeros correctly
Closes #13676.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_sqlite/cursor.c16
-rw-r--r--Modules/_sqlite/statement.c8
2 files changed, 13 insertions, 11 deletions
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 94c3f40..028a800 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -268,16 +268,17 @@ PyObject* _pysqlite_build_column_name(const char* colname)
}
}
-PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
+PyObject* pysqlite_unicode_from_string(const char* val_str, Py_ssize_t size, int optimize)
{
const char* check;
+ Py_ssize_t pos;
int is_ascii = 0;
if (optimize) {
is_ascii = 1;
check = val_str;
- while (*check) {
+ for (pos = 0; pos < size; pos++) {
if (*check & 0x80) {
is_ascii = 0;
break;
@@ -288,9 +289,9 @@ PyObject* pysqlite_unicode_from_string(const char* val_str, int optimize)
}
if (is_ascii) {
- return PyString_FromString(val_str);
+ return PyString_FromStringAndSize(val_str, size);
} else {
- return PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
+ return PyUnicode_DecodeUTF8(val_str, size, NULL);
}
}
@@ -375,10 +376,11 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
converted = PyFloat_FromDouble(sqlite3_column_double(self->statement->st, i));
} else if (coltype == SQLITE_TEXT) {
val_str = (const char*)sqlite3_column_text(self->statement->st, i);
+ nbytes = sqlite3_column_bytes(self->statement->st, i);
if ((self->connection->text_factory == (PyObject*)&PyUnicode_Type)
|| (self->connection->text_factory == pysqlite_OptimizedUnicode)) {
- converted = pysqlite_unicode_from_string(val_str,
+ converted = pysqlite_unicode_from_string(val_str, nbytes,
self->connection->text_factory == pysqlite_OptimizedUnicode ? 1 : 0);
if (!converted) {
@@ -391,9 +393,9 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
PyErr_SetString(pysqlite_OperationalError, buf);
}
} else if (self->connection->text_factory == (PyObject*)&PyString_Type) {
- converted = PyString_FromString(val_str);
+ converted = PyString_FromStringAndSize(val_str, nbytes);
} else {
- converted = PyObject_CallFunction(self->connection->text_factory, "s", val_str);
+ converted = PyObject_CallFunction(self->connection->text_factory, "s#", val_str, nbytes);
}
} else {
/* coltype == SQLITE_BLOB */
diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c
index 9d08f2b..c777211 100644
--- a/Modules/_sqlite/statement.c
+++ b/Modules/_sqlite/statement.c
@@ -166,13 +166,13 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec
rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
break;
case TYPE_STRING:
- string = PyString_AS_STRING(parameter);
- rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
+ PyString_AsStringAndSize(parameter, &string, &buflen);
+ rc = sqlite3_bind_text(self->st, pos, string, buflen, SQLITE_TRANSIENT);
break;
case TYPE_UNICODE:
stringval = PyUnicode_AsUTF8String(parameter);
- string = PyString_AsString(stringval);
- rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
+ PyString_AsStringAndSize(stringval, &string, &buflen);
+ rc = sqlite3_bind_text(self->st, pos, string, buflen, SQLITE_TRANSIENT);
Py_DECREF(stringval);
break;
case TYPE_BUFFER: