From: David van der Spoel Date: Fri, 15 Jun 2012 11:47:07 +0000 (+0200) Subject: Fixes #882 - looping bug in trxio.c X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=effe8e432cdce867aaa0819bd5857f21409a0ed3;p=alexxy%2Fgromacs.git Fixes #882 - looping bug in trxio.c This led to infinite output files in the case of a corrupt input file. Change-Id: I25d61752d901012e9ce5e6adae1679c2ef99467a --- diff --git a/include/gmxfio.h b/include/gmxfio.h index da5acaa199..ef56ebc8a1 100644 --- a/include/gmxfio.h +++ b/include/gmxfio.h @@ -218,7 +218,7 @@ int gmx_fio_get_file_md5(t_fileio *fio, gmx_off_t offset, int xtc_seek_frame(t_fileio *fio, int frame, int natoms); -int xtc_seek_time(t_fileio *fio, real time, int natoms); +int xtc_seek_time(t_fileio *fio, real time, int natoms,gmx_bool bSeekForwardOnly); /* Add this to the comment string for debugging */ diff --git a/include/xdrf.h b/include/xdrf.h index a97644dee0..5b89fa9f81 100644 --- a/include/xdrf.h +++ b/include/xdrf.h @@ -102,7 +102,7 @@ int xdr_gmx_large_int(XDR *xdrs,gmx_large_int_t *i,const char *warn); float xdr_xtc_estimate_dt(FILE *fp, XDR *xdrs, int natoms, gmx_bool * bOK); -int xdr_xtc_seek_time(real time, FILE *fp, XDR *xdrs, int natoms); + int xdr_xtc_seek_time(real time, FILE *fp, XDR *xdrs, int natoms,gmx_bool bSeekForwardOnly); int xdr_xtc_seek_frame(int frame, FILE *fp, XDR *xdrs, int natoms); diff --git a/src/gmxlib/gmxfio.c b/src/gmxlib/gmxfio.c index 8487067b44..bd54b08ecc 100644 --- a/src/gmxlib/gmxfio.c +++ b/src/gmxlib/gmxfio.c @@ -1145,12 +1145,12 @@ int xtc_seek_frame(t_fileio *fio, int frame, int natoms) return ret; } -int xtc_seek_time(t_fileio *fio, real time, int natoms) +int xtc_seek_time(t_fileio *fio, real time, int natoms,gmx_bool bSeekForwardOnly) { int ret; gmx_fio_lock(fio); - ret=xdr_xtc_seek_time(time, fio->fp, fio->xdr, natoms); + ret=xdr_xtc_seek_time(time, fio->fp, fio->xdr, natoms, bSeekForwardOnly); gmx_fio_unlock(fio); return ret; diff --git a/src/gmxlib/libxdrf.c b/src/gmxlib/libxdrf.c index 947a41fe3b..4baffe77b9 100644 --- a/src/gmxlib/libxdrf.c +++ b/src/gmxlib/libxdrf.c @@ -1683,7 +1683,7 @@ xdr_xtc_seek_frame(int frame, FILE *fp, XDR *xdrs, int natoms) -int xdr_xtc_seek_time(real time, FILE *fp, XDR *xdrs, int natoms) +int xdr_xtc_seek_time(real time, FILE *fp, XDR *xdrs, int natoms,gmx_bool bSeekForwardOnly) { float t; float dt; @@ -1693,6 +1693,10 @@ int xdr_xtc_seek_time(real time, FILE *fp, XDR *xdrs, int natoms) int res; int dt_sign = 0; + if (bSeekForwardOnly) + { + low = gmx_ftell(fp); + } if (gmx_fseek(fp,0,SEEK_END)) { return -1; @@ -1705,7 +1709,7 @@ int xdr_xtc_seek_time(real time, FILE *fp, XDR *xdrs, int natoms) /* round to int */ high /= XDR_INT_SIZE; high *= XDR_INT_SIZE; - offset = ((high / 2) / XDR_INT_SIZE) * XDR_INT_SIZE; + offset = (((high-low) / 2) / XDR_INT_SIZE) * XDR_INT_SIZE; if (gmx_fseek(fp,offset,SEEK_SET)) { diff --git a/src/gmxlib/trxio.c b/src/gmxlib/trxio.c index 9874339d3e..d9db7f0f50 100644 --- a/src/gmxlib/trxio.c +++ b/src/gmxlib/trxio.c @@ -712,8 +712,9 @@ gmx_bool read_next_frame(const output_env_t oenv,t_trxstatus *status,t_trxframe * accuracy of the control over -b and -e options. */ if (bTimeSet(TBEGIN) && (fr->time < rTimeValue(TBEGIN))) { - if (xtc_seek_time(status->fio, rTimeValue(TBEGIN),fr->natoms)) { - gmx_fatal(FARGS,"Specified frame doesn't exist or file not seekable"); + if (xtc_seek_time(status->fio, rTimeValue(TBEGIN),fr->natoms,TRUE)) { + gmx_fatal(FARGS,"Specified frame (time %f) doesn't exist or file corrupt/inconsistent.", + rTimeValue(TBEGIN)); } initcount(status); } diff --git a/src/tools/gmx_trjcat.c b/src/tools/gmx_trjcat.c index 2ca470dc48..3f5296a141 100644 --- a/src/tools/gmx_trjcat.c +++ b/src/tools/gmx_trjcat.c @@ -684,7 +684,7 @@ int gmx_trjcat(int argc, char *argv[]) { searchtime = last_frame_time; } - if (xtc_seek_time(stfio,searchtime,fr.natoms)) + if (xtc_seek_time(stfio,searchtime,fr.natoms,TRUE)) { gmx_fatal(FARGS,"Error seeking to append position."); }