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

Skip to content

Commit 4fe85ab

Browse files
committed
sqlite3: Fix 64-bit integer handling in user functions on 32-bit architectures
Closes #8033.
1 parent 29877e8 commit 4fe85ab

4 files changed

Lines changed: 24 additions & 6 deletions

File tree

Lib/sqlite3/test/userfunctions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def func_returnnull():
3737
return None
3838
def func_returnblob():
3939
return b"blob"
40+
def func_returnlonglong():
41+
return 1<<31
4042
def func_raiseexception():
4143
5/0
4244

@@ -50,6 +52,8 @@ def func_isnone(v):
5052
return type(v) is type(None)
5153
def func_isblob(v):
5254
return isinstance(v, (bytes, memoryview))
55+
def func_islonglong(v):
56+
return isinstance(v, int) and v >= 1<<31
5357

5458
class AggrNoStep:
5559
def __init__(self):
@@ -127,13 +131,15 @@ def setUp(self):
127131
self.con.create_function("returnfloat", 0, func_returnfloat)
128132
self.con.create_function("returnnull", 0, func_returnnull)
129133
self.con.create_function("returnblob", 0, func_returnblob)
134+
self.con.create_function("returnlonglong", 0, func_returnlonglong)
130135
self.con.create_function("raiseexception", 0, func_raiseexception)
131136

132137
self.con.create_function("isstring", 1, func_isstring)
133138
self.con.create_function("isint", 1, func_isint)
134139
self.con.create_function("isfloat", 1, func_isfloat)
135140
self.con.create_function("isnone", 1, func_isnone)
136141
self.con.create_function("isblob", 1, func_isblob)
142+
self.con.create_function("islonglong", 1, func_islonglong)
137143

138144
def tearDown(self):
139145
self.con.close()
@@ -200,6 +206,12 @@ def CheckFuncReturnBlob(self):
200206
self.assertEqual(type(val), bytes)
201207
self.assertEqual(val, b"blob")
202208

209+
def CheckFuncReturnLongLong(self):
210+
cur = self.con.cursor()
211+
cur.execute("select returnlonglong()")
212+
val = cur.fetchone()[0]
213+
self.assertEqual(val, 1<<31)
214+
203215
def CheckFuncException(self):
204216
cur = self.con.cursor()
205217
try:
@@ -239,6 +251,12 @@ def CheckParamBlob(self):
239251
val = cur.fetchone()[0]
240252
self.assertEqual(val, 1)
241253

254+
def CheckParamLongLong(self):
255+
cur = self.con.cursor()
256+
cur.execute("select islonglong(?)", (1<<42,))
257+
val = cur.fetchone()[0]
258+
self.assertEqual(val, 1)
259+
242260
class AggregateTests(unittest.TestCase):
243261
def setUp(self):
244262
self.con = sqlite.connect(":memory:")

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ Arnaud Delobelle
222222
Erik Demaine
223223
John Dennis
224224
Roger Dev
225+
Philippe Devalkeneer
225226
Raghuram Devarakonda
226227
Caleb Deveraux
227228
Catherine Devlin

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ Core and Builtins
121121
Library
122122
-------
123123

124+
- Issue #8033: sqlite3: Fix 64-bit integer handling in user functions
125+
on 32-bit architectures. Initial patch by Philippe Devalkeneer.
126+
124127
- HTMLParser is now able to handle slashes in the start tag.
125128

126129
- Issue #14001: CVE-2012-0845: xmlrpc: Fix an endless loop in

Modules/_sqlite/connection.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args
484484

485485
void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
486486
{
487-
long longval;
488487
const char* buffer;
489488
Py_ssize_t buflen;
490489

@@ -493,8 +492,7 @@ void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
493492
} else if (py_val == Py_None) {
494493
sqlite3_result_null(context);
495494
} else if (PyLong_Check(py_val)) {
496-
longval = PyLong_AsLong(py_val);
497-
sqlite3_result_int64(context, (PY_LONG_LONG)longval);
495+
sqlite3_result_int64(context, PyLong_AsLongLong(py_val));
498496
} else if (PyFloat_Check(py_val)) {
499497
sqlite3_result_double(context, PyFloat_AsDouble(py_val));
500498
} else if (PyUnicode_Check(py_val)) {
@@ -519,7 +517,6 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
519517
sqlite3_value* cur_value;
520518
PyObject* cur_py_value;
521519
const char* val_str;
522-
PY_LONG_LONG val_int;
523520
Py_ssize_t buflen;
524521

525522
args = PyTuple_New(argc);
@@ -531,8 +528,7 @@ PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_
531528
cur_value = argv[i];
532529
switch (sqlite3_value_type(argv[i])) {
533530
case SQLITE_INTEGER:
534-
val_int = sqlite3_value_int64(cur_value);
535-
cur_py_value = PyLong_FromLong((long)val_int);
531+
cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
536532
break;
537533
case SQLITE_FLOAT:
538534
cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));

0 commit comments

Comments
 (0)