From 27b28a5bd14381cbd7c07c032ab97ac10b6a14af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Wed, 16 Jul 2014 10:35:09 +0200 Subject: [PATCH] Avoid potential rounding errors --- .../partition/CreatePartitionDialog.cpp | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/modules/partition/CreatePartitionDialog.cpp b/src/modules/partition/CreatePartitionDialog.cpp index eec0e3f0c..5e411b368 100644 --- a/src/modules/partition/CreatePartitionDialog.cpp +++ b/src/modules/partition/CreatePartitionDialog.cpp @@ -20,6 +20,7 @@ #include #include +#include // CalaPM #include @@ -93,19 +94,33 @@ CreatePartitionDialog::createPartitionInfo() ); } - // FIXME: Check rounding errors here - qint64 last = m_minSector + qint64( m_ui->sizeSpinBox->value() ) * 1024 * 1024 / m_device->logicalSectorSize(); + qint64 lastSector; + int mbSize = m_ui->sizeSpinBox->value(); + if ( mbSize == m_ui->sizeSpinBox->maximum() ) { + // If we are at the maximum value, select the last sector to avoid + // potential rounding errors which could leave a few sectors at the end + // unused + lastSector = m_maxSector; + } else { + lastSector = m_minSector + qint64( mbSize ) * 1024 * 1024 / m_device->logicalSectorSize(); + Q_ASSERT( lastSector <= m_maxSector ); + if ( lastSector > m_maxSector ) + { + cDebug() << "lastSector (" << lastSector << ") > m_maxSector (" << m_maxSector << "). This should not happen!"; + lastSector = m_maxSector; + } + } FileSystem::Type type = m_role.has( PartitionRole::Extended ) ? FileSystem::Extended : FileSystem::typeForName( m_ui->fsComboBox->currentText() ); - FileSystem* fs = FileSystemFactory::create( type, m_minSector, last ); + FileSystem* fs = FileSystemFactory::create( type, m_minSector, lastSector ); auto partition = new Partition( m_parent, *m_device, m_role, - fs, m_minSector, last, + fs, m_minSector, lastSector, QString() /* path */, PartitionTable::FlagNone /* availableFlags */, QString() /* mountPoint */,