Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MessageId::getDataAsString() crashed with MSVC debug config #108

Merged

Conversation

BewareMyPower
Copy link
Contributor

@BewareMyPower BewareMyPower commented Nov 8, 2022

Fixes #107

Motivation

The MessageId::getDataAsString() API returns a std::string to the application side. In most cases it's not an issue. However, when building Windows DLLs with LINK_STATIC=ON, the library will be built with /MTd or /MT option to link 3rd party dependencies statically. In this case, the DLL and the application have different C runtime libraries that allocate or deallocate memory. The returned std::string object is allocated inside the DLL, while it will be destroyed in the application for debug build because RVO is not applied. The destruction could crash because the application C runtime cannot find the heap address from the C runtime in DLL.

Modifications

For MSVC debug build, change the API to return a const reference to std::string. Then the original std::string object will be deallocated inside the DLL.

Fixes apache#107

### Motivation

The `MessageId::getDataAsString()` API returns a `std::string` to the
application side. In most cases it's not an issue. However, when
building Windows DLLs with `LINK_STATIC=ON`, the library will be
built with `/MTd` or `/MT` option to link 3rd party dependencies
statically. In this case, the DLL and the application have different C
runtime libraries that allocate or deallocate memory. The returned
`std::string` object is allocated inside the DLL, while it will be
destroyed in the application. The destruction could crash because the
application C runtime cannot find the heap address from the C runtime in
DLL.

### Modifications

For MSVC debug build, change the API to return a const reference to
`std::string`. Then the original `std::string` object will be
deallocated inside the DLL.
@BewareMyPower BewareMyPower added bug Something isn't working release/3.0 labels Nov 8, 2022
@BewareMyPower BewareMyPower self-assigned this Nov 8, 2022
@BewareMyPower
Copy link
Contributor Author

Though it changes the public API, but it's only for Windows debug artifacts, which should only be used for tests, not in production. For users of Windows debug libraries, returning a const reference won't affect the application code.

@BewareMyPower
Copy link
Contributor Author

@BewareMyPower BewareMyPower merged commit 1f7fdb8 into apache:main Nov 9, 2022
@BewareMyPower BewareMyPower deleted the bewaremypower/fix-win-debug branch November 9, 2022 04:43
BewareMyPower added a commit that referenced this pull request Nov 9, 2022
Fixes #107

### Motivation

The `MessageId::getDataAsString()` API returns a `std::string` to the
application side. In most cases it's not an issue. However, when
building Windows DLLs with `LINK_STATIC=ON`, the library will be
built with `/MTd` or `/MT` option to link 3rd party dependencies
statically. In this case, the DLL and the application have different C
runtime libraries that allocate or deallocate memory. The returned
`std::string` object is allocated inside the DLL, while it will be
destroyed in the application. The destruction could crash because the
application C runtime cannot find the heap address from the C runtime in
DLL.

### Modifications

For MSVC debug build, change the API to return a const reference to
`std::string`. Then the original `std::string` object will be
deallocated inside the DLL.

(cherry picked from commit 1f7fdb8)
@RobertIndie RobertIndie added this to the 3.1.0 milestone Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] Message::getDataAsString() will crash with Windows debug library
3 participants