From 0e92ac2af7af311b60a5f337a311877087016175 Mon Sep 17 00:00:00 2001 From: Matthias Gehre Date: Tue, 20 Aug 2019 23:27:36 +0200 Subject: [PATCH] modules/core/src/ocl.cpp: Fix dangling pointer Detected by clang trunk: ``` opencv/modules/core/src/ocl.cpp:4337:37: warning: object backing the pointer will be destroyed at the end of the full-expression [-Wdangling] CV_OCL_CHECK_RESULT(retval, cv::format("clCreateBuffer(capacity=%lld) => %p", (long long int)entry.capacity_, (void*)entry.clBuffer_).c_str()); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ opencv/modules/core/src/ocl.cpp:193:42: note: expanded from macro 'CV_OCL_CHECK_RESULT' if (0) { const char* msg_ = (msg); CV_UNUSED(msg_); /* ensure const char* type (cv::String without c_str()) */ } \ ``` because `cv::format` yields a temporary std::string, and thus `msg_` points to a destroyed buffer. --- modules/core/src/ocl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index bb395b3cda..d1791d83bd 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -190,7 +190,7 @@ void traceOpenCLCheck(cl_int status, const char* message) CV_OCL_TRACE_CHECK_RESULT(check_result, msg); \ if (check_result != CL_SUCCESS) \ { \ - if (0) { const char* msg_ = (msg); CV_UNUSED(msg_); /* ensure const char* type (cv::String without c_str()) */ } \ + static_assert(std::is_convertible::value, "msg of CV_OCL_CHECK_RESULT must be const char*"); \ cv::String error_msg = CV_OCL_API_ERROR_MSG(check_result, msg); \ CV_Error(Error::OpenCLApiCallError, error_msg); \ } \ @@ -210,7 +210,7 @@ void traceOpenCLCheck(cl_int status, const char* message) CV_OCL_TRACE_CHECK_RESULT(check_result, msg); \ if (check_result != CL_SUCCESS && isRaiseError()) \ { \ - if (0) { const char* msg_ = (msg); CV_UNUSED(msg_); /* ensure const char* type (cv::String without c_str()) */ } \ + static_assert(std::is_convertible::value, "msg of CV_OCL_DBG_CHECK_RESULT must be const char*"); \ cv::String error_msg = CV_OCL_API_ERROR_MSG(check_result, msg); \ CV_Error(Error::OpenCLApiCallError, error_msg); \ } \