From 01dd7cef45f4278ae0b97f3ccc4f23dd79dc2b76 Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Thu, 27 Apr 2017 13:16:16 -0700 Subject: [PATCH] fix portability issue with strerror_r() If _GNU_SOURCE is defined on linux, then strerror_r() is an alternate, non-POSIX version. Undefine _GNU_SOURCE when including in ConfigManager.cpp to get the POSIX version of strerror_r(), and initialize the error string buffer to "unknown error" so that the code does not crash whichever version of the library function is being used, or strerror_r() fails for some reason. --- src/common/ConfigManager.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/common/ConfigManager.cpp b/src/common/ConfigManager.cpp index 0154055a..dafedac7 100644 --- a/src/common/ConfigManager.cpp +++ b/src/common/ConfigManager.cpp @@ -1,3 +1,8 @@ +// necessary to get portable strerror_r +#undef _GNU_SOURCE +#include +#define _GNU_SOURCE 1 + #include "ConfigManager.h" extern "C" { #include "../common/iniparser.h" @@ -759,8 +764,8 @@ void SaveConfigFile() { FILE *f = fopen(configFile, "w"); if (f == NULL) { - char err_buf[4096]; - char* err_msg = strerror_r(errno, err_buf, 4096); + char err_msg[4096] = "unknown error"; + strerror_r(errno, err_msg, 4096); fprintf(stderr, "Configuration file '%s' could not be written to: %s\n", configFile, err_msg); return; }