Skip to content

Commit 7dd4d28

Browse files
authored
[flang] Fix const cast issue in FreeMemory function call in execute_command_line (#77906)
The FreeMemory function only accepts a void pointer, but it was being called with a const char pointer, resulting in a type-casting issue. To address this, the const was removed, use char * instead.
1 parent 3edf82d commit 7dd4d28

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

flang/docs/Intrinsics.md

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ used in constant expressions have currently no folding support at all.
847847

848848
#### Usage and Info
849849

850-
- **Standard:** Fortran 2008 and later, specified in 16.9.73
850+
- **Standard:** Fortran 2008 and later, specified in subclause 16.9.73
851851
- **Class:** Subroutine
852852
- **Syntax:** `CALL EXECUTE_COMMAND_LINE(COMMAND [, WAIT, EXITSTAT, CMDSTAT, CMDMSG ])`
853853
- **Arguments:**
@@ -862,28 +862,50 @@ used in constant expressions have currently no folding support at all.
862862

863863
#### Implementation Specifics
864864

865-
- **`COMMAND`:**
866-
- Must be preset.
865+
##### `COMMAND`:
867866

868-
- **`WAIT`:**
869-
- If set to `false`, the command is executed asynchronously. If not preset or set to `false`, it is executed synchronously.
870-
- Sync: achieved by passing command into `std::system` on all systems.
871-
- Async: achieved by calling a `fork()` on POSIX-compatible systems, or `CreateProcess()` on Windows.
867+
- Must be preset.
872868

873-
- **`CMDSTAT`:**
869+
##### `WAIT`:
870+
871+
- If set to `false`, the command is executed asynchronously.
872+
- If not preset or set to `true`, it is executed synchronously.
873+
- Synchronous execution is achieved by passing the command into `std::system` on all systems.
874+
- Asynchronous execution is achieved by calling `fork()` on POSIX-compatible systems or `CreateProcess()` on Windows.
875+
876+
##### `EXITSTAT`:
877+
878+
- Synchronous execution:
879+
- Inferred by the return value of `std::system(cmd)`.
880+
- On POSIX-compatible systems: return value is first passed into `WEXITSTATUS(status)`, then assigned to `EXITSTAT`.
881+
- On Windows, the value is directly assigned as the return value of `std::system()`.
882+
- Asynchronous execution:
883+
- Value is not modified.
884+
885+
##### `CMDSTAT`:
886+
887+
- Synchronous execution:
874888
- -2: No error condition occurs, but `WAIT` is present with the value `false`, and the processor does not support asynchronous execution.
875889
- -1: The processor does not support command line execution.
876890
- \+ (positive value): An error condition occurs.
877-
- 1: Fork Error, where `pid_t < 0`, would only occur on POSIX-compatible systems.
878-
- 2: Execution Error, a command exits with status -1.
879-
- 3: Invalid Command Error, determined by the exit code depending on the system.
880-
- On Windows, if the exit code is 1.
881-
- On POSIX-compatible systems, if the exit code is 127 or 126.
882-
- 4: Signal error, either it is stopped or killed by signal, would only occur on POSIX-compatible systems.
891+
- 1: Fork Error (occurs only on POSIX-compatible systems).
892+
- 2: Execution Error (command exits with status -1).
893+
- 3: Invalid Command Error (determined by the exit code depending on the system).
894+
- On Windows: exit code is 1.
895+
- On POSIX-compatible systems: exit code is 127 or 126.
896+
- 4: Signal error (either stopped or killed by signal, occurs only on POSIX-compatible systems).
883897
- 0: Otherwise.
898+
- Asynchronous execution:
899+
- 0 will always be assigned.
900+
901+
##### `CMDMSG`:
884902

885-
- **`CMDMSG`:**
886-
- If an error condition occurs, it is assigned an explanatory message. Otherwise, it remains unchanged.
903+
- Synchronous execution:
904+
- If an error condition occurs, it is assigned an explanatory message; otherwise, it remains unchanged.
905+
- If a condition occurs that would assign a nonzero value to `CMDSTAT` but the `CMDSTAT` variable is not present, error termination is initiated (applies to both POSIX-compatible systems and Windows).
906+
- Asynchronous execution:
907+
- The value is unchanged.
887908
- If a condition occurs that would assign a nonzero value to `CMDSTAT` but the `CMDSTAT` variable is not present, error termination is initiated.
888-
- On POSIX-compatible systems, this applies to both synchronous and asynchronous error termination. When the execution mode is set to async with error termination, the child process (async process) will be terminated with no effect on the parent process (continues).
889-
- On Windows, this only applies to synchronous error termination.
909+
- On POSIX-compatible systems, the child process (async process) will be terminated with no effect on the parent process (continues).
910+
- On Windows, error termination is not initiated.
911+

flang/runtime/execute.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
117117
const Descriptor *exitstat, const Descriptor *cmdstat,
118118
const Descriptor *cmdmsg, const char *sourceFile, int line) {
119119
Terminator terminator{sourceFile, line};
120-
const char *newCmd{EnsureNullTerminated(
120+
char *newCmd{EnsureNullTerminated(
121121
command.OffsetElement(), command.ElementBytes(), terminator)};
122122

123123
if (exitstat) {

flang/runtime/tools.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from) {
173173
ShallowCopy(to, from, to.IsContiguous(), from.IsContiguous());
174174
}
175175

176-
RT_API_ATTRS const char *EnsureNullTerminated(
177-
const char *str, std::size_t length, Terminator &terminator) {
176+
RT_API_ATTRS char *EnsureNullTerminated(
177+
char *str, std::size_t length, Terminator &terminator) {
178178
if (std::memchr(str, '\0', length) == nullptr) {
179179
char *newCmd{(char *)AllocateMemoryOrCrash(terminator, length + 1)};
180180
std::memcpy(newCmd, str, length);

flang/runtime/tools.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,8 +441,8 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from);
441441
// size memory for null-terminator if necessary. Returns the original or a newly
442442
// allocated null-terminated string (responsibility for deallocation is on the
443443
// caller).
444-
RT_API_ATTRS const char *EnsureNullTerminated(
445-
const char *str, std::size_t length, Terminator &terminator);
444+
RT_API_ATTRS char *EnsureNullTerminated(
445+
char *str, std::size_t length, Terminator &terminator);
446446

447447
RT_API_ATTRS bool IsValidCharDescriptor(const Descriptor *value);
448448

0 commit comments

Comments
 (0)