From cf5d80b2ff3e8f594105a92359e711eff288a4ea Mon Sep 17 00:00:00 2001 From: David van der Spoel Date: Thu, 8 Jul 2010 13:58:37 +0200 Subject: [PATCH] Removed possible SEGVs in rmpbc in case ePBC = No. The code in matio.c made me cry. How about this: char *line; snew(line,100); line++; sfree(line); This is nowadays detected by glibc. To avoid drastic changes I removed the call to free, introducing a memory leak, but this code is used only in non-critical xpm2ps AFAIK. --- src/gmxlib/matio.c | 25 +++++++++++-------------- src/gmxlib/rmpbc.c | 20 ++++++++++++-------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/gmxlib/matio.c b/src/gmxlib/matio.c index 7ad68c1225..20971f91e8 100644 --- a/src/gmxlib/matio.c +++ b/src/gmxlib/matio.c @@ -191,14 +191,9 @@ static char *fgetline(char **line,int llmax,int *llalloc,FILE *in) { char *fg; - if (!(*line)) + if (llmax > *llalloc) { - snew(*line, llmax); - *llalloc=llmax; - } - else if (llmax > *llalloc) - { - srenew(*line,llmax); + srenew(*line,llmax+1); *llalloc=llmax; } fg=fgets(*line,llmax,in); @@ -272,7 +267,8 @@ void read_xpm_entry(FILE *in,t_matrix *mm) llmax = STRLEN; - while (fgetline(&line,llmax,&llalloc,in) && (strncmp(line,"static",6) != 0)) { + while ((NULL != fgetline(&line,llmax,&llalloc,in)) && + (strncmp(line,"static",6) != 0)) { parsestring(line,"title",(mm->title)); parsestring(line,"legend",(mm->legend)); parsestring(line,"x-label",(mm->label_x)); @@ -290,7 +286,7 @@ void read_xpm_entry(FILE *in,t_matrix *mm) gmx_input("Invalid XPixMap"); /* Read sizes */ bGetOnWithIt=FALSE; - while (!bGetOnWithIt && fgetline(&line,llmax,&llalloc,in)) { + while (!bGetOnWithIt && (NULL != fgetline(&line,llmax,&llalloc,in))) { while (( line[0] != '\"' ) && ( line[0] != '\0' )) line++; @@ -310,7 +306,7 @@ void read_xpm_entry(FILE *in,t_matrix *mm) /* Read color map */ snew(map,mm->nmap); m=0; - while ((m < mm->nmap) && fgetline(&line,llmax,&llalloc,in)) { + while ((m < mm->nmap) && (NULL != fgetline(&line,llmax,&llalloc,in))) { line=strchr(line,'\"'); if (line) { line++; @@ -398,7 +394,7 @@ void read_xpm_entry(FILE *in,t_matrix *mm) skipstr(&line); } } - } while ((line[0] != '\"') && fgetline(&line,llmax,&llalloc,in)); + } while ((line[0] != '\"') && (NULL != fgetline(&line,llmax,&llalloc,in))); /* Read matrix */ snew(mm->matrix,mm->nx); @@ -424,11 +420,12 @@ void read_xpm_entry(FILE *in,t_matrix *mm) } m--; } - } while ((m>=0) && fgetline(&line,llmax,&llalloc,in)); + } while ((m>=0) && (NULL != fgetline(&line,llmax,&llalloc,in))); if (m>=0) gmx_incons("Not enough rows in the matrix"); - sfree(line); + /* This code makes me cry. DvdS 2010-07-08 */ + /*sfree(line);*/ } int read_xpm_matrix(const char *fnm,t_matrix **matrix) @@ -441,7 +438,7 @@ int read_xpm_matrix(const char *fnm,t_matrix **matrix) in=gmx_fio_fopen(fnm,"r"); nmat=0; - while (fgetline(&line,STRLEN,&llalloc,in)) { + while (NULL != fgetline(&line,STRLEN,&llalloc,in)) { if (strstr(line,"/* XPM */")) { srenew(*matrix,nmat+1); read_xpm_entry(in,&(*matrix)[nmat]); diff --git a/src/gmxlib/rmpbc.c b/src/gmxlib/rmpbc.c index 33e543b410..da6c9d0e61 100644 --- a/src/gmxlib/rmpbc.c +++ b/src/gmxlib/rmpbc.c @@ -65,28 +65,32 @@ gmx_rmpbc_t gmx_rmpbc_init(t_idef *idef,int ePBC,int natoms, else gpbc->ePBC = ePBC; - if ((ePBC != epbcNONE) && (idef->ntypes!=-1)) { - gpbc->gr = mk_graph(NULL,idef,0,natoms,FALSE,FALSE); - } - else { + if (idef->ntypes <= 0) fprintf(stderr, "\nWarning: if there are broken molecules in the trajectory file,\n" " they can not be made whole without a run input file\n\n"); - } + else if (ePBC == epbcNONE) + fprintf(stderr,"\nNot treating periodicity since it is turned off in the input file\n"); + else + gpbc->gr = mk_graph(NULL,idef,0,natoms,FALSE,FALSE); + return gpbc; } void gmx_rmpbc_done(gmx_rmpbc_t gpbc) { - done_graph(gpbc->gr); + if (NULL != gpbc->gr) + done_graph(gpbc->gr); } void gmx_rmpbc(gmx_rmpbc_t gpbc,matrix box,rvec x[],rvec x_s[]) { int i; - mk_mshift(stdout,gpbc->gr,gpbc->ePBC,box,x); - shift_x(gpbc->gr,box,x,x_s); + if (NULL != gpbc->gr) { + mk_mshift(stdout,gpbc->gr,gpbc->ePBC,box,x); + shift_x(gpbc->gr,box,x,x_s); + } if (x != x_s) for (i=0; inatoms; i++) copy_rvec(x[i],x_s[i]); -- 2.11.4.GIT