Skip to content

Commit

Permalink
Remove trailing newline from error message in dlerror() on Windows
Browse files Browse the repository at this point in the history
1. Add FORMAT_MESSAGE_MAX_WIDTH_MASK flag to remove trailing \r\n from
   the error message returned by FormatMessageA().
2. Also, add FORMAT_MESSAGE_IGNORE_INSERTS to avoid potential problems
   with system messages that have argument placeholders.

Quote from FormatMessage docs on MSDN:

If this function is called without FORMAT_MESSAGE_IGNORE_INSERTS, the
Arguments parameter must contain enough parameters to satisfy all insertion
sequences in the message string, and they must be of the correct type.
Therefore, do not use untrusted or unknown message strings with inserts
enabled because they can contain more insertion sequences than Arguments
provides, or those that may be of the wrong type. In particular, it is
unsafe to take an arbitrary system error code returned from an API and use
FORMAT_MESSAGE_FROM_SYSTEM without FORMAT_MESSAGE_IGNORE_INSERTS.
  • Loading branch information
sryze authored and an3l committed Mar 8, 2021
1 parent 3754684 commit d317350
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/my_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,9 @@ typedef ulong myf; /* Type of MyFlags in my_funcs */
static inline char *dlerror(void)
{
static char win_errormsg[2048];
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_MAX_WIDTH_MASK,
0, GetLastError(), 0, win_errormsg, 2048, NULL);
return win_errormsg;
}
Expand Down

0 comments on commit d317350

Please sign in to comment.