Skip to content

Commit 8d6ebf5

Browse files
rhermanklinkdpgeorge
authored andcommitted
unix-ffi/sqlite3: Fix bytes to accommodate for different pointer sizes.
Currently, the bytes object used to store the sqlite3 database pointer is always 4 bytes, which causes segfaults on 64 bit platforms with 8 byte pointers. To address this, the size is now dynamically determined using the uctypes modules pointer size. Signed-off-by: Robert Klink <[email protected]>
1 parent 910af18 commit 8d6ebf5

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

unix-ffi/sqlite3/sqlite3.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import ffilib
3+
import uctypes
34

45

56
sq3 = ffilib.open("libsqlite3")
@@ -61,6 +62,10 @@ def check_error(db, s):
6162
raise Error(s, sqlite3_errmsg(db))
6263

6364

65+
def get_ptr_size():
66+
return uctypes.sizeof({"ptr": (0 | uctypes.PTR, uctypes.PTR)})
67+
68+
6469
class Connections:
6570
def __init__(self, h):
6671
self.h = h
@@ -83,13 +88,13 @@ def execute(self, sql, params=None):
8388
params = [quote(v) for v in params]
8489
sql = sql % tuple(params)
8590
print(sql)
86-
b = bytearray(4)
87-
s = sqlite3_prepare(self.h, sql, -1, b, None)
88-
check_error(self.h, s)
89-
self.stmnt = int.from_bytes(b, sys.byteorder)
90-
# print("stmnt", self.stmnt)
91+
92+
stmnt_ptr = bytes(get_ptr_size())
93+
res = sqlite3_prepare(self.h, sql, -1, stmnt_ptr, None)
94+
check_error(self.h, res)
95+
self.stmnt = int.from_bytes(stmnt_ptr, sys.byteorder)
9196
self.num_cols = sqlite3_column_count(self.stmnt)
92-
# print("num_cols", self.num_cols)
97+
9398
# If it's not select, actually execute it here
9499
# num_cols == 0 for statements which don't return data (=> modify it)
95100
if not self.num_cols:
@@ -127,10 +132,9 @@ def fetchone(self):
127132

128133

129134
def connect(fname):
130-
b = bytearray(4)
131-
sqlite3_open(fname, b)
132-
h = int.from_bytes(b, sys.byteorder)
133-
return Connections(h)
135+
sqlite_ptr = bytes(get_ptr_size())
136+
sqlite3_open(fname, sqlite_ptr)
137+
return Connections(int.from_bytes(sqlite_ptr, sys.byteorder))
134138

135139

136140
def quote(val):

0 commit comments

Comments
 (0)