Token: Added utility function getStrSize as a complement to getStrLength
This commit is contained in:
parent
404c13ef4b
commit
544a5957e1
|
@ -1540,13 +1540,7 @@ void CheckBufferOverrun::checkStringArgument()
|
||||||
const std::list<Library::ArgumentChecks::MinSize> *minsizes = _settings->library.argminsizes(tok->str(), argnr);
|
const std::list<Library::ArgumentChecks::MinSize> *minsizes = _settings->library.argminsizes(tok->str(), argnr);
|
||||||
if (!minsizes)
|
if (!minsizes)
|
||||||
continue;
|
continue;
|
||||||
unsigned int sizeofstring = 1;
|
if (checkMinSizes(*minsizes, tok, Token::getStrSize(argtok), nullptr))
|
||||||
for (unsigned int i = 0U; i < argtok->str().size(); i++) {
|
|
||||||
if (argtok->str()[i] == '\\')
|
|
||||||
++i;
|
|
||||||
++sizeofstring;
|
|
||||||
}
|
|
||||||
if (checkMinSizes(*minsizes, tok, sizeofstring, nullptr))
|
|
||||||
bufferOverrunError(argtok);
|
bufferOverrunError(argtok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -651,6 +651,19 @@ std::size_t Token::getStrLength(const Token *tok)
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::size_t Token::getStrSize(const Token *tok)
|
||||||
|
{
|
||||||
|
assert(tok != nullptr && tok->type() == eString);
|
||||||
|
const std::string &str = tok->str();
|
||||||
|
unsigned int sizeofstring = 1U;
|
||||||
|
for (unsigned int i = 1U; i < str.size() - 1U; i++) {
|
||||||
|
if (str[i] == '\\')
|
||||||
|
++i;
|
||||||
|
++sizeofstring;
|
||||||
|
}
|
||||||
|
return sizeofstring;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Token::getCharAt(const Token *tok, std::size_t index)
|
std::string Token::getCharAt(const Token *tok, std::size_t index)
|
||||||
{
|
{
|
||||||
assert(tok != nullptr);
|
assert(tok != nullptr);
|
||||||
|
|
|
@ -197,6 +197,15 @@ public:
|
||||||
**/
|
**/
|
||||||
static std::size_t getStrLength(const Token *tok);
|
static std::size_t getStrLength(const Token *tok);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return sizeof of C-string.
|
||||||
|
*
|
||||||
|
* Should be called for %%str%% tokens only.
|
||||||
|
*
|
||||||
|
* @param tok token with C-string
|
||||||
|
**/
|
||||||
|
static std::size_t getStrSize(const Token *tok);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return char of C-string at index (possible escaped "\\n")
|
* @return char of C-string at index (possible escaped "\\n")
|
||||||
*
|
*
|
||||||
|
|
|
@ -48,6 +48,7 @@ private:
|
||||||
TEST_CASE(multiCompare4);
|
TEST_CASE(multiCompare4);
|
||||||
TEST_CASE(multiCompare5);
|
TEST_CASE(multiCompare5);
|
||||||
TEST_CASE(getStrLength);
|
TEST_CASE(getStrLength);
|
||||||
|
TEST_CASE(getStrSize);
|
||||||
TEST_CASE(strValue);
|
TEST_CASE(strValue);
|
||||||
|
|
||||||
TEST_CASE(deleteLast);
|
TEST_CASE(deleteLast);
|
||||||
|
@ -266,6 +267,19 @@ private:
|
||||||
ASSERT_EQUALS(1, (int)Token::getStrLength(&tok));
|
ASSERT_EQUALS(1, (int)Token::getStrLength(&tok));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void getStrSize() const {
|
||||||
|
Token tok(0);
|
||||||
|
|
||||||
|
tok.str("\"abc\"");
|
||||||
|
ASSERT_EQUALS(sizeof("abc"), Token::getStrSize(&tok));
|
||||||
|
|
||||||
|
tok.str("\"\\0abc\"");
|
||||||
|
ASSERT_EQUALS(sizeof("\0abc"), Token::getStrSize(&tok));
|
||||||
|
|
||||||
|
tok.str("\"\\\\\"");
|
||||||
|
ASSERT_EQUALS(sizeof("\\"), Token::getStrSize(&tok));
|
||||||
|
}
|
||||||
|
|
||||||
void strValue() const {
|
void strValue() const {
|
||||||
Token tok(0);
|
Token tok(0);
|
||||||
tok.str("\"\"");
|
tok.str("\"\"");
|
||||||
|
|
Loading…
Reference in New Issue