Skip to content

Commit abe0a57

Browse files
author
Yi Wu
committed
fixes on Windows
1 parent b5df8d7 commit abe0a57

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

flang/runtime/execute.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ template <int KIND> struct FitsInIntegerKind {
100100

101101
// If a condition occurs that would assign a nonzero value to CMDSTAT but
102102
// the CMDSTAT variable is not present, error termination is initiated.
103-
int TerminationCheck(int status, const Descriptor *cmdstat, const Descriptor *cmdmsg,
104-
Terminator &terminator) {
103+
int TerminationCheck(int status, const Descriptor *cmdstat,
104+
const Descriptor *cmdmsg, Terminator &terminator) {
105105
if (status == -1) {
106106
if (!cmdstat) {
107107
terminator.Crash("Execution error with system status code: %d", status);
@@ -191,8 +191,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
191191
if (wait) {
192192
// either wait is not specified or wait is true: synchronous mode
193193
int status{std::system(newCmd)};
194-
int exitStatusVal{
195-
TerminationCheck(status, cmdstat, cmdmsg, terminator)};
194+
int exitStatusVal{TerminationCheck(status, cmdstat, cmdmsg, terminator)};
196195
CheckAndStoreIntToDescriptor(exitstat, exitStatusVal, terminator);
197196
} else {
198197
// Asynchronous mode
@@ -204,26 +203,27 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
204203
ZeroMemory(&pi, sizeof(pi));
205204

206205
// append "cmd.exe /c " to the beginning of command
207-
const char *cmd{command->OffsetElement()};
208206
const char *prefix{"cmd.exe /c "};
209-
char *newCmd{(char *)malloc(std::strlen(prefix) + std::strlen(cmd) + 1)};
207+
char *newCmdWin{
208+
(char *)malloc(std::strlen(prefix) + std::strlen(newCmd) + 1)};
210209
if (newCmd != NULL) {
211-
std::strcpy(newCmd, prefix);
212-
std::strcat(newCmd, cmd);
210+
std::strcpy(newCmdWin, prefix);
211+
std::strcat(newCmdWin, newCmd);
213212
} else {
214213
terminator.Crash("Memory allocation failed for newCmd");
215214
}
216215

217216
// Convert the char to wide char
218-
const size_t sizeNeeded{mbstowcs(NULL, newCmd, 0) + 1};
217+
const size_t sizeNeeded{mbstowcs(NULL, newCmdWin, 0) + 1};
219218
wchar_t *wcmd{new wchar_t[sizeNeeded]};
220-
if (std::mbstowcs(wcmd, newCmd, sizeNeeded) == static_cast<size_t>(-1)) {
219+
if (std::mbstowcs(wcmd, newCmdWin, sizeNeeded) == static_cast<size_t>(-1)) {
221220
terminator.Crash("Char to wide char failed for newCmd");
222221
}
223-
free(newCmd);
222+
free(newCmdWin);
224223

225224
if (CreateProcess(nullptr, wcmd, nullptr, nullptr, FALSE, 0, nullptr,
226225
nullptr, &si, &pi)) {
226+
// Close handles so it will be removed when terminated
227227
CloseHandle(pi.hProcess);
228228
CloseHandle(pi.hThread);
229229
} else {

0 commit comments

Comments
 (0)