From 3b8dfe2e279381d181e7938e4591d5964cc2649a Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Wed, 25 Jan 2023 18:50:18 -0800 Subject: [PATCH] Don't use free to satisfy Prefast requirements (#14354) ### Description Don't use free to satisfy Prefast requirements ### Motivation and Context Fix ADO#9004 --- onnxruntime/core/providers/cuda/cuda_call.cc | 17 +++++----- onnxruntime/core/providers/rocm/rocm_call.cc | 33 +++++++++----------- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/onnxruntime/core/providers/cuda/cuda_call.cc b/onnxruntime/core/providers/cuda/cuda_call.cc index 09e575328a..e6546765d1 100644 --- a/onnxruntime/core/providers/cuda/cuda_call.cc +++ b/onnxruntime/core/providers/cuda/cuda_call.cc @@ -3,6 +3,7 @@ #include "core/providers/shared_library/provider_api.h" #include "shared_inc/cuda_call.h" +#include #ifdef _WIN32 #else // POSIX @@ -86,19 +87,15 @@ const char* CudaErrString(ncclResult_t e) { template std::conditional_t CudaCall( - ERRTYPE retCode, const char* exprString, const char* libName, ERRTYPE successCode, const char* msg) { + ERRTYPE retCode, const char* exprString, const char* libName, ERRTYPE successCode, const char* msg) { if (retCode != successCode) { try { #ifdef _WIN32 - auto del = [](char* p) { free(p); }; - std::unique_ptr hostname_ptr(nullptr, del); - size_t hostname_len = 0; - char* hostname = nullptr; - //TODO: avoid using const_cast - if (-1 == _dupenv_s(&hostname, &hostname_len, "COMPUTERNAME")) - hostname = const_cast("?"); - else - hostname_ptr.reset(hostname); + std::string hostname_str = GetEnvironmentVar("COMPUTERNAME"); + if (hostname_str.empty()) { + hostname_str = "?"; + } + const char* hostname = hostname_str.c_str(); #else char hostname[HOST_NAME_MAX]; if (gethostname(hostname, HOST_NAME_MAX) != 0) diff --git a/onnxruntime/core/providers/rocm/rocm_call.cc b/onnxruntime/core/providers/rocm/rocm_call.cc index e4f4b6d2a3..c0e572b344 100644 --- a/onnxruntime/core/providers/rocm/rocm_call.cc +++ b/onnxruntime/core/providers/rocm/rocm_call.cc @@ -3,6 +3,7 @@ #include "core/providers/shared_library/provider_api.h" #include "shared_inc/rocm_call.h" +#include #ifdef _WIN32 #else // POSIX @@ -25,13 +26,13 @@ const char* RocmErrString(ERRTYPE) { template <> const char* RocmErrString(hipError_t x) { - (void)hipDeviceSynchronize(); // void to silence nodiscard + ORT_IGNORE_RETURN_VALUE(hipDeviceSynchronize()); // void to silence nodiscard return hipGetErrorString(x); } template <> const char* RocmErrString(rocblas_status e) { - (void)hipDeviceSynchronize(); // void to silence nodiscard + ORT_IGNORE_RETURN_VALUE(hipDeviceSynchronize()); // void to silence nodiscard switch (e) { CASE_ENUM_TO_STR(rocblas_status_success); @@ -54,19 +55,19 @@ const char* RocmErrString(rocblas_status e) { template <> const char* RocmErrString(hiprandStatus_t) { - (void)hipDeviceSynchronize(); // void to silence nodiscard + ORT_IGNORE_RETURN_VALUE(hipDeviceSynchronize()); // void to silence nodiscard return "(see hiprand.h & look for hiprandStatus_t or HIPRAND_STATUS_xxx)"; } template <> const char* RocmErrString(miopenStatus_t e) { - (void)hipDeviceSynchronize(); // void to silence nodiscard + ORT_IGNORE_RETURN_VALUE(hipDeviceSynchronize()); // void to silence nodiscard return miopenGetErrorString(e); } template <> const char* RocmErrString(hipfftResult e) { - (void)hipDeviceSynchronize(); // void to silence nodiscard + ORT_IGNORE_RETURN_VALUE(hipDeviceSynchronize()); // void to silence nodiscard switch (e) { CASE_ENUM_TO_STR(HIPFFT_SUCCESS); CASE_ENUM_TO_STR(HIPFFT_ALLOC_FAILED); @@ -82,34 +83,30 @@ const char* RocmErrString(hipfftResult e) { #ifdef ORT_USE_NCCL template <> const char* RocmErrString(ncclResult_t e) { - (void)hipDeviceSynchronize(); // void to silence nodiscard + ORT_IGNORE_RETURN_VALUE(hipDeviceSynchronize()); // void to silence nodiscard return ncclGetErrorString(e); } #endif template std::conditional_t RocmCall( - ERRTYPE retCode, const char* exprString, const char* libName, ERRTYPE successCode, const char* msg) { + ERRTYPE retCode, const char* exprString, const char* libName, ERRTYPE successCode, const char* msg) { if (retCode != successCode) { try { #ifdef _WIN32 - auto del = [](char* p) { free(p); }; - std::unique_ptr hostname_ptr(nullptr, del); - size_t hostname_len = 0; - char* hostname = nullptr; - //TODO: avoid using const_cast - if (-1 == _dupenv_s(&hostname, &hostname_len, "COMPUTERNAME")) - hostname = const_cast("?"); - else - hostname_ptr.reset(hostname); + std::string hostname_str = GetEnvironmentVar("COMPUTERNAME"); + if (hostname_str.empty()) { + hostname_str = "?"; + } + const char* hostname = hostname_str.c_str(); #else char hostname[HOST_NAME_MAX]; if (gethostname(hostname, HOST_NAME_MAX) != 0) strcpy(hostname, "?"); #endif int currentHipDevice; - (void)hipGetDevice(¤tHipDevice); // void to silence nodiscard - (void)hipGetLastError(); // clear last ROCM error; void to silence nodiscard + ORT_IGNORE_RETURN_VALUE(hipGetDevice(¤tHipDevice)); // void to silence nodiscard + ORT_IGNORE_RETURN_VALUE(hipGetLastError()); // clear last ROCM error; void to silence nodiscard static char str[1024]; snprintf(str, 1024, "%s failure %d: %s ; GPU=%d ; hostname=%s ; expr=%s; %s", libName, (int)retCode, RocmErrString(retCode), currentHipDevice,