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

38 cant use a static message buffer #39

Merged
merged 2 commits into from
Jun 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions include/dylib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,12 @@ class dylib {
FreeLibrary(lib);
}
static char *_get_error() noexcept {
constexpr size_t buf_size = 512;
auto error_code = GetLastError();
if (!error_code)
return nullptr;
static char msg[buf_size];
char msg[512];
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bug. You are now returning a pointer to a local variable which goes out of scope.

auto lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
const DWORD len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error_code, lang, msg, buf_size, nullptr);
const DWORD len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error_code, lang, msg, 512, nullptr);
if (len > 0)
return msg;
return nullptr;
Expand Down