Skip to content

Commit 15a983c

Browse files
committed
add cmdmsg to common exit code 1, 126, 127 for Linux
for Windows, all three of them has a exit code of 1 so can't write cmdmsg for them
1 parent c38a5d1 commit 15a983c

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed

flang/runtime/execute.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,37 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
7979
CheckAndCopyCharsToDescriptor(cmdmsg, "Invalid command line");
8080
}
8181
}
82+
83+
#ifndef _WIN32
84+
if (exitStatusVal == 1) {
85+
if (!cmdstat) {
86+
terminator.Crash("General Error with exit status code: 1");
87+
} else {
88+
StoreIntToDescriptor(cmdstat, INVALID_CL_ERR, terminator);
89+
CheckAndCopyCharsToDescriptor(cmdmsg,
90+
"General Error: a catch-all exit code for a variety of general "
91+
"errors.");
92+
}
93+
} else if (exitStatusVal == 126) {
94+
if (!cmdstat) {
95+
terminator.Crash("Command cannot execute with exit status code: 126");
96+
} else {
97+
StoreIntToDescriptor(cmdstat, INVALID_CL_ERR, terminator);
98+
CheckAndCopyCharsToDescriptor(cmdmsg,
99+
"Command cannot execute: command was found, but it could not be "
100+
"executed.");
101+
}
102+
} else if (exitStatusVal == 127) {
103+
if (!cmdstat) {
104+
terminator.Crash("Command not found with exit status code: 127");
105+
} else {
106+
StoreIntToDescriptor(cmdstat, INVALID_CL_ERR, terminator);
107+
CheckAndCopyCharsToDescriptor(cmdmsg,
108+
"Command not found: command was not found in the system's PATH");
109+
}
110+
}
111+
#endif
112+
82113
if (status == -1) {
83114
if (!cmdstat) {
84115
terminator.Crash("Execution error with system status code: %d", status);

flang/unittests/Runtime/CommandTest.cpp

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,22 +339,64 @@ TEST_F(ZeroArguments, ECLValidCommandStatusSetSync) {
339339
CheckDescriptorEqStr(cmdMsg.get(), "No change");
340340
}
341341

342-
TEST_F(ZeroArguments, ECLInvalidCommandErrorSync) {
343-
OwningPtr<Descriptor> command{CharDescriptor("InvalidCommand")};
342+
TEST_F(ZeroArguments, ECLGeneralErrorCommandErrorSync) {
343+
OwningPtr<Descriptor> command{CharDescriptor("cat general-error")};
344+
bool wait{true};
345+
OwningPtr<Descriptor> exitStat{IntDescriptor(404)};
346+
OwningPtr<Descriptor> cmdStat{IntDescriptor(202)};
347+
OwningPtr<Descriptor> cmdMsg{CharDescriptor("cmd msg buffer XXXXXXXX")};
348+
349+
RTNAME(ExecuteCommandLine)
350+
(*command.get(), wait, exitStat.get(), cmdStat.get(), cmdMsg.get());
351+
#ifdef _WIN32
352+
CheckDescriptorEqInt(exitStat.get(), 1);
353+
CheckDescriptorEqStr(cmdMsg.get(), "Invalid command lineXXX");
354+
#else
355+
CheckDescriptorEqInt<std::int64_t>(exitStat.get(), 1);
356+
CheckDescriptorEqStr(cmdMsg.get(), "General Error: a catch-");
357+
#endif
358+
CheckDescriptorEqInt<std::int64_t>(cmdStat.get(), 3);
359+
}
360+
361+
TEST_F(ZeroArguments, ECLNotExecutedCommandErrorSync) {
362+
OwningPtr<Descriptor> command{CharDescriptor("cd NotExecutedCommand")};
363+
bool wait{true};
364+
OwningPtr<Descriptor> exitStat{IntDescriptor(404)};
365+
OwningPtr<Descriptor> cmdStat{IntDescriptor(202)};
366+
OwningPtr<Descriptor> cmdMsg{CharDescriptor("cmd msg buffer XXXXXXXX")};
367+
368+
RTNAME(ExecuteCommandLine)
369+
(*command.get(), wait, exitStat.get(), cmdStat.get(), cmdMsg.get());
370+
#ifdef _WIN32
371+
CheckDescriptorEqInt(exitStat.get(), 1);
372+
CheckDescriptorEqStr(cmdMsg.get(), "Invalid command lineXXX");
373+
#else
374+
CheckDescriptorEqInt<std::int64_t>(exitStat.get(), 126);
375+
CheckDescriptorEqStr(cmdMsg.get(), "Command cannot execute:");
376+
#endif
377+
CheckDescriptorEqInt<std::int64_t>(cmdStat.get(), 3);
378+
}
379+
380+
TEST_F(ZeroArguments, ECLNotFoundCommandErrorSync) {
381+
OwningPtr<Descriptor> command{CharDescriptor("NotFoundCommand")};
344382
bool wait{true};
345383
OwningPtr<Descriptor> exitStat{IntDescriptor(404)};
346384
OwningPtr<Descriptor> cmdStat{IntDescriptor(202)};
347-
OwningPtr<Descriptor> cmdMsg{CharDescriptor("Message ChangedXXXXXXXXX")};
385+
OwningPtr<Descriptor> cmdMsg{CharDescriptor(
386+
"cmdmsg will not modify the remaining buffer XXXXXXXXXXXXXXXXXXXX")};
348387

349388
RTNAME(ExecuteCommandLine)
350389
(*command.get(), wait, exitStat.get(), cmdStat.get(), cmdMsg.get());
351390
#ifdef _WIN32
352391
CheckDescriptorEqInt(exitStat.get(), 1);
392+
CheckDescriptorEqStr(cmdMsg.get(),
393+
"Invalid command lineXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
353394
#else
354395
CheckDescriptorEqInt<std::int64_t>(exitStat.get(), 127);
396+
CheckDescriptorEqStr(cmdMsg.get(),
397+
"Command not found: command was not found in the system's PATHXXX");
355398
#endif
356399
CheckDescriptorEqInt<std::int64_t>(cmdStat.get(), 3);
357-
CheckDescriptorEqStr(cmdMsg.get(), "Invalid command lineXXXX");
358400
}
359401

360402
TEST_F(ZeroArguments, ECLInvalidCommandTerminatedSync) {

0 commit comments

Comments
 (0)