From d6270e102f46fb39975b8b182922acca5d6695dd Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Tue, 30 Jan 2018 20:05:37 +0100
Subject: [PATCH] Sonarqube bug fixes

---
 control/descriptor.c                        |  1 +
 control/workspace.c                         |  2 +-
 runtime/parsec/control/runtime_descriptor.c |  3 ++-
 runtime/starpu/control/runtime_profiling.c  | 11 ++++++-----
 testing/testing_zgemm.c                     |  4 ++--
 timing/timing.c                             |  5 ++++-
 6 files changed, 16 insertions(+), 10 deletions(-)

diff --git a/control/descriptor.c b/control/descriptor.c
index 245932989..6cf9be548 100644
--- a/control/descriptor.c
+++ b/control/descriptor.c
@@ -430,6 +430,7 @@ int MORSE_Desc_Create(MORSE_desc_t **descptr, void *mat, MORSE_enum dtyp, int mb
 
         if ((desc->mat = RUNTIME_malloc(size)) == NULL) {
             morse_error("MORSE_Desc_Create", "malloc() failed");
+            free(desc);
             return MORSE_ERR_OUT_OF_RESOURCES;
         }
         desc->use_mat      = 1;
diff --git a/control/workspace.c b/control/workspace.c
index 931577f2a..e5c04088c 100644
--- a/control/workspace.c
+++ b/control/workspace.c
@@ -142,7 +142,7 @@ int morse_alloc_ipiv(int M, int N, MORSE_enum func, int type, MORSE_desc_t **des
     ln = NB * NT;
 
     size = (size_t)(chameleon_min(MT, NT) * NB * NT * sizeof(int));
-    if (size <= 0) {
+    if (size == 0) {
         *IPIV = NULL;
         return MORSE_SUCCESS;
     }
diff --git a/runtime/parsec/control/runtime_descriptor.c b/runtime/parsec/control/runtime_descriptor.c
index 8f01af0a5..12ac813e8 100644
--- a/runtime/parsec/control/runtime_descriptor.c
+++ b/runtime/parsec/control/runtime_descriptor.c
@@ -188,7 +188,8 @@ morse_parsec_key_to_string(parsec_data_collection_t *data_collection, parsec_dat
     res = snprintf(buffer, buffer_size, "(%d, %d)", m, n);
     if (res < 0)
     {
-        printf("error in key_to_string for tile (%u, %u) key: %u\n", m, n, datakey);
+        printf("error in key_to_string for tile (%u, %u) key: %u\n",
+               (unsigned int)m, (unsigned int)n, datakey);
     }
     return res;
 }
diff --git a/runtime/starpu/control/runtime_profiling.c b/runtime/starpu/control/runtime_profiling.c
index 9c2d395a9..787ae481d 100644
--- a/runtime/starpu/control/runtime_profiling.c
+++ b/runtime/starpu/control/runtime_profiling.c
@@ -25,10 +25,10 @@
 #endif
 
 #ifdef CHAMELEON_ENABLE_PRUNING_STATS
-unsigned long RUNTIME_total_tasks;
-unsigned long RUNTIME_exec_tasks;
-unsigned long RUNTIME_comm_tasks;
-unsigned long RUNTIME_changed_tasks;
+unsigned long RUNTIME_total_tasks   = 0;
+unsigned long RUNTIME_exec_tasks    = 0;
+unsigned long RUNTIME_comm_tasks    = 0;
+unsigned long RUNTIME_changed_tasks = 0;
 #endif
 
 double RUNTIME_get_time(){
@@ -81,7 +81,8 @@ void RUNTIME_start_stats(){
 
 void RUNTIME_stop_stats(){
 #ifdef CHAMELEON_ENABLE_PRUNING_STATS
-    fprintf(stderr, "\ntasks: %u = exec: %u + comm: %u + changed: %u\n", RUNTIME_total_tasks, RUNTIME_exec_tasks, RUNTIME_comm_tasks, RUNTIME_changed_tasks);
+    fprintf( stderr, "\ntasks: %ul = exec: %ul + comm: %ul + changed: %ul\n",
+             RUNTIME_total_tasks, RUNTIME_exec_tasks, RUNTIME_comm_tasks, RUNTIME_changed_tasks );
 #endif
 }
 
diff --git a/testing/testing_zgemm.c b/testing/testing_zgemm.c
index e10da200a..d676f8717 100644
--- a/testing/testing_zgemm.c
+++ b/testing/testing_zgemm.c
@@ -88,9 +88,9 @@ int testing_zgemm(int argc, char **argv)
     MORSE_Complex64_t *Cfinal = (MORSE_Complex64_t *)malloc(LDCxN*sizeof(MORSE_Complex64_t));
 
     /* Check if unable to allocate memory */
-    if ( (!A) || (!B) || (!Cinit) || (!Cfinal) )
+    if ( (!A) || (!B) || (!C) || (!Cinit) || (!Cfinal) )
     {
-        free(A); free(B);
+        free(A); free(B); free(C);
         free(Cinit); free(Cfinal);
         printf("Out of Memory \n ");
         return -2;
diff --git a/timing/timing.c b/timing/timing.c
index e928035c2..36576de17 100644
--- a/timing/timing.c
+++ b/timing/timing.c
@@ -170,7 +170,10 @@ Test(int64_t n, int *iparam) {
 
     if ( iparam[IPARAM_WARMUP] ) {
       int status = RunTest( iparam, dparam, &(t[0]));
-      if (status != MORSE_SUCCESS) return status;
+      if (status != MORSE_SUCCESS) {
+          free(t);
+          return status;
+      }
     }
 
     sumgf  = 0.0;
-- 
GitLab