sqlite3: Handle strings with embedded zeros correctly

Closes #13676.
This commit is contained in:
Petri Lehtinen 2012-02-01 22:18:19 +02:00
parent fc3ba6b8fc
commit 023fe334bb
5 changed files with 54 additions and 9 deletions

View file

@ -268,9 +268,9 @@ 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)
{
return PyUnicode_FromString(val_str);
return PyUnicode_FromStringAndSize(val_str, size);
}
/*
@ -355,10 +355,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) {
@ -383,11 +384,11 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
}
}
} else if (self->connection->text_factory == (PyObject*)&PyBytes_Type) {
converted = PyBytes_FromString(val_str);
converted = PyBytes_FromStringAndSize(val_str, nbytes);
} else if (self->connection->text_factory == (PyObject*)&PyByteArray_Type) {
converted = PyByteArray_FromStringAndSize(val_str, strlen(val_str));
converted = PyByteArray_FromStringAndSize(val_str, nbytes);
} else {
converted = PyObject_CallFunction(self->connection->text_factory, "y", val_str);
converted = PyObject_CallFunction(self->connection->text_factory, "y#", val_str, nbytes);
}
} else {
/* coltype == SQLITE_BLOB */

View file

@ -129,9 +129,9 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec
rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
break;
case TYPE_UNICODE:
string = _PyUnicode_AsString(parameter);
string = _PyUnicode_AsStringAndSize(parameter, &buflen);
if (string != NULL)
rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
rc = sqlite3_bind_text(self->st, pos, string, buflen, SQLITE_TRANSIENT);
else
rc = -1;
break;