Skip to content

Commit

Permalink
Merge pull request #91 from michalc/feat/better-error-with-short-sqli…
Browse files Browse the repository at this point in the history
…te-file

feat: raise slightly better error when the object in S3 is too short
  • Loading branch information
michalc committed Mar 15, 2024
2 parents 58d06b4 + 19da90c commit 7fe8861
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion sqlite_s3_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def sqlite_s3_query_multi(url, get_credentials=lambda now: (
SQLITE_NOTFOUND = 12
SQLITE_ROW = 100
SQLITE_DONE = 101
SQLITE_IOERR_SHORT_READ = 522
SQLITE_TRANSIENT = c_void_p(-1)
SQLITE_OPEN_READONLY = 0x00000001
SQLITE_OPEN_NOMUTEX = 0x00008000
Expand Down Expand Up @@ -208,7 +209,13 @@ def x_read(p_file, p_out, i_amt, i_ofst):
set_pending_exception(exception)
return SQLITE_IOERR

if offset != i_amt:
if offset < i_amt:
# The SQLite docs strongly suggest to fill unused with zeroes
remainder = i_amt - offset
memmove(p_out + offset, b'\0' * remainder, remainder)
return SQLITE_IOERR_SHORT_READ

if offset > i_amt:
return SQLITE_IOERR

return SQLITE_OK
Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def test_short_db_header(self):
'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
None,
), get_libsqlite3=get_libsqlite3) as query:
with self.assertRaisesRegex(SQLiteError, 'disk I/O error'):
with self.assertRaisesRegex(SQLiteError, 'not a database'):
query("SELECT * FROM non_table").__enter__()

def test_bad_db_header(self):
Expand All @@ -471,7 +471,7 @@ def test_bad_db_header(self):
'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
None,
), get_libsqlite3=get_libsqlite3) as query:
with self.assertRaisesRegex(SQLiteError, 'disk I/O error'):
with self.assertRaisesRegex(SQLiteError, 'not a database'):
query("SELECT * FROM non_table").__enter__()

def test_bad_db_first_page(self):
Expand Down

0 comments on commit 7fe8861

Please sign in to comment.