From 8699950b04343afe37e9dcdce87028ba0f56827b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Wed, 19 May 2010 01:27:23 +0000 Subject: [PATCH] Issue #6697: Check that _PyUnicode_AsString() result is not NULL in _sqlite Strip also some trailing spaces --- Modules/_sqlite/connection.c | 8 +++++--- Modules/_sqlite/cursor.c | 4 ++-- Modules/_sqlite/row.c | 2 ++ Modules/_sqlite/statement.c | 5 ++++- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index 88d6b40e3ed..184bdeef93a 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -3,7 +3,7 @@ * Copyright (C) 2004-2010 Gerhard Häring * * This file is part of pysqlite. - * + * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. @@ -495,7 +495,9 @@ void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val) } else if (PyFloat_Check(py_val)) { sqlite3_result_double(context, PyFloat_AsDouble(py_val)); } else if (PyUnicode_Check(py_val)) { - sqlite3_result_text(context, _PyUnicode_AsString(py_val), -1, SQLITE_TRANSIENT); + char *str = _PyUnicode_AsString(py_val); + if (str != NULL) + sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT); } else if (PyObject_CheckBuffer(py_val)) { if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) { PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer"); @@ -892,7 +894,7 @@ static int _progress_handler(void* user_arg) } /* abort query if error occurred */ - rc = 1; + rc = 1; } else { rc = (int)PyObject_IsTrue(ret); Py_DECREF(ret); diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index 543111261d9..97908a30939 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -368,7 +368,7 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self) } PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'", colname , val_str); - buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf)); + buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf)); if (!buf_bytes) { PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8"); } else { @@ -533,7 +533,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* } operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len); - if (operation == NULL) + if (operation_cstr == NULL) goto error; /* reset description and rowcount */ diff --git a/Modules/_sqlite/row.c b/Modules/_sqlite/row.c index 0aa78f4b5dc..54a89e11d98 100644 --- a/Modules/_sqlite/row.c +++ b/Modules/_sqlite/row.c @@ -83,6 +83,8 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx) return item; } else if (PyUnicode_Check(idx)) { key = _PyUnicode_AsString(idx); + if (key == NULL) + return NULL; nitems = PyTuple_Size(self->description); diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index 48e039bd405..c04029003cb 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -130,7 +130,10 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec break; case TYPE_UNICODE: string = _PyUnicode_AsString(parameter); - rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT); + if (string != NULL) + rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT); + else + rc = -1; break; case TYPE_BUFFER: if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {