-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fixed potential buffer overrun in Win32 OpenGL error handling #1246
Conversation
src/SFML/Window/Win32/WglContext.cpp
Outdated
String errMsg(ss.str()); | ||
|
||
PTCHAR buffer; | ||
FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errorCode, 0, reinterpret_cast<PTCHAR>(&buffer), 0, NULL); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is PTCHAR
the right type for the cast?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes and no. While you're supposed to pass a TCHAR**
to retrieve a system allocated buffer, the signature only accepts a TCHAR*
(i.e. PTCHAR
). Remember, we're in C land here, so no overloads. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buffer
is already a PTCHAR
, so the &
operator would make it a PTCHAR*
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you basically cast away the second layer of reference for the signature alone. You'll pass a PTCHAR*
, because it's a return value here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not cast it to a LPTSTR
then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually copied the snippet from SFML's Joystick handling. That would probably be an even "more correct" solution I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still has a serious bug:
The FORMAT_MESSAGE_IGNORE_INSERTS flag should be added to the FormatMessage call.
Be aware that buffer may be null.
(In the original code the above flag should be added, the buffer length set correctly and the return code checked for zero.)
Bump. Any alternative solution? |
String getErrorString(DWORD errorCode)
{
PTCHAR buffer;
if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorCode, 0, reinterpret_cast<LPTSTR>(&buffer), 256, NULL) != 0)
{
String errMsg(buffer);
LocalFree(buffer);
return errMsg;
}
std::ostringstream ss;
ss << "Error " << errorCode;
return String(ss.str());
} |
Any update on this one, @MarioLiebisch? Or do you want to take over, @binary1248? |
0e5f184
to
e12d28d
Compare
@binary1248 can you re-review this? I applied the suggested change. |
This fixes issue #1245.
e12d28d
to
0980e90
Compare
This fixes issue #1245.