apolukhin / Boost.DLL

Library for comfortable work with DLL and DSO
https://boost.org/libs/dll
110 stars 69 forks source link

path_from_handle implementation is incorrect (redux) #63

Open relapids opened 1 year ago

relapids commented 1 year ago

Previous issue: https://github.com/apolukhin/Boost.DLL/issues/57 PR which was intending to fix that issue: https://github.com/apolukhin/Boost.DLL/pull/59

@apolukhin I believe this fix is incorrect. Per the documentation from GetModuleFileNameW (emphasis mine):

If the function succeeds, the return value is the length of the string that is copied to the buffer, in characters, not including the terminating null character. If the buffer is too small to hold the module name, the string is truncated to nSize characters including the terminating null character, the function returns nSize, and the function sets the last error to ERROR_INSUFFICIENT_BUFFER.

The important thing to note here is that the returned size can be non-zero even in failure cases. This appears to not be handled any more, which is the entire purpose of that loop which grows the buffer on each iteration afaict.

Likely the original structure of the code was correct, but what it meant to do instead of calling GetLastError was this: SetLastError(0);