fix epicsLoadError()

FORMAT_MESSAGE_IGNORE_INSERTS as no va_list is provided.

Handle possibility of n=0 if unable to lookup error.
This commit is contained in:
Michael Davidsaver
2020-04-18 07:35:21 -07:00
parent 6e41f2911b
commit aa7c2a647c

View File

@@ -51,19 +51,27 @@ epicsShareFunc const char *epicsLoadError(void)
DWORD n;
n = FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM,
FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
epicsLoadErrorCode,
0,
buffer,
sizeof(buffer)-1, NULL );
/* n - number of chars stored excluding nil.
*
* Some messages include a trailing newline, which we strip.
*/
for(; n>=1 && (buffer[n-1]=='\n' || buffer[n-1]=='\r'); n--)
buffer[n-1] = '\0';
if(n) {
/* n - number of chars stored excluding nil.
*
* Some messages include a trailing newline, which we strip.
*/
for(; n>=1 && (buffer[n-1]=='\n' || buffer[n-1]=='\r'); n--)
buffer[n-1] = '\0';
} else {
epicsSnprintf(buffer, sizeof(buffer),
"Unable to format WIN32 error code %lu",
(unsigned long)epicsLoadErrorCode);
buffer[sizeof(buffer)-1] = '\0';
}
return buffer;
}