From 648e303072c77e781eca2bb06f488f6be9ccac84 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Sat, 24 Mar 2018 23:12:40 +1300 Subject: [PATCH 2/2] Minor code cleanup for llvmjit_wrap.cpp. llvm::sys::getHostCPUName()'s result is a llvm::StringRef. Its data() member function doesn't guarantee a null-terminated result, so we'd better jump through an extra hoop to get a C string. It seems better to use LLVMCreateMessage() rather than strdup() to allocate the copy returned by LLVMGetHostCPUFeatures() and LLVMGetHostCPUName(), since the contract is that the caller should free it with LLVMDisposeMessage(). While we can see that LLVMCreateMessage() and LLVMDisposeMessage() are currently just wrappers for strdup() and free(), using them symmetrically seems like a good idea for future Windows support, where DLLs can be using different heap allocators (the same reason we provide PQfreemem in libpq). Fix brace style. Thomas Munro --- src/backend/jit/llvm/llvmjit_wrap.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/backend/jit/llvm/llvmjit_wrap.cpp b/src/backend/jit/llvm/llvmjit_wrap.cpp index 5d1a17cde04..d2b19ab9a8b 100644 --- a/src/backend/jit/llvm/llvmjit_wrap.cpp +++ b/src/backend/jit/llvm/llvmjit_wrap.cpp @@ -18,6 +18,7 @@ extern "C" #include #include +#include #include "jit/llvmjit.h" @@ -26,13 +27,15 @@ extern "C" * C-API extensions. */ #if defined(HAVE_DECL_LLVMGETHOSTCPUNAME) && !HAVE_DECL_LLVMGETHOSTCPUNAME -char *LLVMGetHostCPUName(void) { - return strdup(llvm::sys::getHostCPUName().data()); +char *LLVMGetHostCPUName(void) +{ + return LLVMCreateMessage(llvm::sys::getHostCPUName().str().c_str()); } #endif -char *LLVMGetHostCPUFeatures(void) { +char *LLVMGetHostCPUFeatures(void) +{ llvm::SubtargetFeatures Features; llvm::StringMap HostFeatures; @@ -40,5 +43,5 @@ char *LLVMGetHostCPUFeatures(void) { for (auto &F : HostFeatures) Features.AddFeature(F.first(), F.second); - return strdup(Features.getString().c_str()); + return LLVMCreateMessage(Features.getString().c_str()); } -- 2.16.2