From aa7c2a647c054fb6417da635efcf1f3a724b5eba Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Sat, 18 Apr 2020 07:35:21 -0700 Subject: [PATCH] fix epicsLoadError() FORMAT_MESSAGE_IGNORE_INSERTS as no va_list is provided. Handle possibility of n=0 if unable to lookup error. --- .../libcom/src/osi/os/WIN32/osdFindSymbol.c | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/modules/libcom/src/osi/os/WIN32/osdFindSymbol.c b/modules/libcom/src/osi/os/WIN32/osdFindSymbol.c index 2753f0959..6c1fb401c 100644 --- a/modules/libcom/src/osi/os/WIN32/osdFindSymbol.c +++ b/modules/libcom/src/osi/os/WIN32/osdFindSymbol.c @@ -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; }