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

path_from_handle using GetModuleFileNameW in wrong way #16

Closed
Agile86 opened this issue Feb 21, 2018 · 3 comments
Closed

path_from_handle using GetModuleFileNameW in wrong way #16

Agile86 opened this issue Feb 21, 2018 · 3 comments

Comments

@Agile86
Copy link

Agile86 commented Feb 21, 2018

boost\dll\detail\windows\path_from_handle.hpp

        boost::detail::winapi::GetModuleFileNameW(handle, path_hldr, 
            DEFAULT_PATH_SIZE_);
        ec = last_error_code();
        if (!ec) {
            return boost::filesystem::path(path_hldr);
        }

The problem here is that code doesn't analyze return code. If GetLastError() was dirty before a call this code will fail even if GetModuleFileNameW() will success. Yes, on success, GetModuleFileNameW() doesn't reset last error to ERROR_SUCCESS.

@apolukhin
Copy link
Member

Many thanks for reporting the issue!

@Oberon00
Copy link

I don't think the fix in f04ceff is correct, as calling GetLastError does not reset the error code. Instead, as @Agile86 said, you should check the return value of GetModuleFileNameW and call GetLastError only if the return value indicates an error (i.e. if the return value equals zero or DEFAULT_PATH_SIZE_ in this case).

I think, even setting the error code to zero beforehand is not guaranteed to work as the function might internally cause and handle an error reported via GetLastError without setting the error code to zero afterwards.

@muggenhor
Copy link

The chosen solution definitely doesn't work:

#include <assert.h>
#include <windows.h>

int main()
{
  SetLastError(42u);
  GetLastError(); // <-- we depend on this resetting the error code (it doesn't)
  assert(GetLastError() == ERROR_SUCCESS);
  return 0;
}

Leads to:

Assertion failed!

Program: Z:\home\vanschig\git\novaui\thirdparty\test.exe
File: test.cpp, Line 8

Expression: GetLastError() == ERROR_SUCCESS

abnormal program termination

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

No branches or pull requests

4 participants