Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/vf_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "vf_common.h"

#include "astutils.h"
#include "errortypes.h"
#include "library.h"
#include "mathlib.h"
#include "path.h"
Expand Down Expand Up @@ -137,6 +138,8 @@ namespace ValueFlow
setTokenValue(tok, std::move(value), settings);
} catch (const std::exception & /*e*/) {
// Bad character literal
} catch (const InternalError & /*e*/) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This catches the throw in valueflow. But TemplateSimplifier::simplifyCalculations (templatesimplifier.cpp:2766-2769) also calls MathLib::toBigNumber(tok) on a %char% token with no catch. Inside template arguments the invalid escape still gives an internal error:

template<bool B> struct V {};
V<('\s' == 1)> v;
$ ./cppcheck -q e4.cpp
e4.cpp:2:4: error: Internal Error. MathLib::toBigNumber: characterLiteralToLL('\s') => invalid escape sequence [internalError]

The same happens with U<('\s' > 3)> u2; for template<int N> struct U {};. To fix it, catch the error there too and leave the token unchanged, and add a test for it.

// Bad character literal
}
} else if (tok->isNumber() && MathLib::isFloat(tok->str())) {
// A long double literal out of the range of double (1e400L) has no
Expand Down
8 changes: 8 additions & 0 deletions test/testtokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ class TestTokenizer : public TestFixture {
TEST_CASE(simplifyEnum1);

TEST_CASE(simplifyEnum2);

TEST_CASE(nonStandardCharLiteral);
}

class TokenizerTest final : public Tokenizer
Expand Down Expand Up @@ -9547,6 +9549,12 @@ class TestTokenizer : public TestFixture {
ASSERT_EQUALS(tok->column(), 24);
ASSERT_EQUALS(tok->next()->column(), 16);
}

void nonStandardCharLiteral() {
const Settings s = settingsBuilder(settings2).severity(Severity::portability).build();
(void)tokenizeAndStringify("int a = '\\s';\n", s, false);
ASSERT_EQUALS("[test.c:1:9]: (portability) Non-standard character literal '\\s'. invalid escape sequence [nonStandardCharLiteral]\n", errout_str());
}
};

REGISTER_TEST(TestTokenizer)
Loading