Library: added bufferSize parameters

This commit is contained in:
Daniel Marjamäki 2019-03-20 19:26:57 +01:00
parent 0372c9cde6
commit 55433fce40
4 changed files with 33 additions and 19 deletions

View File

@ -474,11 +474,9 @@
</define>
<define name="DATA-BUFFER-SIZE">
<choice>
<value>malloc</value>
<value>calloc</value>
<value>strdup</value>
</choice>
<data type="string">
<param name="pattern">malloc(:[1-5])?|calloc(:[1-5],[1-5])?|strdup(:[1-5])?</param>
</data>
</define>
<define name="CONTAINER-ACTION">

View File

@ -204,14 +204,25 @@ Library::Error Library::load(const tinyxml2::XMLDocument &doc)
const char *bufferSize = memorynode->Attribute("buffer-size");
if (!bufferSize)
temp.bufferSize = AllocFunc::BufferSize::none;
else if (std::strcmp(bufferSize, "malloc") == 0)
else {
if (std::strncmp(bufferSize, "malloc", 6) == 0)
temp.bufferSize = AllocFunc::BufferSize::malloc;
else if (std::strcmp(bufferSize, "calloc") == 0)
else if (std::strncmp(bufferSize, "calloc", 6) == 0)
temp.bufferSize = AllocFunc::BufferSize::calloc;
else if (std::strcmp(bufferSize, "strdup") == 0)
else if (std::strncmp(bufferSize, "strdup", 6) == 0)
temp.bufferSize = AllocFunc::BufferSize::strdup;
else
return Error(BAD_ATTRIBUTE_VALUE, bufferSize);
if (bufferSize[6] == 0) {
temp.bufferSizeArg1 = 1;
temp.bufferSizeArg2 = 2;
} else if (bufferSize[6] == ':' && bufferSize[7] >= '1' && bufferSize[7] <= '5') {
temp.bufferSizeArg1 = bufferSize[7] - '0';
if (bufferSize[8] == ',' && bufferSize[9] >= '1' && bufferSize[9] <= '5')
temp.bufferSizeArg2 = bufferSize[9] - '0';
} else
return Error(BAD_ATTRIBUTE_VALUE, bufferSize);
}
mAlloc[memorynode->GetText()] = temp;
} else if (memorynodename == "dealloc") {

View File

@ -75,6 +75,8 @@ public:
int arg;
enum class BufferSize {none,malloc,calloc,strdup};
BufferSize bufferSize;
int bufferSizeArg1;
int bufferSizeArg2;
};
/** get allocation info for function */

View File

@ -5130,21 +5130,24 @@ static void valueFlowDynamicBufferSize(TokenList *tokenlist, SymbolDatabase *sym
const std::vector<const Token *> args = getArguments(rhs->previous());
const Token * const arg1 = (args.size() >= allocFunc->bufferSizeArg1) ? args[allocFunc->bufferSizeArg1 - 1] : nullptr;
const Token * const arg2 = (args.size() >= allocFunc->bufferSizeArg2) ? args[allocFunc->bufferSizeArg2 - 1] : nullptr;
MathLib::bigint sizeValue = -1;
switch (allocFunc->bufferSize) {
case Library::AllocFunc::BufferSize::none:
break;
case Library::AllocFunc::BufferSize::malloc:
if (args.size() == 1 && args[0]->hasKnownIntValue())
sizeValue = args[0]->getKnownIntValue();
if (arg1 && arg1->hasKnownIntValue())
sizeValue = arg1->getKnownIntValue();
break;
case Library::AllocFunc::BufferSize::calloc:
if (args.size() == 2 && args[0]->hasKnownIntValue() && args[1]->hasKnownIntValue())
sizeValue = args[0]->getKnownIntValue() * args[1]->getKnownIntValue();
if (arg1 && arg2 && arg1->hasKnownIntValue() && arg2->hasKnownIntValue())
sizeValue = arg1->getKnownIntValue() * arg2->getKnownIntValue();
break;
case Library::AllocFunc::BufferSize::strdup:
if (args.size() == 1 && args[0]->hasKnownValue()) {
const ValueFlow::Value &value = args[0]->values().back();
if (arg1 && arg1->hasKnownValue()) {
const ValueFlow::Value &value = arg1->values().back();
if (value.isTokValue() && value.tokvalue->tokType() == Token::eString)
sizeValue = Token::getStrLength(value.tokvalue);
}