From 8a57cf6b75b0a58fda5a7e28a65c521b3e2466e2 Mon Sep 17 00:00:00 2001 From: Gennaro Prota Date: Wed, 18 Nov 2026 17:56:29 +0100 Subject: [PATCH] Work around MSVC 14.52 converting to empty bases with the wrong offset MSVC 14.52 converts a pointer to an empty base which ends its derived class with an offset one less than earlier versions use, although the layout it reports is unchanged (https://developercommunity.visualstudio.com/t/C-object-layout-places-empty-non-first/11175160). The interface base of every archive is such a base. A program built with 14.52 and linked statically with the official 1.92.0 binaries, which were built with an earlier version, gets some of the inline archive functions from the library and some from its own object files, and the two disagree on where the archive is: for instance, constructing a `binary_oarchive` or a `binary_iarchive` crashes in Debug builds. The DLL isn't affected, because it doesn't export those functions, so a program always uses its own copies. We now give `interface_oarchive` and `interface_iarchive` a data member when `_MSC_VER` is defined, so that they are no longer empty and all versions convert with the same offset. I've checked every archive type, linking a library built with MSVC 14.44 into a program built with 14.52 and vice versa. Note that the fix needs the library and the program to have it, so it can't help with the 1.92.0 binaries, and that it makes every archive a few bytes larger on MSVC. There's no test, as reproducing the problem needs two compilers. Microsoft has fixed the bug for a later release, so we can drop the member once 14.52 is no longer in use. Closes #389. --- .../boost/archive/detail/interface_iarchive.hpp | 4 ++++ .../boost/archive/detail/interface_oarchive.hpp | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/include/boost/archive/detail/interface_iarchive.hpp b/include/boost/archive/detail/interface_iarchive.hpp index 18e5cb1cc..474823447 100644 --- a/include/boost/archive/detail/interface_iarchive.hpp +++ b/include/boost/archive/detail/interface_iarchive.hpp @@ -34,6 +34,10 @@ template class interface_iarchive { protected: + #if defined(_MSC_VER) + // See the same member of interface_oarchive. + char m_msvc_14_52_workaround; + #endif interface_iarchive() {} public: ///////////////////////////////////////////////////////// diff --git a/include/boost/archive/detail/interface_oarchive.hpp b/include/boost/archive/detail/interface_oarchive.hpp index 8a2017345..7cd2ee1cc 100644 --- a/include/boost/archive/detail/interface_oarchive.hpp +++ b/include/boost/archive/detail/interface_oarchive.hpp @@ -36,6 +36,22 @@ template class interface_oarchive { protected: + #if defined(_MSC_VER) + // MSVC 14.52, unlike earlier versions, converts a pointer to an empty + // base which ends its derived class with an offset one less than the + // layout says, so a static library and a program built by different + // versions disagree on where the archive is. This member keeps the + // base from being empty. Microsoft has fixed the bug for a later + // release, so the member can be removed once 14.52 is no longer in + // use; see + // + // and . + // + // Note that we test _MSC_VER, not BOOST_MSVC: clang-cl and the Intel + // compiler define it too, and as they use the same layout as MSVC and + // link with libraries built by it, they must agree on the member. + char m_msvc_14_52_workaround; + #endif interface_oarchive() {} public: /////////////////////////////////////////////////////////