sql-relax-constraints-on-VirtualCursor-layout.patch 2.1 KB

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