From c704b25b449418217f2e3ee41c100f3ee9a59e4c Mon Sep 17 00:00:00 2001 From: AndreyChurbanov Date: Wed, 27 Oct 2021 16:38:09 +0300 Subject: [PATCH] [OpenMP] libomp: Fix possible NULL dereference. According to dlsym description, the value of symbol could be NULL, and there is no error in this case. Thus dlerror will also return NULL in this case. We need to check the value returned by dlerror before printing it. Differential Revision: https://reviews.llvm.org/D112174 --- openmp/runtime/src/ompt-general.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/openmp/runtime/src/ompt-general.cpp b/openmp/runtime/src/ompt-general.cpp index 89cd23ce34a0..c4a9be8753b7 100644 --- a/openmp/runtime/src/ompt-general.cpp +++ b/openmp/runtime/src/ompt-general.cpp @@ -346,9 +346,16 @@ ompt_try_start_tool(unsigned int omp_version, const char *runtime_version) { OMPT_VERBOSE_INIT_CONTINUED_PRINT("Success. \n"); OMPT_VERBOSE_INIT_PRINT("Searching for ompt_start_tool in %s... ", fname); + dlerror(); // Clear any existing error start_tool = (ompt_start_tool_t)dlsym(h, "ompt_start_tool"); if (!start_tool) { - OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", dlerror()); + char *error = dlerror(); + if (error != NULL) { + OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", error); + } else { + OMPT_VERBOSE_INIT_CONTINUED_PRINT("Failed: %s\n", + "ompt_start_tool = NULL"); + } } else #elif KMP_OS_WINDOWS OMPT_VERBOSE_INIT_PRINT("Opening %s... ", fname);