Catch roff parse errors when checking for grid data

This commit is contained in:
Magne Sjaastad committed 2026-09-28 09:18:41 +02:00
1 parent 17e8c96e00
commit 0db64b400b
2 files changed
+32 -9

No files matched your search

@@ -666,18 +666,26 @@ bool RifRoffFileTools::hasGridData( const QString& filename )
return false; return false;
} }
roff::Reader reader( stream ); try
reader.parse(); {
roff::Reader reader( stream );
reader.parse();
const std::vector<std::pair<std::string, roff::Token::Kind>> arrayTypes = reader.getNamedArrayTypes(); const std::vector<std::pair<std::string, roff::Token::Kind>> arrayTypes = reader.getNamedArrayTypes();
const std::string cornerLinesDataKeyword = "cornerLines" + roff::Parser::postFixData(); const std::string cornerLinesDataKeyword = "cornerLines" + roff::Parser::postFixData();
auto cornerLinesDataItr = std::find_if( arrayTypes.begin(), auto cornerLinesDataItr = std::find_if( arrayTypes.begin(),
arrayTypes.end(), arrayTypes.end(),
[&cornerLinesDataKeyword]( const auto& arrayType ) [&cornerLinesDataKeyword]( const auto& arrayType )
{ return arrayType.first == cornerLinesDataKeyword; } ); { return arrayType.first == cornerLinesDataKeyword; } );
return cornerLinesDataItr != arrayTypes.end(); return cornerLinesDataItr != arrayTypes.end();
}
catch ( std::runtime_error& err )
{
RiaLogging::error( std::format( "Roff file parsing failed: {}", err.what() ) );
return false;
}
} }
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
@@ -16,6 +16,7 @@
#include <QDir> #include <QDir>
#include <QFile> #include <QFile>
#include <QTemporaryDir>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
@@ -179,6 +180,20 @@ TEST( RifRoffFileTools, HasGridDataFalseForMissingFile )
EXPECT_FALSE( RifRoffFileTools::hasGridData( filePath ) ); EXPECT_FALSE( RifRoffFileTools::hasGridData( filePath ) );
} }
TEST( RifRoffFileTools, HasGridDataFalseForMalformedFile )
{
QTemporaryDir tempDir;
ASSERT_TRUE( tempDir.isValid() );
auto filePath = tempDir.filePath( "malformed.roff" );
QFile file( filePath );
ASSERT_TRUE( file.open( QIODevice::WriteOnly ) );
file.write( "this is not a roff file" );
file.close();
EXPECT_FALSE( RifRoffFileTools::hasGridData( filePath ) );
}
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
// openGridFile on a stream is the entry point used when the roff data is not a file on disk, e.g. a // openGridFile on a stream is the entry point used when the roff data is not a file on disk, e.g. a
// blob downloaded from Sumo. // blob downloaded from Sumo.