Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8699950

Browse files
author
Victor Stinner
committed
Issue #6697: Check that _PyUnicode_AsString() result is not NULL in _sqlite
Strip also some trailing spaces
1 parent f6c5783 commit 8699950

4 files changed

Lines changed: 13 additions & 6 deletions

File tree

Modules/_sqlite/connection.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright (C) 2004-2010 Gerhard Häring <[email protected]>
44
*
55
* This file is part of pysqlite.
6-
*
6+
*
77
* This software is provided 'as-is', without any express or implied
88
* warranty. In no event will the authors be held liable for any damages
99
* arising from the use of this software.
@@ -495,7 +495,9 @@ void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
495495
} else if (PyFloat_Check(py_val)) {
496496
sqlite3_result_double(context, PyFloat_AsDouble(py_val));
497497
} else if (PyUnicode_Check(py_val)) {
498-
sqlite3_result_text(context, _PyUnicode_AsString(py_val), -1, SQLITE_TRANSIENT);
498+
char *str = _PyUnicode_AsString(py_val);
499+
if (str != NULL)
500+
sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
499501
} else if (PyObject_CheckBuffer(py_val)) {
500502
if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
501503
PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
@@ -892,7 +894,7 @@ static int _progress_handler(void* user_arg)
892894
}
893895

894896
/* abort query if error occurred */
895-
rc = 1;
897+
rc = 1;
896898
} else {
897899
rc = (int)PyObject_IsTrue(ret);
898900
Py_DECREF(ret);

Modules/_sqlite/cursor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ PyObject* _pysqlite_fetch_one_row(pysqlite_Cursor* self)
368368
}
369369
PyOS_snprintf(buf, sizeof(buf) - 1, "Could not decode to UTF-8 column '%s' with text '%s'",
370370
colname , val_str);
371-
buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
371+
buf_bytes = PyByteArray_FromStringAndSize(buf, strlen(buf));
372372
if (!buf_bytes) {
373373
PyErr_SetString(pysqlite_OperationalError, "Could not decode to UTF-8");
374374
} else {
@@ -533,7 +533,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
533533
}
534534

535535
operation_cstr = _PyUnicode_AsStringAndSize(operation, &operation_len);
536-
if (operation == NULL)
536+
if (operation_cstr == NULL)
537537
goto error;
538538

539539
/* reset description and rowcount */

Modules/_sqlite/row.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
8383
return item;
8484
} else if (PyUnicode_Check(idx)) {
8585
key = _PyUnicode_AsString(idx);
86+
if (key == NULL)
87+
return NULL;
8688

8789
nitems = PyTuple_Size(self->description);
8890

Modules/_sqlite/statement.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ int pysqlite_statement_bind_parameter(pysqlite_Statement* self, int pos, PyObjec
130130
break;
131131
case TYPE_UNICODE:
132132
string = _PyUnicode_AsString(parameter);
133-
rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
133+
if (string != NULL)
134+
rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
135+
else
136+
rc = -1;
134137
break;
135138
case TYPE_BUFFER:
136139
if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {

0 commit comments

Comments
 (0)