mirror of
				https://github.com/subsurface/subsurface.git
				synced 2025-02-19 22:16:15 +00:00 
			
		
		
		
	In a number of places the global 'tank_info' array is being iterated based on a 'tank_info[idx].name != NULL' condition. This is dangerous because if the user has added a lot of tanks, such loops can reach 'tank_info[MAX_TANK_INFO]'. This is an out of bounds read and if the 'name' pointer there happens to be non-NULL, passing that address to a peace of code that tries to read it (like strlen()) would either SIGSEGV or have undefined behavior. Clamp all loops that iterate 'tank_info' to MAX_TANK_INFO. Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
		
			
				
	
	
		
			120 lines
		
	
	
	
		
			2.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			120 lines
		
	
	
	
		
			2.6 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| // SPDX-License-Identifier: GPL-2.0
 | |
| #include "qt-models/tankinfomodel.h"
 | |
| #include "core/dive.h"
 | |
| #include "core/gettextfromc.h"
 | |
| #include "core/metrics.h"
 | |
| 
 | |
| TankInfoModel *TankInfoModel::instance()
 | |
| {
 | |
| 	static TankInfoModel self;
 | |
| 	return &self;
 | |
| }
 | |
| 
 | |
| const QString &TankInfoModel::biggerString() const
 | |
| {
 | |
| 	return biggerEntry;
 | |
| }
 | |
| 
 | |
| bool TankInfoModel::insertRows(int, int count, const QModelIndex &parent)
 | |
| {
 | |
| 	beginInsertRows(parent, rowCount(), rowCount());
 | |
| 	rows += count;
 | |
| 	endInsertRows();
 | |
| 	return true;
 | |
| }
 | |
| 
 | |
| bool TankInfoModel::setData(const QModelIndex &index, const QVariant &value, int)
 | |
| {
 | |
| 	//WARN Seems wrong, we need to check for role == Qt::EditRole
 | |
| 
 | |
| 	if (index.row() < 0 || index.row() > MAX_TANK_INFO - 1)
 | |
| 		return false;
 | |
| 
 | |
| 	struct tank_info_t *info = &tank_info[index.row()];
 | |
| 	switch (index.column()) {
 | |
| 	case DESCRIPTION:
 | |
| 		info->name = strdup(value.toByteArray().data());
 | |
| 		break;
 | |
| 	case ML:
 | |
| 		info->ml = value.toInt();
 | |
| 		break;
 | |
| 	case BAR:
 | |
| 		info->bar = value.toInt();
 | |
| 		break;
 | |
| 	}
 | |
| 	emit dataChanged(index, index);
 | |
| 	return true;
 | |
| }
 | |
| 
 | |
| void TankInfoModel::clear()
 | |
| {
 | |
| }
 | |
| 
 | |
| QVariant TankInfoModel::data(const QModelIndex &index, int role) const
 | |
| {
 | |
| 	QVariant ret;
 | |
| 	if (!index.isValid() || index.row() < 0 || index.row() > MAX_TANK_INFO - 1) {
 | |
| 		return ret;
 | |
| 	}
 | |
| 	if (role == Qt::FontRole) {
 | |
| 		return defaultModelFont();
 | |
| 	}
 | |
| 	if (role == Qt::DisplayRole || role == Qt::EditRole) {
 | |
| 		struct tank_info_t *info = &tank_info[index.row()];
 | |
| 		int ml = info->ml;
 | |
| 		double bar = (info->psi) ? psi_to_bar(info->psi) : info->bar;
 | |
| 
 | |
| 		if (info->cuft && info->psi)
 | |
| 			ml = lrint(cuft_to_l(info->cuft) * 1000 / bar_to_atm(bar));
 | |
| 
 | |
| 		switch (index.column()) {
 | |
| 		case BAR:
 | |
| 			ret = bar * 1000;
 | |
| 			break;
 | |
| 		case ML:
 | |
| 			ret = ml;
 | |
| 			break;
 | |
| 		case DESCRIPTION:
 | |
| 			ret = QString(info->name);
 | |
| 			break;
 | |
| 		}
 | |
| 	}
 | |
| 	return ret;
 | |
| }
 | |
| 
 | |
| int TankInfoModel::rowCount(const QModelIndex&) const
 | |
| {
 | |
| 	return rows + 1;
 | |
| }
 | |
| 
 | |
| TankInfoModel::TankInfoModel() : rows(-1)
 | |
| {
 | |
| 	setHeaderDataStrings(QStringList() << tr("Description") << tr("ml") << tr("bar"));
 | |
| 	struct tank_info_t *info = tank_info;
 | |
| 	for (info = tank_info; info->name && info < tank_info + MAX_TANK_INFO; info++, rows++) {
 | |
| 		QString infoName = gettextFromC::tr(info->name);
 | |
| 		if (infoName.count() > biggerEntry.count())
 | |
| 			biggerEntry = infoName;
 | |
| 	}
 | |
| 
 | |
| 	if (rows > -1) {
 | |
| 		beginInsertRows(QModelIndex(), 0, rows);
 | |
| 		endInsertRows();
 | |
| 	}
 | |
| }
 | |
| 
 | |
| void TankInfoModel::update()
 | |
| {
 | |
| 	if (rows > -1) {
 | |
| 		beginRemoveRows(QModelIndex(), 0, rows);
 | |
| 		endRemoveRows();
 | |
| 		rows = -1;
 | |
| 	}
 | |
| 	struct tank_info_t *info = tank_info;
 | |
| 	for (info = tank_info; info->name && info < tank_info + MAX_TANK_INFO; info++, rows++);
 | |
| 
 | |
| 	if (rows > -1) {
 | |
| 		beginInsertRows(QModelIndex(), 0, rows);
 | |
| 		endInsertRows();
 | |
| 	}
 | |
| }
 |