Skip to content

[flang] Fix const cast issue in FreeMemory function call in execute_command_line #77906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 13, 2024

Conversation

yiwu0b11
Copy link
Contributor

@yiwu0b11 yiwu0b11 commented Jan 12, 2024

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.

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).
@yiwu0b11 yiwu0b11 requested a review from klausler January 12, 2024 11:04
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Jan 12, 2024
@llvmbot
Copy link
Member

llvmbot commented Jan 12, 2024

@llvm/pr-subscribers-flang-runtime

Author: Yi Wu (yi-wu-arm)

Changes

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).


Full diff: https://github.com/llvm/llvm-project/pull/77906.diff

1 Files Affected:

  • (modified) flang/runtime/execute.cpp (+1-1)
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)));
   }
 }
 

@yiwu0b11
Copy link
Contributor Author

@klausler point it out in the flang slack channel:
flang/runtime/execute.cpp:202:24: error: cast from type 'const char*' to type 'void*' casts away qualifiers [-Werror=cast-qual]
202 | FreeMemory((void *)newCmd);
| ^~~~~~

@yiwu0b11
Copy link
Contributor Author

Build and pass all test on clang 10.0.0 and on gcc 9.4.0

@yiwu0b11 yiwu0b11 merged commit 7dd4d28 into llvm:main Jan 13, 2024
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
…ommand_line (llvm#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants