Skip to content

Commit bc6aef8

Browse files
committed
move EnsureNullTerminated to tools.h, free memory after use.
On Linux, malloc is freed in parent, child process will be cleaned up on termination. Same approach was taken on Windows as well, but does it work in the same way?
1 parent dfac142 commit bc6aef8

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

flang/runtime/execute.cpp

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,11 @@ int TerminationCheck(int status, const Descriptor *cmdstat,
147147
return exitStatusVal;
148148
}
149149

150-
const char *ensureNullTerminated(
151-
const char *str, size_t length, Terminator &terminator) {
152-
if (length <= strlen(str)) {
153-
char *newCmd{(char *)malloc(length + 1)};
154-
if (newCmd == NULL) {
155-
terminator.Crash("Command not null-terminated, memory allocation failed "
156-
"for null-terminated newCmd.");
157-
}
158-
159-
strncpy(newCmd, str, length);
160-
newCmd[length] = '\0';
161-
return newCmd;
162-
} else {
163-
return str;
164-
}
165-
}
166-
167150
void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
168151
const Descriptor *exitstat, const Descriptor *cmdstat,
169152
const Descriptor *cmdmsg, const char *sourceFile, int line) {
170153
Terminator terminator{sourceFile, line};
171-
const char *newCmd{ensureNullTerminated(
154+
const char *newCmd{EnsureNullTerminated(
172155
command.OffsetElement(), command.ElementBytes(), terminator)};
173156

174157
if (exitstat) {
@@ -202,22 +185,18 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
202185

203186
// append "cmd.exe /c " to the beginning of command
204187
const char *prefix{"cmd.exe /c "};
205-
char *newCmdWin{
206-
(char *)malloc(std::strlen(prefix) + std::strlen(newCmd) + 1)};
207-
if (newCmd != NULL) {
208-
std::strcpy(newCmdWin, prefix);
209-
std::strcat(newCmdWin, newCmd);
210-
} else {
211-
terminator.Crash("Memory allocation failed for newCmd");
212-
}
188+
char *newCmdWin{(char *)AllocateMemoryOrCrash(
189+
terminator, std::strlen(prefix) + std::strlen(newCmd) + 1)};
190+
std::strcpy(newCmdWin, prefix);
191+
std::strcat(newCmdWin, newCmd);
213192

214193
// Convert the char to wide char
215194
const size_t sizeNeeded{mbstowcs(NULL, newCmdWin, 0) + 1};
216195
wchar_t *wcmd{new wchar_t[sizeNeeded]};
217196
if (std::mbstowcs(wcmd, newCmdWin, sizeNeeded) == static_cast<size_t>(-1)) {
218197
terminator.Crash("Char to wide char failed for newCmd");
219198
}
220-
free(newCmdWin);
199+
FreeMemory((void *)newCmdWin);
221200

222201
if (CreateProcess(nullptr, wcmd, nullptr, nullptr, FALSE, 0, nullptr,
223202
nullptr, &si, &pi)) {
@@ -233,7 +212,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
233212
CheckAndCopyToDescriptor(cmdmsg, "CreateProcess failed.");
234213
}
235214
}
236-
delete[] wcmd;
215+
FreeMemory((void *)wcmd);
237216
#else
238217
// terminated children do not become zombies
239218
signal(SIGCHLD, SIG_IGN);
@@ -251,6 +230,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
251230
exit(status);
252231
}
253232
#endif
233+
FreeMemory((void *)newCmd);
254234
}
255235
}
256236

flang/runtime/tools.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,5 +411,17 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from,
411411
bool toIsContiguous, bool fromIsContiguous);
412412
RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from);
413413

414+
inline RT_API_ATTRS const char *EnsureNullTerminated(
415+
const char *str, size_t length, Terminator &terminator) {
416+
if (length <= std::strlen(str)) {
417+
char *newCmd{(char *)AllocateMemoryOrCrash(terminator, length + 1)};
418+
std::memcpy(newCmd, str, length);
419+
newCmd[length] = '\0';
420+
return newCmd;
421+
} else {
422+
return str;
423+
}
424+
}
425+
414426
} // namespace Fortran::runtime
415427
#endif // FORTRAN_RUNTIME_TOOLS_H_

0 commit comments

Comments
 (0)