Resolving a couple of clang analyzer warnings
authorRoland Schulz <roland@utk.edu>
Sun, 11 Mar 2012 11:18:19 +0000 (07:18 -0400)
committerRoland Schulz <roland@utk.edu>
Sun, 22 Apr 2012 16:44:28 +0000 (12:44 -0400)
Most of these are real errors or potential problems.

It's only a small start ~100 warnings are left and quite a few
false positives are within those remaining ones.

Change-Id: I7c83afa84c52362ecc1b7c992591d82f8262a6ac

18 files changed:
include/gmx_fatal.h
include/types/commrec.h
src/gmxlib/futil.c
src/gmxlib/index.c
src/gmxlib/matio.c
src/gmxlib/mshift.c
src/gmxlib/oenv.c
src/gmxlib/statutil.c
src/gmxlib/tpxio.c
src/gmxlib/vmdio.c
src/kernel/topio.c
src/kernel/xutils.c
src/mdlib/coupling.c
src/mdlib/domdec.c
src/mdlib/fft5d.c
src/mdlib/pme.c
src/tools/expfit.c
src/tools/gmx_genpr.c

index 8108af79124ee4aa2597d883fed5f73d1b68e8d8..df1bda65b5af0c004a9e3fe626418739bfb2e53e 100644 (file)
 #ifdef __cplusplus
 extern "C" {
 #endif
+
+#ifndef __has_feature      // Optional.
+#define __has_feature(x) 0 // Compatibility with non-clang compilers.
+#endif
+
+/** \def GMX_ATTRIBUTE_NORETURN \brief Indicate that a function is not
+ * expected to return.
+ * WARNING: In general this flag should not be used for compiler
+ * optimizations, since set_gmx_error_handler can be set to a
+ * handler which does not quit.
+ */
+#ifndef GMX_ATTRIBUTE_NORETURN
+#if __has_feature(attribute_analyzer_noreturn)
+#define GMX_ATTRIBUTE_NORETURN __attribute__((analyzer_noreturn))
+#else
+#define GMX_ATTRIBUTE_NORETURN
+#endif
+#endif
   
 void 
 _where(const char *file,int line);
@@ -62,7 +80,7 @@ _unset_fatal_tmp_file(const char *fn, const char *file, int line);
 /* unsets filename to be removed */
 
 void 
-gmx_fatal(int fatal_errno,const char *file,int line,const char *fmt,...);
+gmx_fatal(int fatal_errno,const char *file,int line,const char *fmt,...) GMX_ATTRIBUTE_NORETURN;
 #define FARGS 0,__FILE__,__LINE__
 /*
  * Routine gmx_fatal prints 
@@ -84,7 +102,7 @@ gmx_fatal(int fatal_errno,const char *file,int line,const char *fmt,...);
 void
 gmx_fatal_collective(int f_errno,const char *file,int line,
                     t_commrec *cr,gmx_domdec_t *dd,
-                    const char *fmt,...);
+                    const char *fmt,...) GMX_ATTRIBUTE_NORETURN;
 /* As gmx_fatal, but only the master process prints the error message.
  * This should only be called one of the following two situations:
  * 1) On all nodes in cr->mpi_comm_mysim, with cr!=NULL,dd==NULL.
@@ -149,7 +167,7 @@ void doexceptions(void);
    * The messages are stored in src/gmxlib/fatal.c
    */
   
-  void _gmx_error(const char *key,const char *msg,const char *file,int line);
+  void _gmx_error(const char *key,const char *msg,const char *file,int line) GMX_ATTRIBUTE_NORETURN;
 #define gmx_error(key,msg) _gmx_error(key,msg,__FILE__,__LINE__)
   /* Error msg of type key is generated and the program is 
    * terminated unless and error handle is set (see below)
index 62136607be2d26030b2f984f5c4c8881c6283c15..0c9d320b08dc8a8addb389ed0ceebc30c6359143 100644 (file)
@@ -282,7 +282,7 @@ typedef struct {
   mpi_in_place_buf_t *mpb;
 } t_commrec;
 
-#define MASTERNODE(cr)     ((cr)->nodeid == 0)
+#define MASTERNODE(cr)     (((cr)->nodeid == 0) || !PAR(cr))
   /* #define MASTERTHREAD(cr)   ((cr)->threadid == 0) */
   /* #define MASTER(cr)         (MASTERNODE(cr) && MASTERTHREAD(cr)) */
 #define MASTER(cr)         MASTERNODE(cr)
@@ -294,7 +294,7 @@ typedef struct {
 #define RANK(cr,nodeid)    (nodeid)
 #define MASTERRANK(cr)     (0)
 
-#define DOMAINDECOMP(cr)   ((cr)->dd != NULL)
+#define DOMAINDECOMP(cr)   (((cr)->dd != NULL) && PAR(cr))
 #define DDMASTER(dd)       ((dd)->rank == (dd)->masterrank)
 
 #define PARTDECOMP(cr)     ((cr)->pd != NULL)
index 45b76e2703508d49dd0f5d8e796c6333c453f380..98620b8ecf245acca255ea2a0797405a0da657c7 100644 (file)
@@ -168,7 +168,7 @@ int ffclose(FILE *fp)
     else {
         while ((ps->prev != NULL) && (ps->prev->fp != fp))
             ps=ps->prev;
-        if (ps->prev->fp == fp) {
+        if ((ps->prev != NULL) && ps->prev->fp == fp) {
             if (ps->prev->fp != NULL)
                 ret = pclose(ps->prev->fp);
             tmp=ps->prev;
index acdf06b8d2646b95de51bd832c901d8329784a56..158a2ea85c58c954e1a2e64610c8e45fa81fa57c 100644 (file)
@@ -920,7 +920,7 @@ t_blocka *init_index(const char *gfile, char ***grpname)
        (*grpname)[b->nr-1]=strdup(str);
       } else {
        pt=line;
-       while ((i=sscanf(pt,"%s",str)) == 1) {
+       while (sscanf(pt,"%s",str) == 1) {
          i=b->index[b->nr];
          if (i>=maxentries) {
            maxentries+=1024;
index 4db3227210543b4a87c384fbb63f52240865cc32..ebfccd4526f2202b76bbdc148828f30023cedbc3 100644 (file)
@@ -242,7 +242,7 @@ void parsestring(char *line,const char *label, char *string)
 void read_xpm_entry(FILE *in,t_matrix *mm)
 {
   t_mapping *map;
-  char *line_buf=NULL,*line=NULL,*str,buf[256];
+  char *line_buf=NULL,*line=NULL,*str,buf[256]={0};
   int i,m,col_len,nch,n_axis_x,n_axis_y,llmax;
   int llalloc=0;
   unsigned int r,g,b;
@@ -271,6 +271,12 @@ void read_xpm_entry(FILE *in,t_matrix *mm)
     parsestring(line,"y-label",(mm->label_y));
     parsestring(line,"type",buf);
   }
+
+  if (!line || strncmp(line,"static",6) != 0)
+  {
+      gmx_input("Invalid XPixMap");
+  }
+
   if (buf[0] && (gmx_strcasecmp(buf,"Discrete")==0))
     mm->bDiscrete=TRUE;
    
@@ -278,8 +284,6 @@ void read_xpm_entry(FILE *in,t_matrix *mm)
     fprintf(debug,"%s %s %s %s\n",
            mm->title,mm->legend,mm->label_x,mm->label_y);
 
-  if  (strncmp(line,"static",6) != 0)
-    gmx_input("Invalid XPixMap");
   /* Read sizes */
   bGetOnWithIt=FALSE;
   while (!bGetOnWithIt && (NULL != fgetline(&line_buf,llmax,&llalloc,in))) {
@@ -291,7 +295,13 @@ void read_xpm_entry(FILE *in,t_matrix *mm)
       line2string(&line);
       sscanf(line,"%d %d %d %d",&(mm->nx),&(mm->ny),&(mm->nmap),&nch);
       if (nch > 2)
-       gmx_fatal(FARGS,"Sorry can only read xpm's with at most 2 caracters per pixel\n");
+      {
+          gmx_fatal(FARGS,"Sorry can only read xpm's with at most 2 caracters per pixel\n");
+      }
+      if (mm->nx <= 0 || mm->ny <= 0 )
+      {
+          gmx_fatal(FARGS,"Dimensions of xpm-file have to be larger than 0\n");
+      }
       llmax = max(STRLEN,mm->nx+10);
       bGetOnWithIt=TRUE;
     }
index 5bb304f1945e2ac2a0924de6b79dfc2c1a37f465..17f057418c21fecd0d24b4891d35e18523af1531 100644 (file)
@@ -129,7 +129,7 @@ static void mk_igraph(t_graph *g,int ftype,t_ilist *il,
   }
 }
 
-static void g_error(int line,const char *file)
+GMX_ATTRIBUTE_NORETURN static void g_error(int line,const char *file) 
 {
   gmx_fatal(FARGS,"Tring to print non existant graph (file %s, line %d)",
              file,line);
index 4e01d25ffe8c2dd763eac20eca837c6ae7885ca2..aea00e0eb2875e7c57d4923e8c6ac594a7551f1f 100644 (file)
@@ -130,10 +130,13 @@ void output_env_init(output_env_t oenv,  int argc, char *argv[],
         
     /* Fill the cmdline string */
     snew(oenv->cmd_line,cmdlength+argc+1);
-    for (i=0; i<argc; i++) 
+    if (argv)
     {
-        strcat(oenv->cmd_line,argv[i]);
-        strcat(oenv->cmd_line," ");
+        for (i=0; i<argc; i++)
+        {
+            strcat(oenv->cmd_line,argv[i]);
+            strcat(oenv->cmd_line," ");
+        }
     }
 }
 
index 589cbea166dc821402331289478b248c5e2928de..0144ba9696a2015fbf5d1f318dae1b3c2e890110 100644 (file)
@@ -39,6 +39,7 @@
 
 
 #include <ctype.h>
+#include <assert.h>
 #include "copyrite.h"
 #include "sysstuff.h"
 #include "macros.h"
@@ -334,8 +335,8 @@ t_topology *read_top(const char *fn,int *ePBC)
 
 static void usage(const char *type,const char *arg)
 {
-    if (arg != NULL)
-        gmx_fatal(FARGS,"Expected %s argument for option %s\n",type,arg);
+    assert(arg);
+    gmx_fatal(FARGS,"Expected %s argument for option %s\n",type,arg);
 }
 
 int iscan(int argc,char *argv[],int *i)
index b6c782c5875cc253db32c7dd19e1e129f4c8dd4e..8d7d3f953da1e822dd2b4be9111681ed606c92f8 100644 (file)
@@ -2515,7 +2515,7 @@ gmx_bool read_tps_conf(const char *infile,char *title,t_topology *top,int *ePBC,
 {
   t_tpxheader  header;
   int          natoms,i,version,generation;
-  gmx_bool         bTop,bXNULL;
+  gmx_bool         bTop,bXNULL=FALSE;
   gmx_mtop_t   *mtop;
   t_topology   *topconv;
   gmx_atomprop_t aps;
@@ -2539,14 +2539,19 @@ gmx_bool read_tps_conf(const char *infile,char *title,t_topology *top,int *ePBC,
   else {
     get_stx_coordnum(infile,&natoms);
     init_t_atoms(&top->atoms,natoms,FALSE);
-    bXNULL = (x == NULL);
+    if (x == NULL)
+    {
+        snew(x,1);
+        bXNULL = TRUE;
+    }
     snew(*x,natoms);
     if (v)
       snew(*v,natoms);
     read_stx_conf(infile,title,&top->atoms,*x,(v==NULL) ? NULL : *v,ePBC,box);
-    if (bXNULL) {
+    if (bXNULL)
+    {
       sfree(*x);
-      x = NULL;
+      sfree(x);
     }
     if (bMass) {
       aps = gmx_atomprop_init();
index 65f962e4ff868b53d66237b6e453bfe8d4917a9e..9dad9e0f5a7b20538a0c7debde08a3478ac81a72 100644 (file)
@@ -73,6 +73,7 @@ OTHER DEALINGS WITH THE SOFTWARE.
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
 /* 
  * Plugin header files; get plugin source from www.ks.uiuc.edu/Research/vmd"
@@ -393,6 +394,7 @@ int read_first_vmd_frame(int *status,const char *fn,t_trxframe *fr,int flags)
     if (fr->vmdplugin.api->abiversion > 10 && fr->vmdplugin.api->read_timestep_metadata)
     {
         fr->vmdplugin.api->read_timestep_metadata(fr->vmdplugin.handle, metadata);
+        assert(metadata);
         fr->vmdplugin.bV = metadata->has_velocities; 
         if (fr->vmdplugin.bV)
         {
index e42c19a74b0ffd05e0d263d317468040e8a3f317..37602df41fa5a65cc7117db51800f39c2b7321b6 100644 (file)
@@ -43,6 +43,8 @@
 #include <string.h>
 #include <errno.h>
 #include <ctype.h>
+#include <assert.h>
+
 #include "futil.h"
 #include "sysstuff.h"
 #include "typedefs.h"
@@ -826,6 +828,7 @@ static char **read_topol(const char *infile,const char *outfile,
           push_vsitesn(d,plist,mi0->plist,&(mi0->atoms),atype,pline,wi);
            break;
          case d_exclusions:
+           assert(block2);
            if (!block2[nmol-1].nr)
              init_block2(&(block2[nmol-1]),mi0->atoms.nr);
            push_excl(pline,&(block2[nmol-1]));
index aa7fa23f702fc6038ec8ad0c8668161d9dda2224..5093724770bb9587b46de926293b26d3bf56228a 100644 (file)
@@ -36,6 +36,8 @@
 #include <config.h>
 #endif
 
+#include <assert.h>
+
 #include "typedefs.h"
 #include "smalloc.h"
 #include "strdb.h"
@@ -219,6 +221,7 @@ static void update_ff(t_forcerec *fr,int nparm,t_range range[],int param_val[])
   
   if (fr->bBHAM) {
     if (bhama == NULL) {
+      assert(bhamb==NULL && bhamc==NULL);
       snew(bhama,atnr);
       snew(bhamb,atnr);
       snew(bhamc,atnr);
@@ -226,6 +229,7 @@ static void update_ff(t_forcerec *fr,int nparm,t_range range[],int param_val[])
   }
   else {
     if (sigma == NULL) {
+      assert(eps==NULL && c6==NULL && cn==NULL);
       snew(sigma,atnr);
       snew(eps,atnr);
       snew(c6,atnr);
@@ -242,18 +246,23 @@ static void update_ff(t_forcerec *fr,int nparm,t_range range[],int param_val[])
       fprintf(debug,"val = %g\n",val);
     switch (range[i].ptype) {
     case eseSIGMA:
+      assert(!fr->bBHAM);
       sigma[range[i].atype] = val;
       break;
     case eseEPSILON:
+      assert(!fr->bBHAM);
       eps[range[i].atype] = val;
       break;
     case eseBHAMA:
+      assert(fr->bBHAM);
       bhama[range[i].atype] = val;
       break;
     case eseBHAMB:
+      assert(fr->bBHAM);
       bhamb[range[i].atype] = val;
       break;
     case eseBHAMC:
+      assert(fr->bBHAM);
       bhamc[range[i].atype] = val;
       break;
     case eseCELLX:
index 0e822326b89f2aaf18486b84ce167e7bbd4203a1..90815f04f02cf58cbfee90facd8549ea4fd2bc88 100644 (file)
@@ -662,7 +662,7 @@ void trotter_update(t_inputrec *ir,gmx_large_int_t step, gmx_ekindata_t *ekind,
     tensor dumpres,dumvir;
     double *scalefac,dtc;
     int *trotter_seq;
-    rvec sumv,consk;
+    rvec sumv={0,0,0},consk;
     gmx_bool bCouple;
 
     if (trotter_seqno <= ettTSEQ2)
index f26a124b856396bbdd62bcf543164be205a632e6..f42a47cfa50ece20afc81e8a7f8914e08345a8f6 100644 (file)
@@ -3814,8 +3814,8 @@ static void get_cg_distribution(FILE *fplog,gmx_large_int_t step,gmx_domdec_t *d
     ivec npulse;
     int  i,cg_gl;
     int  *ibuf,buf2[2] = { 0, 0 };
-    
-    if (DDMASTER(dd))
+    gmx_bool bMaster = DDMASTER(dd);
+    if (bMaster)
     {
         ma = dd->ma;
         
@@ -3850,7 +3850,7 @@ static void get_cg_distribution(FILE *fplog,gmx_large_int_t step,gmx_domdec_t *d
         srenew(dd->index_gl,dd->cg_nalloc);
         srenew(dd->cgindex,dd->cg_nalloc+1);
     }
-    if (DDMASTER(dd))
+    if (bMaster)
     {
         for(i=0; i<dd->nnodes; i++)
         {
index 82a0ba7c5133ba16604272f8c4fc5e5433ef9558..f070e6de3ccee67737edfd2efe3c8c992a7f5fdb 100644 (file)
@@ -366,6 +366,7 @@ fft5d_plan fft5d_plan_3d(int NG, int MG, int KG, MPI_Comm comm[2], int flags, t_
         free(N1); free(oN1); /*these are not used for this order*/
         free(K0); free(oK0); /*the rest is freed in destroy*/
     }
+    N[2]=pN[2]=-1; /*not used*/
     
     /*
       Difference between x-y-z regarding 2d decomposition is whether they are 
index db1e5874203dc9ae4e2794efd08fc2d3579e9147..62c04a1b17dcd308e3b59479ecbeccfe48505a62 100644 (file)
@@ -74,6 +74,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
+#include <assert.h>
 #include "typedefs.h"
 #include "txtdump.h"
 #include "vec.h"
@@ -3935,6 +3936,11 @@ int gmx_pme_do(gmx_pme_t pme,
     real *  fftgrid;
     t_complex * cfftgrid;
     int     thread;
+    gmx_bool bCalcEnerVir = flags & GMX_PME_CALC_ENER_VIR;
+    gmx_bool bCalcF = flags & GMX_PME_CALC_F;
+
+    assert(pme->nnodes > 0);
+    assert(pme->nnodes == 1 || pme->ndecompdim > 0);
 
     if (pme->nnodes > 1) {
         atc = &pme->atc[0];
@@ -4107,7 +4113,7 @@ int gmx_pme_do(gmx_pme_t pme,
                 loop_count =
                     solve_pme_yzx(pme,cfftgrid,ewaldcoeff,
                                   box[XX][XX]*box[YY][YY]*box[ZZ][ZZ],
-                                  flags & GMX_PME_CALC_ENER_VIR,
+                                  bCalcEnerVir,
                                   pme->nthread,thread);
                 if (thread == 0)
                 {
@@ -4118,7 +4124,7 @@ int gmx_pme_do(gmx_pme_t pme,
                 }
             }
 
-            if (flags & GMX_PME_CALC_F)
+            if (bCalcF)
             {
                 /* do 3d-invfft */
                 if (thread == 0)
@@ -4154,7 +4160,7 @@ int gmx_pme_do(gmx_pme_t pme,
          * With MPI we have to synchronize here before gmx_sum_qgrid_dd.
          */
 
-        if (flags & GMX_PME_CALC_F)
+        if (bCalcF)
         {
             /* distribute local grid to all nodes */
 #ifdef GMX_MPI
@@ -4195,7 +4201,7 @@ int gmx_pme_do(gmx_pme_t pme,
             wallcycle_stop(wcycle,ewcPME_SPREADGATHER);
         }
 
-        if (flags & GMX_PME_CALC_ENER_VIR)
+        if (bCalcEnerVir)
         {
             /* This should only be called on the master thread
              * and after the threads have synchronized.
@@ -4204,7 +4210,7 @@ int gmx_pme_do(gmx_pme_t pme,
         }
     } /* of q-loop */
 
-    if ((flags & GMX_PME_CALC_F) && pme->nnodes > 1) {
+    if (bCalcF && pme->nnodes > 1) {
         wallcycle_start(wcycle,ewcPME_REDISTXF);
         for(d=0; d<pme->ndecompdim; d++)
         {
@@ -4232,15 +4238,23 @@ int gmx_pme_do(gmx_pme_t pme,
     }
     where();
 
-    if (!pme->bFEP) {
-        *energy = energy_AB[0];
-        m_add(vir,vir_AB[0],vir);
-    } else {
-        *energy = (1.0-lambda)*energy_AB[0] + lambda*energy_AB[1];
-        *dvdlambda += energy_AB[1] - energy_AB[0];
-        for(i=0; i<DIM; i++)
-            for(j=0; j<DIM; j++)
-                vir[i][j] += (1.0-lambda)*vir_AB[0][i][j] + lambda*vir_AB[1][i][j];
+    if (bCalcEnerVir)
+    {
+        if (!pme->bFEP) {
+            *energy = energy_AB[0];
+            m_add(vir,vir_AB[0],vir);
+        } else {
+            *energy = (1.0-lambda)*energy_AB[0] + lambda*energy_AB[1];
+            *dvdlambda += energy_AB[1] - energy_AB[0];
+            for(i=0; i<DIM; i++)
+            {
+                for(j=0; j<DIM; j++)
+                {
+                    vir[i][j] += (1.0-lambda)*vir_AB[0][i][j] + 
+                        lambda*vir_AB[1][i][j];
+                }
+            }
+        }
     }
 
     if (debug)
index 8c14ceb1941b344f10aef71f24477c3aa57b3775..52874843aec6114197e0a710c0e1137d720d1fea 100644 (file)
@@ -510,8 +510,13 @@ real do_lmfit(int ndata,real c1[],real sig[],real dt,real x0[],
        xvgrclose(fp);
       }
     }
-    for(i=0;(i<nparm);i++)
-      fitparms[i] = parm[i];
+    if (fitparms)
+    {
+        for(i=0;(i<nparm);i++)
+        {
+            fitparms[i] = parm[i];
+        }
+    }
     sfree(parm);
     sfree(dparm);
   }
index 663adee5bc6780ef10d31cbbf29514f6f9b4ffaf..658d69c760ac40f48a6501c14ec035d15defab5a 100644 (file)
@@ -152,7 +152,7 @@ int gmx_genpr(int argc,char *argv[])
   }
   
   if (bFreeze) {
-    if (atoms && atoms->pdbinfo) 
+    if (!atoms || !atoms->pdbinfo)
       gmx_fatal(FARGS,"No B-factors in input file %s, use a pdb file next time.",
                xfn);