diff --git a/src/SrmRideFile.cpp b/src/SrmRideFile.cpp index 4a02becbc..160e39fb2 100644 --- a/src/SrmRideFile.cpp +++ b/src/SrmRideFile.cpp @@ -207,6 +207,7 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL } blockhdr *blockhdrs = new blockhdr[blockcnt+1]; + int blockchunkcnt = 0; for (int i = 0; i < blockcnt; ++i) { // In the .srm files generated by Rainer Clasen's srmcmd, // hsecsincemidn is a *signed* 32-bit integer. I haven't seen a @@ -217,6 +218,7 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL blockhdrs[i].chunkcnt = readShort(in); blockhdrs[i].dt = QDateTime(date); blockhdrs[i].dt = blockhdrs[i].dt.addMSecs(hsecsincemidn * 10); + blockchunkcnt += blockhdrs[i].chunkcnt; } // fail early to tell devs whats wrong with file @@ -247,6 +249,13 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL blockhdrs[0].dt = QDateTime(date); } + // datacnt might overflow at 64k - so, use sum from blocks, instead + if( datacnt > blockchunkcnt ){ + blockchunkcnt = datacnt; + errorStrings << QString("inconsistent chunk count total=%1, blocks=%2") + .arg(datacnt) + .arg(blockchunkcnt); + } int blknum = 0, blkidx = 0, mrknum = 0, interval = 0; double km = 0.0, secs = 0.0; @@ -254,7 +263,7 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL if (markercnt > 0) mrknum = 1; - for (int i = 0; i < datacnt; ++i) { + for (int i = 0; i < blockchunkcnt; ++i) { int cad, hr, watts; double kph, alt; double temp=-255; @@ -268,8 +277,7 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL watts = (ps[1] & 0x0f) | (ps[2] << 0x4); alt = 0.0; } - else { - assert(version == 7); + else if (version == 7 ){ watts = readShort(in); cad = readByte(in); hr = readByte(in); @@ -280,6 +288,16 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL alt = readSignedLong(in); temp = 0.1 * readSignedShort(in); } + else { + errorStrings << QString("unsupported SRM file version: %1") + .arg( version ); + return NULL; + } + + if( in.status() != QDataStream::Ok ){ + errorStrings << QString("premature end of file" ); + break; + } if (i == 0) { result->setStartTime(blockhdrs[blknum].dt);