[Beignet] [PATCH] Add the NULL pointer check.

Yang Rong rong.r.yang at intel.com
Fri Dec 16 08:59:47 UTC 2016


---
 backend/src/backend/gen_program.cpp  | 14 ++++++++------
 backend/src/backend/program.cpp      |  2 ++
 backend/src/llvm/PromoteIntegers.cpp |  1 +
 src/cl_device_id.c                   |  2 +-
 src/cl_event.c                       |  2 +-
 src/cl_kernel.c                      |  3 ++-
 src/cl_program.c                     | 26 ++++++++++++++++----------
 src/performance.c                    |  6 ++++++
 8 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index ebba7d4..e8fc788 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -545,7 +545,9 @@ namespace gbe {
         if (strstr(options, "-cl-fast-relaxed-math") != NULL)
           fast_relaxed_math = 1;
 
-    char *options_str = (char *)malloc(sizeof(char) * (strlen(options) + 1));
+      char *options_str = (char *)malloc(sizeof(char) * (strlen(options) + 1));
+      if (options_str == NULL)
+        return;
       memcpy(options_str, options, strlen(options) + 1);
       std::string optionStr(options_str);
       while (end != std::string::npos) {
@@ -566,11 +568,11 @@ namespace gbe {
     GenProgram* p = (GenProgram*) program;
     p->fast_relaxed_math = fast_relaxed_math;
     if (!dumpASMFileName.empty()) {
-        p->asm_file_name = dumpASMFileName.c_str();
-        FILE *asmDumpStream = fopen(dumpASMFileName.c_str(), "w");
-        if (asmDumpStream)
-          fclose(asmDumpStream);
-      }
+      p->asm_file_name = dumpASMFileName.c_str();
+      FILE *asmDumpStream = fopen(dumpASMFileName.c_str(), "w");
+      if (asmDumpStream)
+        fclose(asmDumpStream);
+    }
     // Try to compile the program
     acquireLLVMContextLock();
     llvm::Module* module = (llvm::Module*)p->module;
diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index c68d604..44252ea 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -886,6 +886,8 @@ namespace gbe {
 
     if (options) {
       char *c_str = (char *)malloc(sizeof(char) * (strlen(options) + 1));
+      if (c_str == NULL)
+        return false;
       memcpy(c_str, options, strlen(options) + 1);
       std::string optionStr(c_str);
       const std::string unsupportedOptions("-cl-denorms-are-zero, -cl-strict-aliasing, -cl-opt-disable,"
diff --git a/backend/src/llvm/PromoteIntegers.cpp b/backend/src/llvm/PromoteIntegers.cpp
index adba004..a500311 100644
--- a/backend/src/llvm/PromoteIntegers.cpp
+++ b/backend/src/llvm/PromoteIntegers.cpp
@@ -151,6 +151,7 @@ static Value *convertConstant(Constant *C, bool SignExt=false) {
   } else {
     errs() << "Value: " << *C << "\n";
     report_fatal_error("Unexpected constant value");
+    return NULL;
   }
 }
 
diff --git a/src/cl_device_id.c b/src/cl_device_id.c
index 444f3e2..fafb418 100644
--- a/src/cl_device_id.c
+++ b/src/cl_device_id.c
@@ -1389,7 +1389,7 @@ cl_check_builtin_kernel_dimension(cl_kernel kernel, cl_device_id device)
   const char * n = cl_kernel_get_name(kernel);
   const char * builtin_kernels_2d = "__cl_copy_image_2d_to_2d;__cl_copy_image_2d_to_buffer;__cl_copy_buffer_to_image_2d;__cl_fill_image_2d;__cl_fill_image_2d_array;";
   const char * builtin_kernels_3d = "__cl_copy_image_3d_to_2d;__cl_copy_image_2d_to_3d;__cl_copy_image_3d_to_3d;__cl_copy_image_3d_to_buffer;__cl_copy_buffer_to_image_3d;__cl_fill_image_3d";
-    if (!strstr(device->built_in_kernels, n)){
+    if (n == NULL || !strstr(device->built_in_kernels, n)){
       return 0;
     }else if(strstr(builtin_kernels_2d, n)){
       return 2;
diff --git a/src/cl_event.c b/src/cl_event.c
index 0804dbd..2c5cdd0 100644
--- a/src/cl_event.c
+++ b/src/cl_event.c
@@ -206,7 +206,7 @@ cl_event_create(cl_context ctx, cl_command_queue queue, cl_uint num_events,
       CL_OBJECT_UNLOCK(queue);
 
       for (i = 0; i < num_events; i++) {
-        assert(event_list[i]);
+        assert(event_list && event_list[i]);
         assert(event_list[i]->ctx == ctx);
         assert(CL_OBJECT_IS_EVENT(event_list[i]));
         cl_event_add_ref(event_list[i]);
diff --git a/src/cl_kernel.c b/src/cl_kernel.c
index 49bbaf0..66d7344 100644
--- a/src/cl_kernel.c
+++ b/src/cl_kernel.c
@@ -435,7 +435,8 @@ cl_kernel_setup(cl_kernel k, gbe_kernel opaque)
   k->opaque = opaque;
 
   const char* kname = cl_kernel_get_name(k);
-  if (strncmp(kname, "block_motion_estimate_intel", sizeof("block_motion_estimate_intel")) == 0)
+  if (kname != NULL &&
+      strncmp(kname, "block_motion_estimate_intel", sizeof("block_motion_estimate_intel")) == 0)
     k->vme = 1;
   else
     k->vme = 0;
diff --git a/src/cl_program.c b/src/cl_program.c
index 3be31bd..79fcf84 100644
--- a/src/cl_program.c
+++ b/src/cl_program.c
@@ -413,7 +413,7 @@ cl_program_create_with_built_in_kernles(cl_context     ctx,
       for (i = 0; i < ctx->built_in_prgs->ker_n; ++i) {
         assert(ctx->built_in_prgs->ker[i]);
         const char *ker_name = cl_kernel_get_name(ctx->built_in_prgs->ker[i]);
-        if (strcmp(ker_name, kernel) == 0) {
+        if (ker_name != NULL && strcmp(ker_name, kernel) == 0) {
           break;
         }
       }
@@ -539,7 +539,7 @@ static int check_cl_version_option(cl_program p, const char* options) {
   const char* s = NULL;
   int ver1 = 0;
   int ver2 = 0;
-  char version_str[64];
+  char version_str[64] = {0};
 
   if (options && (s = strstr(options, "-cl-std="))) {
 
@@ -945,7 +945,7 @@ cl_program_create_kernel(cl_program p, const char *name, cl_int *errcode_ret)
   for (i = 0; i < p->ker_n; ++i) {
     assert(p->ker[i]);
     const char *ker_name = cl_kernel_get_name(p->ker[i]);
-    if (strcmp(ker_name, name) == 0) {
+    if (ker_name != NULL && strcmp(ker_name, name) == 0) {
       from = p->ker[i];
       break;
     }
@@ -1004,10 +1004,13 @@ cl_program_get_kernel_names(cl_program p, size_t size, char *names, size_t *size
     return;
   }
 
-  ker_name = cl_kernel_get_name(p->ker[i]);
-  len = strlen(ker_name);
-  if(names) {
-    strncpy(names, cl_kernel_get_name(p->ker[0]), size - 1);
+  ker_name = cl_kernel_get_name(p->ker[0]);
+  if (ker_name != NULL)
+    len = strlen(ker_name);
+  else
+    len = 0;
+  if(names && ker_name) {
+    strncpy(names, ker_name, size - 1);
     names[size - 1] = '\0';
     if(size < len - 1) {
       if(size_ret) *size_ret = size;
@@ -1015,12 +1018,15 @@ cl_program_get_kernel_names(cl_program p, size_t size, char *names, size_t *size
     }
     size = size - len - 1;  //sub \0
   }
-  if(size_ret) *size_ret = strlen(ker_name) + 1;  //add NULL
+  if(size_ret) *size_ret = len + 1;  //add NULL
 
   for (i = 1; i < p->ker_n; ++i) {
     ker_name = cl_kernel_get_name(p->ker[i]);
-    len = strlen(ker_name);
-    if(names) {
+    if (ker_name != NULL)
+      len = strlen(ker_name);
+    else
+      len = 0;
+    if(names && ker_name) {
       strncat(names, ";", size);
       if(size >= 1)
         strncat(names, ker_name, size - 1);
diff --git a/src/performance.c b/src/performance.c
index 28bd6c6..1e676c3 100644
--- a/src/performance.c
+++ b/src/performance.c
@@ -54,6 +54,8 @@ static context_storage_node * find_context(cl_context context)
   if(NULL == record.context_storage)
   {
     record.context_storage = (context_storage_node *) malloc(sizeof(context_storage_node));
+    if (record.context_storage == NULL)
+      return NULL;
     record.context_storage->context_id = (uintptr_t)context;
     record.context_storage->kernels_storage = NULL;
     record.context_storage->kernel_max_time = 0.0f;
@@ -96,6 +98,8 @@ static kernel_storage_node * find_kernel(context_storage_node *p_context, const
   if(NULL == p_context->kernels_storage)
   {
     p_context->kernels_storage = (kernel_storage_node *)malloc(sizeof(kernel_storage_node));
+    if (p_context->kernels_storage == NULL)
+      return NULL;
     p_context->kernel_count++;
     strncpy(p_context->kernels_storage->kernel_name,kernel_name, MAX_KERNEL_NAME_LENGTH);
     p_context->kernels_storage->kernel_name[MAX_KERNEL_NAME_LENGTH - 1] = '\0';
@@ -188,6 +192,8 @@ static void print_time_info()
     kernel_storage_node *p_kernel = p_context->kernels_storage;
     kernel_storage_node *p_tmp_kernel = p_kernel;
     time_element *te = (time_element *)malloc(sizeof(time_element)*p_context->kernel_count);
+    if (te == NULL)
+      return;
     memset(te, 0, sizeof(time_element)*p_context->kernel_count);
     int i = -1, j = 0, k = 0;
     while(NULL != p_tmp_kernel)
-- 
2.1.4



More information about the Beignet mailing list