Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Unified Diff: third_party/sqlite/src/src/recover.c

Issue 343423004: [sql] Prevent nChildren overrun decoding interior pages in recover.c. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.c ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/sqlite/src/src/recover.c
diff --git a/third_party/sqlite/src/src/recover.c b/third_party/sqlite/src/src/recover.c
index e67ef5409fe85477fee581668a48f433398a17ba..097c92019ea238a07106f5db3af87ce12755e339 100644
--- a/third_party/sqlite/src/src/recover.c
+++ b/third_party/sqlite/src/src/recover.c
@@ -653,12 +653,28 @@ static void interiorCursorSetPage(RecoverInteriorCursor *pCursor,
pCursor->iChild = 0;
/* A child for each cell, plus one in the header. */
- /* TODO(shess): Sanity-check the count? Page header plus per-cell
- * cost of 16-bit offset, 32-bit page number, and one varint
- * (minimum 1 byte).
- */
pCursor->nChildren = decodeUnsigned16(PageHeader(pPage) +
kiPageCellCountOffset) + 1;
+
+ /* Each child requires a 16-bit offset from an array after the header,
+ * and each child contains a 32-bit page number and at least a varint
+ * (min size of one byte). The final child page is in the header. So
+ * the maximum value for nChildren is:
+ * (nPageSize - kiPageInteriorHeaderBytes) /
+ * (sizeof(uint16) + sizeof(uint32) + 1) + 1
+ */
+ /* TODO(shess): This count is very unlikely to be corrupted in
+ * isolation, so seeing this could signal to skip the page. OTOH, I
+ * can't offhand think of how to get here unless this or the page-type
+ * byte is corrupted. Could be an overflow page, but it would require
+ * a very large database.
+ */
+ const unsigned knMinCellLength = 2 + 4 + 1;
+ unsigned nMaxChildren =
+ (pCursor->nPageSize - kiPageInteriorHeaderBytes) / knMinCellLength + 1;
+ if (pCursor->nChildren > nMaxChildren) {
+ pCursor->nChildren = nMaxChildren;
+ }
}
static int interiorCursorCreate(RecoverInteriorCursor *pParent,
« no previous file with comments | « third_party/sqlite/amalgamation/sqlite3.c ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698