1 From 94ffefa81d00466fabcf1050080c95fa4b294dda Mon Sep 17 00:00:00 2001
2 From: Elly Fong-Jones <ellyjones@chromium.org>
3 Date: Thu, 2 Mar 2023 00:15:11 +0000
4 Subject: [PATCH 1/9] sql: relax constraints on VirtualCursor layout
6 VirtualCursor::FromSqliteCursor required that VirtualCursor had a
7 standard layout, but in fact VirtualCursor shouldn't have a standard
8 layout, and the fact that it does with libc++ is a deviation from the
9 C++ standard. This change:
11 1. Relaxes the requirement that VirtualCursor has a standard layout, and
12 2. Relaxes the requirement that the sqlite_cursor_ field has to be at
15 by use of offsetof() and pointer subtraction. This change both improves
16 standards compliance and makes this code build with libstdc++.
19 Change-Id: I9c47abd9197b187da0360ca5619ccf7dadab4f33
20 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4292313
21 Reviewed-by: Austin Sullivan <asully@chromium.org>
22 Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
23 Cr-Commit-Position: refs/heads/main@{#1111925}
25 sql/recover_module/cursor.h | 10 ++++------
26 1 file changed, 4 insertions(+), 6 deletions(-)
28 diff --git a/sql/recover_module/cursor.h b/sql/recover_module/cursor.h
29 index 1970bdca8c6f1..4cb0655700977 100644
30 --- a/sql/recover_module/cursor.h
31 +++ b/sql/recover_module/cursor.h
32 @@ -63,12 +63,10 @@ class VirtualCursor {
33 // |sqlite_cursor| must have been returned by VirtualTable::SqliteCursor().
34 static inline VirtualCursor* FromSqliteCursor(
35 sqlite3_vtab_cursor* sqlite_cursor) {
36 - static_assert(std::is_standard_layout<VirtualCursor>::value,
37 - "needed for the reinterpret_cast below");
38 - static_assert(offsetof(VirtualCursor, sqlite_cursor_) == 0,
39 - "sqlite_cursor_ must be the first member of the class");
40 - VirtualCursor* result = reinterpret_cast<VirtualCursor*>(sqlite_cursor);
41 - DCHECK_EQ(sqlite_cursor, &result->sqlite_cursor_);
42 + VirtualCursor* result = reinterpret_cast<VirtualCursor*>(
43 + (reinterpret_cast<char*>(sqlite_cursor) -
44 + offsetof(VirtualCursor, sqlite_cursor_)));
45 + CHECK_EQ(sqlite_cursor, &result->sqlite_cursor_);