From 9463eeef2bf68962e86f4d25df31584487e4c82f Mon Sep 17 00:00:00 2001 From: Aldrin Date: Sat, 27 Jul 2024 14:57:52 +0530 Subject: [PATCH] fix: Avoided dereferencing NULL pointer (#34836) issue : https://github.com/milvus-io/milvus/issues/34835 Signed-off-by: Ald392 --- internal/core/src/indexbuilder/init_c.cpp | 3 +++ internal/core/src/log/Log.cpp | 3 +++ internal/core/src/segcore/metrics_c.cpp | 3 +++ internal/core/src/segcore/segcore_init_c.cpp | 2 ++ 4 files changed, 11 insertions(+) diff --git a/internal/core/src/indexbuilder/init_c.cpp b/internal/core/src/indexbuilder/init_c.cpp index 0ed371dc47..e0e80bb21a 100644 --- a/internal/core/src/indexbuilder/init_c.cpp +++ b/internal/core/src/indexbuilder/init_c.cpp @@ -10,7 +10,9 @@ // or implied. See the License for the specific language governing permissions and limitations under the License #include +#include "common/EasyAssert.h" #include "config/ConfigKnowhere.h" +#include "fmt/core.h" #include "indexbuilder/init_c.h" void @@ -23,6 +25,7 @@ char* IndexBuilderSetSimdType(const char* value) { auto real_type = milvus::config::KnowhereSetSimdType(value); char* ret = reinterpret_cast(malloc(real_type.length() + 1)); + AssertInfo(ret != nullptr, "memmory allocation for ret failed!"); memcpy(ret, real_type.c_str(), real_type.length()); ret[real_type.length()] = 0; return ret; diff --git a/internal/core/src/log/Log.cpp b/internal/core/src/log/Log.cpp index 4ec482e676..68ac1b21cc 100644 --- a/internal/core/src/log/Log.cpp +++ b/internal/core/src/log/Log.cpp @@ -14,6 +14,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "common/EasyAssert.h" +#include "fmt/core.h" #include "log/Log.h" /* @@ -105,6 +107,7 @@ get_thread_starttime() { int64_t val = 0; char comm[16], state; FILE* thread_stat = fopen(filename, "r"); + AssertInfo(thread_stat != nullptr, "opening file:{} failed!", filename); auto ret = fscanf( thread_stat, "%lld %s %s ", (long long*)&val, comm, &state); // NOLINT diff --git a/internal/core/src/segcore/metrics_c.cpp b/internal/core/src/segcore/metrics_c.cpp index 2a754f75b6..a912360ba9 100644 --- a/internal/core/src/segcore/metrics_c.cpp +++ b/internal/core/src/segcore/metrics_c.cpp @@ -11,6 +11,8 @@ #include +#include "common/EasyAssert.h" +#include "fmt/core.h" #include "knowhere/prometheus_client.h" #include "segcore/metrics_c.h" @@ -19,6 +21,7 @@ GetKnowhereMetrics() { auto str = knowhere::prometheusClient->GetMetrics(); auto len = str.length(); char* res = (char*)malloc(len + 1); + AssertInfo(res != nullptr, "memmory allocation for res failed!"); memcpy(res, str.data(), len); res[len] = '\0'; return res; diff --git a/internal/core/src/segcore/segcore_init_c.cpp b/internal/core/src/segcore/segcore_init_c.cpp index 060d3e5f32..38ffbe6bc6 100644 --- a/internal/core/src/segcore/segcore_init_c.cpp +++ b/internal/core/src/segcore/segcore_init_c.cpp @@ -10,6 +10,7 @@ // or implied. See the License for the specific language governing permissions and limitations under the License #include "config/ConfigKnowhere.h" +#include "fmt/core.h" #include "log/Log.h" #include "segcore/SegcoreConfig.h" #include "segcore/segcore_init_c.h" @@ -74,6 +75,7 @@ SegcoreSetSimdType(const char* value) { LOG_DEBUG("set config simd_type: {}", value); auto real_type = milvus::config::KnowhereSetSimdType(value); char* ret = reinterpret_cast(malloc(real_type.length() + 1)); + AssertInfo(ret != nullptr, "memmory allocation for ret failed!"); memcpy(ret, real_type.c_str(), real_type.length()); ret[real_type.length()] = 0; return ret;