From 982e7a5fdad3f15350292d3f5b027dc81a5f7161 Mon Sep 17 00:00:00 2001 From: Jules Clero Date: Wed, 22 Apr 2015 09:11:39 +0200 Subject: [PATCH] Add check for inclusive criterion overflow Inclusive criteria have a limited value number due to their internal state representation. Currently, the only check made for this is at test-platform level. This patch adds a new check to make addValuePair API fail if values too large are added to an inclusive criterion type. A log has also been added to warn the client. The check at test-platform level has been removed. Change-Id: Ie9c9ec8fb8561f949bb62adbab127bc900aa254b Signed-off-by: Jules Clero --- parameter/SelectionCriterionType.cpp | 16 +++++++++++++++- test/test-platform/TestPlatform.cpp | 17 +---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/parameter/SelectionCriterionType.cpp b/parameter/SelectionCriterionType.cpp index ce633c6f0..6e5e73d4b 100644 --- a/parameter/SelectionCriterionType.cpp +++ b/parameter/SelectionCriterionType.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2014, Intel Corporation + * Copyright (c) 2011-2015, Intel Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -30,6 +30,8 @@ #include "SelectionCriterionType.h" #include "Tokenizer.h" +#include + #define base CElement const std::string CSelectionCriterionType::_strDelimiter = "|"; @@ -51,6 +53,18 @@ std::string CSelectionCriterionType::getKind() const // From ISelectionCriterionTypeInterface bool CSelectionCriterionType::addValuePair(int iValue, const std::string& strValue) { + // An inclusive criterion is implemented as a bitfield over an int and + // thus, can't have values larger than the number of bits in an int. + static const unsigned int inclusiveCriterionMaxValue = 1 << (sizeof(iValue) * CHAR_BIT - 1); + + if (_bInclusive && (unsigned int)iValue > inclusiveCriterionMaxValue) { + + log_warning("Rejecting value pair association: 0x%X - %s " + "because an inclusive criterion can't have values larger than 0x%X", + iValue, strValue.c_str(), inclusiveCriterionMaxValue); + return false; + } + // Check 1 bit set only for inclusive types if (_bInclusive && (!iValue || (iValue & (iValue - 1)))) { diff --git a/test/test-platform/TestPlatform.cpp b/test/test-platform/TestPlatform.cpp index 6e67b3a55..9ccac1ba9 100644 --- a/test/test-platform/TestPlatform.cpp +++ b/test/test-platform/TestPlatform.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2014, Intel Corporation + * Copyright (c) 2011-2015, Intel Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -333,14 +333,6 @@ bool CTestPlatform::createInclusiveSelectionCriterionFromStateList( assert(pCriterionType != NULL); uint32_t uiNbStates = remoteCommand.getArgumentCount() - 1; - - if (uiNbStates > 32) { - - strResult = "Maximum number of states for inclusive criterion is 32"; - - return false; - } - uint32_t uiState; for (uiState = 0; uiState < uiNbStates; uiState++) { @@ -397,13 +389,6 @@ bool CTestPlatform::createInclusiveSelectionCriterion(const string& strName, ISelectionCriterionTypeInterface* pCriterionType = _pParameterMgrPlatformConnector->createSelectionCriterionType(true); - if (uiNbStates > 32) { - - strResult = "Maximum number of states for inclusive criterion is 32"; - - return false; - } - uint32_t uiState; for (uiState = 0; uiState < uiNbStates; uiState++) {