[flang-commits] [flang] [flang] Fix const cast issue in FreeMemory function call in execute_command_line (PR #77906)
Yi Wu via flang-commits
flang-commits at lists.llvm.org
Fri Jan 12 05:48:13 PST 2024
https://github.com/yi-wu-arm updated https://github.com/llvm/llvm-project/pull/77906
>From 2a36ab7f05771662c361a301f02039757694151a Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 12 Jan 2024 11:01:06 +0000
Subject: [PATCH 1/2] Fix const cast issue in FreeMemory function call
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 commit replaces the direct cast with a
const_cast<void *>(static_cast<const void *>(newCmd).
---
flang/runtime/execute.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flang/runtime/execute.cpp b/flang/runtime/execute.cpp
index 48773ae8114b0b..fa2b7eca0dde69 100644
--- a/flang/runtime/execute.cpp
+++ b/flang/runtime/execute.cpp
@@ -199,7 +199,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
}
// Deallocate memory if EnsureNullTerminated dynamically allocated memory
if (newCmd != command.OffsetElement()) {
- FreeMemory((void *)newCmd);
+ FreeMemory(const_cast<void *>(static_cast<const void *>(newCmd)));
}
}
>From 0525df1ba81764c10c6f0f4ab1d7a6d616430c6a Mon Sep 17 00:00:00 2001
From: Yi Wu <yi.wu2 at arm.com>
Date: Fri, 12 Jan 2024 13:40:43 +0000
Subject: [PATCH 2/2] remove const
---
flang/runtime/execute.cpp | 4 ++--
flang/runtime/tools.cpp | 4 ++--
flang/runtime/tools.h | 4 ++--
3 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/flang/runtime/execute.cpp b/flang/runtime/execute.cpp
index fa2b7eca0dde69..3a818670c96452 100644
--- a/flang/runtime/execute.cpp
+++ b/flang/runtime/execute.cpp
@@ -116,7 +116,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
const Descriptor *exitstat, const Descriptor *cmdstat,
const Descriptor *cmdmsg, const char *sourceFile, int line) {
Terminator terminator{sourceFile, line};
- const char *newCmd{EnsureNullTerminated(
+ char *newCmd{EnsureNullTerminated(
command.OffsetElement(), command.ElementBytes(), terminator)};
if (exitstat) {
@@ -199,7 +199,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
}
// Deallocate memory if EnsureNullTerminated dynamically allocated memory
if (newCmd != command.OffsetElement()) {
- FreeMemory(const_cast<void *>(static_cast<const void *>(newCmd)));
+ FreeMemory((void *)newCmd);
}
}
diff --git a/flang/runtime/tools.cpp b/flang/runtime/tools.cpp
index 6d2d86586c5fe6..e653323ed1de03 100644
--- a/flang/runtime/tools.cpp
+++ b/flang/runtime/tools.cpp
@@ -173,8 +173,8 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from) {
ShallowCopy(to, from, to.IsContiguous(), from.IsContiguous());
}
-RT_API_ATTRS const char *EnsureNullTerminated(
- const char *str, std::size_t length, Terminator &terminator) {
+RT_API_ATTRS char *EnsureNullTerminated(
+ char *str, std::size_t length, Terminator &terminator) {
if (std::memchr(str, '\0', length) == nullptr) {
char *newCmd{(char *)AllocateMemoryOrCrash(terminator, length + 1)};
std::memcpy(newCmd, str, length);
diff --git a/flang/runtime/tools.h b/flang/runtime/tools.h
index 47398a910ce73d..89e5069995748b 100644
--- a/flang/runtime/tools.h
+++ b/flang/runtime/tools.h
@@ -441,8 +441,8 @@ RT_API_ATTRS void ShallowCopy(const Descriptor &to, const Descriptor &from);
// size memory for null-terminator if necessary. Returns the original or a newly
// allocated null-terminated string (responsibility for deallocation is on the
// caller).
-RT_API_ATTRS const char *EnsureNullTerminated(
- const char *str, std::size_t length, Terminator &terminator);
+RT_API_ATTRS char *EnsureNullTerminated(
+ char *str, std::size_t length, Terminator &terminator);
RT_API_ATTRS bool IsValidCharDescriptor(const Descriptor *value);
More information about the flang-commits
mailing list