diff options
author | Frédéric Boisnard <fredericx.boisnard@intel.com> | 2012-08-06 19:03:01 +0200 |
---|---|---|
committer | David Wagner <david.wagner@intel.com> | 2014-02-12 17:03:12 +0100 |
commit | dc32d63f2ed740af1bfc0b3f8fd2337cba46d895 (patch) | |
tree | 2afb4dd944e250bf86b172590f80d97f0be3506e /parameter/IntegerParameterType.cpp | |
parent | 7bd615ba9b4d44d78aa745a10e836d2e02f6a995 (diff) | |
download | external_parameter-framework-dc32d63f2ed740af1bfc0b3f8fd2337cba46d895.zip external_parameter-framework-dc32d63f2ed740af1bfc0b3f8fd2337cba46d895.tar.gz external_parameter-framework-dc32d63f2ed740af1bfc0b3f8fd2337cba46d895.tar.bz2 |
PFW: Check if value provided is exactly an integer
BZ: 50802
Previously, if a fixed point value was provided for an integer parameter, there was no error.
This was due to the results of strtol/strtoll which was not correctly checked (it would only fail
if no digit was provided, but would succeed even if letters were appended after/before the digits).
So for fixed point values, only the first part of the string was fetched, giving the impression of a truncation at the '.' character .
Now the whole string shall be completly parsed for the conversion to succeed.
Change-Id: Ia907e8cc05e5639e9a8bbd7204f46df5bdfa394a
Signed-off-by: Frédéric Boisnard <fredericx.boisnard@intel.com>
Reviewed-on: http://android.intel.com:8080/60743
Reviewed-by: Rocard, KevinX <kevinx.rocard@intel.com>
Reviewed-by: Benavoli, Patrick <patrick.benavoli@intel.com>
Reviewed-by: De Chivre, Renaud <renaud.de.chivre@intel.com>
Tested-by: Mendi, EduardoX <eduardox.mendi@intel.com>
Reviewed-by: buildbot <buildbot@intel.com>
Tested-by: buildbot <buildbot@intel.com>
Diffstat (limited to 'parameter/IntegerParameterType.cpp')
-rw-r--r-- | parameter/IntegerParameterType.cpp | 58 |
1 files changed, 38 insertions, 20 deletions
diff --git a/parameter/IntegerParameterType.cpp b/parameter/IntegerParameterType.cpp index d6b27ba..8fadf27 100644 --- a/parameter/IntegerParameterType.cpp +++ b/parameter/IntegerParameterType.cpp @@ -141,42 +141,30 @@ bool CIntegerParameterType::toBlackboard(const string& strValue, uint32_t& uiVal // Hexa bool bValueProvidedAsHexa = !strValue.compare(0, 2, "0x"); - // Get value + // Get integer value from the string provided int64_t iData; - // Reset errno to check if it is updated during the conversion (strtol/strtoul) - errno = 0; - char *pcStrEnd; - - // Convert the input string - if (_bSigned) { - - iData = strtoll(strValue.c_str(), &pcStrEnd, 0); - } else { + if (!convertValueFromString(strValue, iData, parameterAccessContext)) { - iData = strtoull(strValue.c_str(), &pcStrEnd, 0); + return false; } - - // Conversion error when the input string does not contain any digit or the number is out of range (int32_t type) - bool bConversionSucceeded = !errno && (strValue.c_str() != pcStrEnd); - // Check against Min / Max if (_bSigned) { - if (bConversionSucceeded && bValueProvidedAsHexa && isEncodable((uint64_t)iData, !bValueProvidedAsHexa)) { + if (bValueProvidedAsHexa && isEncodable((uint64_t)iData, !bValueProvidedAsHexa)) { // Sign extend signExtend(iData); } - if (!checkValueAgainstRange<int64_t>(strValue, iData, (int32_t)_uiMin, (int32_t)_uiMax, parameterAccessContext, bValueProvidedAsHexa, bConversionSucceeded)) { + if (!checkValueAgainstRange<int64_t>(strValue, iData, (int32_t)_uiMin, (int32_t)_uiMax, parameterAccessContext, bValueProvidedAsHexa)) { return false; } } else { - if (!checkValueAgainstRange<uint64_t>(strValue, iData, _uiMin, _uiMax, parameterAccessContext, bValueProvidedAsHexa, bConversionSucceeded)) { + if (!checkValueAgainstRange<uint64_t>(strValue, iData, _uiMin, _uiMax, parameterAccessContext, bValueProvidedAsHexa)) { return false; } @@ -375,10 +363,40 @@ uint32_t CIntegerParameterType::getDefaultValue() const return _uiMin; } +// Convert value provided by the user as a string into an int64 +bool CIntegerParameterType::convertValueFromString(const string& strValue, int64_t& iData, CParameterAccessContext& parameterAccessContext) const { + + // Reset errno to check if it is updated during the conversion (strtol/strtoul) + errno = 0; + char *pcStrEnd; + + // Convert the input string + if (_bSigned) { + + iData = strtoll(strValue.c_str(), &pcStrEnd, 0); + } else { + + iData = strtoull(strValue.c_str(), &pcStrEnd, 0); + } + + // Conversion error when the input string does not contain only digits or the number is out of range (int32_t type) + if (errno || (*pcStrEnd != '\0')) { + + string strError; + strError = "Impossible to convert value " + strValue + " for " + getKind(); + + parameterAccessContext.setError(strError); + + return false; + } + + return true; +} + // Range checking -template <typename type> bool CIntegerParameterType::checkValueAgainstRange(const string& strValue, type value, type minValue, type maxValue, CParameterAccessContext& parameterAccessContext, bool bHexaValue, bool bConversionSucceeded) const +template <typename type> bool CIntegerParameterType::checkValueAgainstRange(const string& strValue, type value, type minValue, type maxValue, CParameterAccessContext& parameterAccessContext, bool bHexaValue) const { - if (!bConversionSucceeded || value < minValue || value > maxValue) { + if (value < minValue || value > maxValue) { ostringstream strStream; |