[flang-commits] [flang] [flang] add SYSTEM runtime and lowering intrinsics support (PR #74309)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Wed Dec 13 09:15:55 PST 2023
================
@@ -282,4 +282,40 @@ std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
return StatOk;
}
+const char *ensureNullTerminated(
+ const char *str, size_t length, Terminator &terminator) {
+ if (length <= strlen(str)) {
+ char *newCmd{(char *)malloc(length + 1)};
+ if (newCmd == NULL) {
+ terminator.Crash("Command not null-terminated, memory allocation failed "
+ "for null-terminated newCmd.");
+ }
+
+ strncpy(newCmd, str, length);
+ newCmd[length] = '\0';
+ return newCmd;
+ } else {
+ return str;
+ }
+}
+
+void RTNAME(System)(const Descriptor &command, const Descriptor *exitstat,
+ const char *sourceFile, int line) {
+ Terminator terminator{sourceFile, line};
+
+ const char *newCmd{ensureNullTerminated(
+ command.OffsetElement(), command.ElementBytes(), terminator)};
+ int status{std::system(newCmd)};
+
----------------
klausler wrote:
This blank line should instead deallocate any storage allocated by `EnsureNullTerminated`. Otherwise it's just going to leak. Please don't leak memory.
https://github.com/llvm/llvm-project/pull/74309
More information about the flang-commits
mailing list