Skip to content

Work around MSVC 14.52 converting to empty bases with the wrong offset - #390

Open
gennaroprota wants to merge 1 commit into
developfrom
work-around-msvc-14.52-converting-to-empty-bases-with-the-wrong-offset
Open

gennaroprota wants to merge 1 commit into
developfrom
work-around-msvc-14.52-converting-to-empty-bases-with-the-wrong-offset

Conversation

@gennaroprota

Copy link
Copy Markdown
Collaborator

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.

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.
class interface_oarchive
{
protected:
#if defined(_MSC_VER)

@jmairboeck jmairboeck Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initally wanted to add the following:

Suggested change
#if defined(_MSC_VER)
#if defined(_MSC_VER) && _MSC_VER == 1952

because Microsoft said they are already working on a fix, but for supporting linking different versions together, we can't be that specific I guess. But the following should work:

Suggested change
#if defined(_MSC_VER)
#if defined(_MSC_VER) && _MSC_VER >= 1900 && _MSC_VER < 2000

Other versions (older ones) are not ABI compatible anyway and can't be linked together with the affected version. Maybe the minimum version can even be increased to 1950.

The same change needs also be done in the other file.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would that change buy?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not adding the member unnecessarily for older MSVC versions (before 2015 in the _MSC_VER == 1900 case).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

binary_iarchive and binary_oarchive constructors crash in Visual Studio 2026 in Debug mode

2 participants