Skip to content

Commit 783fe3d

Browse files
committed
Fixed error handling for MSVC, which very weird:
MSVC rejects strerror, suggest to use "secure" strerror_s, but then does not supply strerrorlen_s GCC does not provide strerror_s, looks like strerror is good enough there.
1 parent 763f308 commit 783fe3d

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

source/matplot/backend/gnuplot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ namespace matplot::backend {
8080

8181
// Check if everything is OK
8282
if (perr != 0 || !pipe_.opened()) {
83-
std::cerr << "Opening the gnuplot pipe_ failed:\n";
83+
std::cerr << "Opening the gnuplot failed: ";
8484
std::cerr << pipe_.error() << std::endl;
8585
std::cerr << "Please install gnuplot 5.2.6+: http://www.gnuplot.info"
8686
<< std::endl;

source/matplot/util/popen.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ int common_pipe::open(const std::string& cmd, char mode)
2828
if (!SetHandleInformation(hChildStdinWr, HANDLE_FLAG_INHERIT, 0)) {
2929
auto err = GetLastError();
3030
CloseHandle(hChildStdinRd);
31-
return err;
31+
return report(err, "SetHandleInformation(hChildStdinWr)");
3232
}
3333

3434
// Create a pipe for the child process's output
3535
if (!CreatePipe(&hStdin, &hStdout, &saAttr, 0)) {
3636
auto err = GetLastError();
3737
CloseHandle(hChildStdinRd);
3838
CloseHandle(hChildStdinWr);
39-
return err;
39+
return report(err, "CreatePipe");
4040
}
4141

4242
// Ensure the read handle to the output pipe is not inherited by child
@@ -46,7 +46,7 @@ int common_pipe::open(const std::string& cmd, char mode)
4646
CloseHandle(hChildStdinRd);
4747
CloseHandle(hChildStdinWr);
4848
CloseHandle(hStdin);
49-
return err;
49+
return report(err, "SetHandleInformation(hStdout)");
5050
}
5151

5252
// Configure STARTUPINFO structure for the new process
@@ -67,7 +67,7 @@ int common_pipe::open(const std::string& cmd, char mode)
6767
CloseHandle(hChildStdinWr);
6868
CloseHandle(hStdin);
6969
CloseHandle(hStdout);
70-
return err;
70+
return report(err, "CreateProcess");
7171
}
7272
hProcess = pi.hProcess;
7373
hThread = pi.hThread;
@@ -88,7 +88,7 @@ int common_pipe::open(const std::string& cmd, char mode)
8888
CloseHandle(hStdin);
8989
CloseHandle(pi.hProcess);
9090
CloseHandle(pi.hThread);
91-
return err;
91+
return report(err, "_fdopen(_open_osfhandle)");
9292
}
9393
return 0;
9494
}
@@ -189,12 +189,12 @@ int common_pipe::close(int *exit_code)
189189

190190
inline int common_pipe::report(int code, const std::string& what)
191191
{
192-
#if defined(_MSC_VER)
193-
const auto len = strerrorlen_s(code);
194-
error_.assign(len, ' ');
195-
strerror_s(error_.c_str(), len+1, code);
192+
#ifdef __STDC_LIB_EXT1__
193+
char buffer[128]{}; // MSVC has no strerrorlen_s
194+
strerror_s(buffer, 128, code); // MSVC rejects strerror
195+
error_ = what + ": " + buffer;
196196
#else
197-
error_ = std::strerror(code);
197+
error_ = what + ": " + std::strerror(code); // GCC fixed strerror and has no strerror_s
198198
#endif
199199
if (exceptions_)
200200
throw std::system_error{code, std::generic_category(), what};

0 commit comments

Comments
 (0)