Skip to content

Commit 1e58b09

Browse files
authored
Fix error when reading first page from WAL commit in SQLite3 (#15)
1 parent cba61a9 commit 1e58b09

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

‎dissect/database/sqlite3/sqlite3.py‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,27 @@ def raw_page(self, num: int) -> bytes:
202202
if (num < 1 or num > self.header.page_count) and self.header.page_count > 0:
203203
raise InvalidPageNumber("Page number exceeds boundaries")
204204

205-
# If a specific WAL checkpoint was provided, use it instead of the on-disk page.
205+
data = None
206206
if self.checkpoint is not None and (frame := self.checkpoint.get(num)):
207-
return frame.data
207+
# If a specific WAL checkpoint was provided, use it instead of the on-disk page.
208+
data = frame.data
208209

209-
# Check if the latest valid instance of the page is committed (either the frame itself
210-
# is the commit frame or it is included in a commit's frames). If so, return that frame's data.
211-
if self.wal:
210+
elif self.wal:
211+
# Check if the latest valid instance of the page is committed (either the frame itself
212+
# is the commit frame or it is included in a commit's frames). If so, return that frame's data.
212213
for commit in reversed(self.wal.commits):
213214
if (frame := commit.get(num)) and frame.valid:
214-
return frame.data
215+
data = frame.data
216+
break
217+
218+
if data is not None:
219+
if num == 1:
220+
# Page 1 has a database header that needs to be stripped
221+
return data[len(c_sqlite3.header) :]
222+
return data
215223

216224
# Else we read the page from the database file.
217-
if num == 1: # Page 1 is root
225+
if num == 1: # Page 1 is root, skip header
218226
self.fh.seek(len(c_sqlite3.header))
219227
else:
220228
self.fh.seek((num - 1) * self.page_size)

0 commit comments

Comments
 (0)