Fixes #882 - looping bug in trxio.c
authorDavid van der Spoel <spoel@xray.bmc.uu.se>
Fri, 15 Jun 2012 11:47:07 +0000 (13:47 +0200)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sat, 16 Jun 2012 00:10:20 +0000 (02:10 +0200)
This led to infinite output files in the case of a corrupt
input file.

Change-Id: I25d61752d901012e9ce5e6adae1679c2ef99467a

include/gmxfio.h
include/xdrf.h
src/gmxlib/gmxfio.c
src/gmxlib/libxdrf.c
src/gmxlib/trxio.c
src/tools/gmx_trjcat.c

index da5acaa199d93ab0f1155efeabf3253b9ae0cdae..ef56ebc8a166fa02c3dbd3edf7796bfc48bcd136 100644 (file)
@@ -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 */
index a97644dee04778eac93b1b2c32debfc80887465a..5b89fa9f812c57f424302fc2ce3537009c8ac387 100644 (file)
@@ -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);
index 8487067b445749849c2b160874904b46e8a38505..bd54b08ecc7903932193be7272ce7ffdcc16467f 100644 (file)
@@ -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;
index 947a41fe3bdce9cb050fb09bced026b9a0ba532a..4baffe77b90ed9ae7864bc52a7a95510866bc9c1 100644 (file)
@@ -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))
     {
index 9874339d3e61adfcccf238f1721ea29573d11b68..d9db7f0f50288fc2c4a81821d614df0a42d73d68 100644 (file)
@@ -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);
         }
index 2ca470dc4864c0b5df8a3848d893077e768ee8d9..3f5296a14150c51d51af22cb923dfc05f026f8e3 100644 (file)
@@ -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.");
                 }