[PATCH 3/3] Models: fix two potential crashes

Lubomir I. Ivanov neolit123 at gmail.com
Sun Oct 13 06:20:32 UTC 2013


From: "Lubomir I. Ivanov" <neolit123 at gmail.com>

CylinderModels::setDive() and WeightModel::setDive() have
potential to pass the 'last' argument to beginInsertRows() as
a negative number which triggers an assert that 'last' cannot
be smaller than 'first'.

Patch attempts to fix that by only calling beginInsertRows()
when there is at least one row to insert.

Signed-off-by: Lubomir I. Ivanov <neolit123 at gmail.com>
---

difficult to debug since even that the assert is visible
there are a lot of models in the app and we are not sure
what is causing it. running trough gdb says nothing, because once
the assert triggers the call stack is cleared and there is no
back trace. so either arbitrary breakpoints have to be set
or going trough the code and commenting out hunks until
it works/does-not-works...
---
 qt-ui/models.cpp | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/qt-ui/models.cpp b/qt-ui/models.cpp
index e70bece..3c3b48b 100644
--- a/qt-ui/models.cpp
+++ b/qt-ui/models.cpp
@@ -323,12 +323,13 @@ void CylindersModel::setDive(dive* d)
 			break;
 		}
 	}
-
-	beginInsertRows(QModelIndex(), 0, amount-1);
 	rows = amount;
 	current = d;
 	changed = false;
-	endInsertRows();
+	if (amount > 0) {
+		beginInsertRows(QModelIndex(), 0, amount - 1);
+		endInsertRows();
+	}
 }
 
 Qt::ItemFlags CylindersModel::flags(const QModelIndex& index) const
@@ -504,12 +505,13 @@ void WeightModel::setDive(dive* d)
 			break;
 		}
 	}
-
-	beginInsertRows(QModelIndex(), 0, amount-1);
 	rows = amount;
 	current = d;
 	changed = false;
-	endInsertRows();
+	if (amount > 0) {
+		beginInsertRows(QModelIndex(), 0, amount - 1);
+		endInsertRows();
+	}
 }
 
 WSInfoModel* WSInfoModel::instance()
-- 
1.7.11.msysgit.0



More information about the subsurface mailing list