From 0d06e047ae32f5a0c9afddb8a0df1986f3b006d4 Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Mon, 22 Jul 2019 15:56:20 +0200 Subject: [PATCH] libcalamares: Prevent integer overflows when parsing configuration Currently, a number of configuration parsing-related functions and classes use only `int` type for dealing with integers. Should the user need a bigger integer value, this would result in an erroneous value being used (`0`), as the correct value would overflow the 32-bits type. In order to prevent these overflow, this patch replaces `int` with `qint64` in the following functions & classes : * CalamaresUtils::yamlScalarToVariant() * CalamaresUtils::getInteger * NamedSuffix * PartitionSize This way, sizes or other integer values greater than 2^31 (for signed types) can be used. Signed-off-by: Arnaud Ferraris --- src/libcalamares/partition/PartitionSize.cpp | 2 +- src/libcalamares/utils/NamedSuffix.h | 8 ++++---- src/libcalamares/utils/Variant.cpp | 10 +++++++--- src/libcalamares/utils/Variant.h | 2 +- src/libcalamares/utils/Yaml.cpp | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/libcalamares/partition/PartitionSize.cpp b/src/libcalamares/partition/PartitionSize.cpp index 3f4463f3c..3397dcbec 100644 --- a/src/libcalamares/partition/PartitionSize.cpp +++ b/src/libcalamares/partition/PartitionSize.cpp @@ -50,7 +50,7 @@ PartitionSize::PartitionSize( const QString& s ) if ( m_unit == SizeUnit::None ) { - m_value = s.toInt(); + m_value = s.toLongLong(); if ( m_value > 0 ) { m_unit = SizeUnit::Byte; diff --git a/src/libcalamares/utils/NamedSuffix.h b/src/libcalamares/utils/NamedSuffix.h index e697c0640..8ad52edea 100644 --- a/src/libcalamares/utils/NamedSuffix.h +++ b/src/libcalamares/utils/NamedSuffix.h @@ -58,7 +58,7 @@ public: } /** @brief Specific value and unit. */ - NamedSuffix( int value, unit_t unit ) + NamedSuffix( qint64 value, unit_t unit ) : m_value( value ) , m_unit( unit ) { @@ -75,7 +75,7 @@ public: for ( const auto& suffix : table.table ) if ( s.endsWith( suffix.first ) ) { - m_value = s.left( s.length() - suffix.first.length() ).toInt(); + m_value = s.left( s.length() - suffix.first.length() ).toLongLong(); m_unit = suffix.second; break; } @@ -89,7 +89,7 @@ public: */ NamedSuffix( const QString& s ); - int value() const { return m_value; } + qint64 value() const { return m_value; } unit_t unit() const { return m_unit; } /** @brief Check that a value-unit combination is valid. @@ -100,7 +100,7 @@ public: bool isValid() const; protected: - int m_value; + qint64 m_value; unit_t m_unit; }; diff --git a/src/libcalamares/utils/Variant.cpp b/src/libcalamares/utils/Variant.cpp index 2e7a13eed..c56f9301a 100644 --- a/src/libcalamares/utils/Variant.cpp +++ b/src/libcalamares/utils/Variant.cpp @@ -61,10 +61,10 @@ getString( const QVariantMap& map, const QString& key ) return QString(); } -int -getInteger( const QVariantMap& map, const QString& key, int d ) +qint64 +getInteger( const QVariantMap& map, const QString& key, qint64 d ) { - int result = d; + qint64 result = d; if ( map.contains( key ) ) { auto v = map.value( key ); @@ -72,6 +72,10 @@ getInteger( const QVariantMap& map, const QString& key, int d ) { result = v.toInt(); } + else if ( v.type() == QVariant::LongLong ) + { + result = v.toLongLong(); + } } return result; diff --git a/src/libcalamares/utils/Variant.h b/src/libcalamares/utils/Variant.h index c68c2a801..15f791b74 100644 --- a/src/libcalamares/utils/Variant.h +++ b/src/libcalamares/utils/Variant.h @@ -41,7 +41,7 @@ DLLEXPORT QString getString( const QVariantMap& map, const QString& key ); /** * Get an integer value from a mapping; returns @p d if no value. */ -DLLEXPORT int getInteger( const QVariantMap& map, const QString& key, int d ); +DLLEXPORT qint64 getInteger( const QVariantMap& map, const QString& key, qint64 d ); /** * Get a double value from a mapping (integers are converted); returns @p d if no value. diff --git a/src/libcalamares/utils/Yaml.cpp b/src/libcalamares/utils/Yaml.cpp index cad6ac1fe..164c17a21 100644 --- a/src/libcalamares/utils/Yaml.cpp +++ b/src/libcalamares/utils/Yaml.cpp @@ -79,7 +79,7 @@ yamlScalarToVariant( const YAML::Node& scalarNode ) } if ( QRegExp( "[-+]?\\d+" ).exactMatch( scalarString ) ) { - return QVariant( scalarString.toInt() ); + return QVariant( scalarString.toLongLong() ); } if ( QRegExp( "[-+]?\\d*\\.?\\d+" ).exactMatch( scalarString ) ) {