From 4fc17a3f65e1f16cf2be11d2f2a31b75afef0905 Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Tue, 6 Jun 2017 15:32:32 +0200
Subject: [PATCH] Fix bug reported by sonarcube

---
 drivers/iohb.c    |  6 +++++-
 drivers/readijv.c |  9 ++++++++-
 z_spm_norm.c      | 18 ++++++------------
 3 files changed, 19 insertions(+), 14 deletions(-)

diff --git a/drivers/iohb.c b/drivers/iohb.c
index e4afcb9f..c4aec8d7 100644
--- a/drivers/iohb.c
+++ b/drivers/iohb.c
@@ -592,12 +592,14 @@ int readHB_aux_double(const char* filename, const char AuxType, double b[])
     if (Nrhs <= 0)
     {
         fprintf(stderr, "Warn: Attempt to read auxillary vector(s) when none are present.\n");
+        fclose(in_file);
         return 0;
     }
     if (Rhstype[0] != 'F' )
     {
         fprintf(stderr,"Warn: Attempt to read auxillary vector(s) which are not stored in Full form.\n");
         fprintf(stderr,"       Rhs must be specified as full. \n");
+        fclose(in_file);
         return 0;
     }
 
@@ -615,10 +617,12 @@ int readHB_aux_double(const char* filename, const char AuxType, double b[])
 
     if ( AuxType == 'G' && Rhstype[1] != 'G' ) {
         fprintf(stderr, "Warn: Attempt to read auxillary Guess vector(s) when none are present.\n");
+        fclose(in_file);
         return 0;
     }
     if ( AuxType == 'X' && Rhstype[2] != 'X' ) {
         fprintf(stderr, "Warn: Attempt to read auxillary eXact solution vector(s) when none are present.\n");
+        fclose(in_file);
         return 0;
     }
 
@@ -1344,7 +1348,7 @@ int writeHB_mat_char(const char* filename, int M, int N,
     int Ptrperline, Ptrwidth, Indperline, Indwidth;
     int Rhsperline, Rhswidth, Rhsprec;
     char Rhsflag;
-    int Valperline=0, Valwidth, Valprec;
+    int Valperline=1, Valwidth, Valprec;
     char Valflag;           /* Indicates 'E','D', or 'F' float format */
     char pformat[16],iformat[16],vformat[19],rformat[19];
 
diff --git a/drivers/readijv.c b/drivers/readijv.c
index 65a6c8bc..0e619fdb 100644
--- a/drivers/readijv.c
+++ b/drivers/readijv.c
@@ -141,6 +141,7 @@ readIJV( const char   *dirname,
     if (iafile == NULL)
     {
         fprintf(stderr,"readijv: Cannot open the ia file (%s)\n", filename);
+        free(filename);
         return PASTIX_ERR_BADPARAMETER;
     }
 
@@ -150,6 +151,7 @@ readIJV( const char   *dirname,
     {
         fprintf(stderr,"readijv: Cannot open the ja file (%s)\n", filename);
         fclose(iafile);
+        free(filename);
         return PASTIX_ERR_BADPARAMETER;
     }
 
@@ -160,6 +162,7 @@ readIJV( const char   *dirname,
         fprintf(stderr,"readijv: Cannot open the ra file (%s)\n", filename);
         fclose(iafile);
         fclose(jafile);
+        free(filename);
         return PASTIX_ERR_BADPARAMETER;
     }
 
@@ -178,6 +181,10 @@ readIJV( const char   *dirname,
             ( 1 != fscanf(rafile,"%le\n", &temp3)) )
         {
             fprintf(stderr, "ERROR: reading matrix\n");
+            fclose(iafile);
+            fclose(jafile);
+            fclose(rafile);
+            free(filename);
             return PASTIX_ERR_IO;
         }
         *temprow = (pastix_int_t)temp1;
@@ -187,6 +194,6 @@ readIJV( const char   *dirname,
     fclose(iafile);
     fclose(jafile);
     fclose(rafile);
-
+    free(filename);
     return PASTIX_SUCCESS;
 }
diff --git a/z_spm_norm.c b/z_spm_norm.c
index 50419ded..72ed9075 100644
--- a/z_spm_norm.c
+++ b/z_spm_norm.c
@@ -184,8 +184,7 @@ z_spmInfNorm( const pastix_spm_t *spm )
                 sumrow[row] += cabs( valptr[i] );
 
                 /* Add the symmetric/hermitian part */
-                if ( ((spm->mtxtype == PastixHermitian) ||
-                      (spm->mtxtype == PastixSymmetric)) &&
+                if ( ( spm->mtxtype != PastixGeneral ) &&
                      ( row != col ) )
                 {
                     sumrow[col] += cabs( valptr[i] );
@@ -204,8 +203,7 @@ z_spmInfNorm( const pastix_spm_t *spm )
         }
 
         /* Add the symmetric/hermitian part */
-        if ( (spm->mtxtype == PastixHermitian) ||
-             (spm->mtxtype == PastixSymmetric) )
+        if ( spm->mtxtype != PastixGeneral )
         {
             for( row=0; row < spm->gN; row++ )
             {
@@ -228,8 +226,7 @@ z_spmInfNorm( const pastix_spm_t *spm )
         }
 
         /* Add the symmetric/hermitian part */
-        if ( (spm->mtxtype == PastixHermitian) ||
-             (spm->mtxtype == PastixSymmetric) )
+        if ( spm->mtxtype != PastixGeneral )
         {
             for(i=0; i < spm->nnz; i++)
             {
@@ -300,8 +297,7 @@ z_spmOneNorm( const pastix_spm_t *spm )
         }
 
         /* Add the symmetric/hermitian part */
-        if ( (spm->mtxtype == PastixHermitian) ||
-             (spm->mtxtype == PastixSymmetric) )
+        if ( spm->mtxtype != PastixGeneral )
         {
             for( col=0; col < spm->gN; col++ )
             {
@@ -325,8 +321,7 @@ z_spmOneNorm( const pastix_spm_t *spm )
                 sumcol[col] += cabs( valptr[i] );
 
                 /* Add the symmetric/hermitian part */
-                if ( ((spm->mtxtype == PastixHermitian) ||
-                      (spm->mtxtype == PastixSymmetric)) &&
+                if ( ( spm->mtxtype != PastixGeneral ) &&
                      ( row != col ) )
                 {
                     sumcol[row] += cabs( valptr[i] );
@@ -342,8 +337,7 @@ z_spmOneNorm( const pastix_spm_t *spm )
         }
 
         /* Add the symmetric/hermitian part */
-        if ( (spm->mtxtype == PastixHermitian) ||
-             (spm->mtxtype == PastixSymmetric) )
+        if ( spm->mtxtype != PastixGeneral )
         {
             for(i=0; i < spm->nnz; i++)
             {
-- 
GitLab