SrmRideFile: handle bad/truncated files

SRM file reader didn't check stream status / values taken from the file.
This could easily lead to segmentation faults.
This commit is contained in:
Rainer Clasen
2012-07-30 18:52:22 +02:00
parent d19689d8f6
commit 29afd39645

View File

@@ -131,11 +131,20 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL
readByte(in); // padding
quint8 commentlen = readByte(in);
if( commentlen > 70 )
commentlen = 70;
char comment[71];
in.readRawData(comment, sizeof(comment) - 1);
comment[commentlen] = '\0';
result->setTag("Notes", QString(comment) );
// assert propper markercnt to avoid segfaults
if( in.status() != QDataStream::Ok ){
errorStrings << QString("failed to read file header" );
return NULL;
}
result->setRecIntSecs(((double) recint1) / recint2);
unsigned recintms = (unsigned) round(result->recIntSecs() * 1000.0);
@@ -190,6 +199,12 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL
(void) wheelcirc;
}
// fail early to tell devs whats wrong with file
if( in.status() != QDataStream::Ok ){
errorStrings << QString("failed to read marker" );
return NULL;
}
blockhdr *blockhdrs = new blockhdr[blockcnt+1];
for (int i = 0; i < blockcnt; ++i) {
// In the .srm files generated by Rainer Clasen's srmcmd,
@@ -203,11 +218,23 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL
blockhdrs[i].dt = blockhdrs[i].dt.addMSecs(hsecsincemidn * 10);
}
// fail early to tell devs whats wrong with file
if( in.status() != QDataStream::Ok ){
errorStrings << QString("failed to read block headers" );
return NULL;
}
quint16 zero = readShort(in);
quint16 slope = readShort(in);
quint16 datacnt = readShort(in);
readByte(in); // padding
// fail early to tell devs whats wrong with file
if( in.status() != QDataStream::Ok ){
errorStrings << QString("failed to read calibration data" );
return NULL;
}
result->setTag("Slope", QString("%1")
.arg( 140.0 / 42781 * slope, 0, 'f', 2) );
result->setTag("Zero Offset", QString("%1").arg(zero) );
@@ -297,6 +324,15 @@ RideFile *SrmFileReader::openRideFile(QFile &file, QStringList &errorStrings, QL
}
}
// assert some points were found. prevents segfault when looking at
// the overall markers[0].start/.end
// note: we're not checking in.status() to cope with truncated files
if( result->dataPoints().size() < 1 ){
errorStrings << QString("file contains no data points");
return NULL;
}
double last = 0.0;
for (int i = 1; i < markers.size(); ++i) {
const marker &marker = markers[i];