Skip to content

Commit 187f83b

Browse files
committed
[flo2d] Protect against overflows on malformed data files
1 parent c67e06f commit 187f83b

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

mdal/frmts/mdal_flo2d.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ void MDAL::DriverFlo2D::parseCADPTSFile( const std::string &datFileName, std::ve
106106
throw MDAL::Error( MDAL_Status::Err_UnknownFormat, "Error while loading CADPTS file, wrong lineparts count (3)" );
107107
}
108108
CellCenter cc;
109-
cc.id = MDAL::toSizeT( lineParts[0] ) - 1; //numbered from 1
109+
const size_t linePart0 = MDAL::toSizeT( lineParts[0] );
110+
if ( linePart0 < 1 )
111+
{
112+
throw MDAL::Error( MDAL_Status::Err_UnknownFormat, "Error while loading CADPTS file, wrong line part id" );
113+
}
114+
cc.id = linePart0 - 1; //numbered from 1
110115
cc.x = MDAL::toDouble( lineParts[1] );
111116
cc.y = MDAL::toDouble( lineParts[2] );
112117
cells.push_back( cc );
@@ -144,7 +149,12 @@ void MDAL::DriverFlo2D::parseCHANBANKFile( const std::string &datFileName,
144149
{
145150
throw MDAL::Error( MDAL_Status::Err_UnknownFormat, "Error while loading CHANBANK file, wrong lineparts count (2)" );
146151
}
147-
int leftBank = MDAL::toInt( MDAL::toSizeT( lineParts[0] ) ) - 1; //numbered from 1
152+
const size_t linePart0 = MDAL::toSizeT( lineParts[0] );
153+
if ( linePart0 < 1 )
154+
{
155+
throw MDAL::Error( MDAL_Status::Err_UnknownFormat, "Error while loading CHANBANK file, wrong line value for left bank" );
156+
}
157+
size_t leftBank = linePart0 - 1; //numbered from 1
148158
int rightBank = MDAL::toInt( MDAL::toSizeT( lineParts[1] ) ) - 1;
149159

150160
std::map<size_t, size_t>::const_iterator it = cellIdToVertices.find( rightBank );
@@ -217,8 +227,14 @@ void MDAL::DriverFlo2D::parseCHANFile( const std::string &datFileName, const std
217227
{
218228
throw MDAL::Error( MDAL_Status::Err_UnknownFormat, "Error while loading CHAN file, wrong confluence line:" );
219229
}
220-
std::map<size_t, size_t>::const_iterator it1 = cellIdToVertices.find( MDAL::toSizeT( lineParts[1] ) - 1 );
221-
std::map<size_t, size_t>::const_iterator it2 = cellIdToVertices.find( MDAL::toSizeT( lineParts[2] ) - 1 );
230+
const size_t linePart1 = MDAL::toSizeT( lineParts[1] );
231+
const size_t linePart2 = MDAL::toSizeT( lineParts[2] );
232+
if ( linePart1 < 1 || linePart2 < 1 )
233+
{
234+
throw MDAL::Error( MDAL_Status::Err_UnknownFormat, "Error while loading CHAN file, wrong confluence line:" );
235+
}
236+
std::map<size_t, size_t>::const_iterator it1 = cellIdToVertices.find( linePart1 - 1 );
237+
std::map<size_t, size_t>::const_iterator it2 = cellIdToVertices.find( linePart2 - 1 );
222238
if ( it1 != cellIdToVertices.end() && it2 != cellIdToVertices.end() )
223239
edges.push_back( {it1->second, it2->second} );
224240
}
@@ -503,7 +519,12 @@ void MDAL::DriverFlo2D::parseFPLAINFile( std::vector<double> &elevations,
503519

504520
if ( !cellSizeCalculated )
505521
{
506-
size_t cc_i = MDAL::toSizeT( lineParts[0] ) - 1; //numbered from 1
522+
const size_t linePart0 = MDAL::toSizeT( lineParts[0] );
523+
if ( linePart0 < 1 )
524+
{
525+
throw MDAL::Error( MDAL_Status::Err_UnknownFormat, "Error while loading FPLAIN.DAT file, invalid line part value" );
526+
}
527+
size_t cc_i = linePart0 - 1; //numbered from 1
507528
for ( int i = 1; i < 5; ++i ) //search the first cell that have a neighbor to calculate cell size
508529
{
509530
int neighborCell = MDAL::toInt( lineParts[i] );

0 commit comments

Comments
 (0)