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>
<define name="DATA-BUFFER-SIZE"> <define name="DATA-BUFFER-SIZE">
<choice> <data type="string">
<value>malloc</value> <param name="pattern">malloc(:[1-5])?|calloc(:[1-5],[1-5])?|strdup(:[1-5])?</param>
<value>calloc</value> </data>
<value>strdup</value>
</choice>
</define> </define>
<define name="CONTAINER-ACTION"> <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"); const char *bufferSize = memorynode->Attribute("buffer-size");
if (!bufferSize) if (!bufferSize)
temp.bufferSize = AllocFunc::BufferSize::none; 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; 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; 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; temp.bufferSize = AllocFunc::BufferSize::strdup;
else else
return Error(BAD_ATTRIBUTE_VALUE, bufferSize); 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; mAlloc[memorynode->GetText()] = temp;
} else if (memorynodename == "dealloc") { } else if (memorynodename == "dealloc") {

View File

@ -75,6 +75,8 @@ public:
int arg; int arg;
enum class BufferSize {none,malloc,calloc,strdup}; enum class BufferSize {none,malloc,calloc,strdup};
BufferSize bufferSize; BufferSize bufferSize;
int bufferSizeArg1;
int bufferSizeArg2;
}; };
/** get allocation info for function */ /** 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 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; MathLib::bigint sizeValue = -1;
switch (allocFunc->bufferSize) { switch (allocFunc->bufferSize) {
case Library::AllocFunc::BufferSize::none: case Library::AllocFunc::BufferSize::none:
break; break;
case Library::AllocFunc::BufferSize::malloc: case Library::AllocFunc::BufferSize::malloc:
if (args.size() == 1 && args[0]->hasKnownIntValue()) if (arg1 && arg1->hasKnownIntValue())
sizeValue = args[0]->getKnownIntValue(); sizeValue = arg1->getKnownIntValue();
break; break;
case Library::AllocFunc::BufferSize::calloc: case Library::AllocFunc::BufferSize::calloc:
if (args.size() == 2 && args[0]->hasKnownIntValue() && args[1]->hasKnownIntValue()) if (arg1 && arg2 && arg1->hasKnownIntValue() && arg2->hasKnownIntValue())
sizeValue = args[0]->getKnownIntValue() * args[1]->getKnownIntValue(); sizeValue = arg1->getKnownIntValue() * arg2->getKnownIntValue();
break; break;
case Library::AllocFunc::BufferSize::strdup: case Library::AllocFunc::BufferSize::strdup:
if (args.size() == 1 && args[0]->hasKnownValue()) { if (arg1 && arg1->hasKnownValue()) {
const ValueFlow::Value &value = args[0]->values().back(); const ValueFlow::Value &value = arg1->values().back();
if (value.isTokValue() && value.tokvalue->tokType() == Token::eString) if (value.isTokValue() && value.tokvalue->tokType() == Token::eString)
sizeValue = Token::getStrLength(value.tokvalue); sizeValue = Token::getStrLength(value.tokvalue);
} }