Skip to content

Commit 9ca6704

Browse files
committed
cppcheck-opensource#7162 Support multicharacter char literals.
1 parent e2b859b commit 9ca6704

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

lib/mathlib.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,13 @@ MathLib::bigint MathLib::characterLiteralToLongNumber(const std::string& str)
320320
return 0; // for unit-testing...
321321
if (str.size()==1)
322322
return str[0] & 0xff;
323-
if (str[0] != '\\')
324-
throw InternalError(0, "Internal Error. MathLib::toLongNumber: Unhandled char constant " + str);
323+
if (str[0] != '\\') {
324+
// C99 6.4.4.4
325+
// The value of an integer character constant containing more than one character (e.g., 'ab'),
326+
// or containing a character or escape sequence that does not map to a single-byte execution character,
327+
// is implementation-defined.
328+
return str[0] & 0xff;
329+
}
325330

326331
switch (str[1]) {
327332
case 'x':

test/testmathlib.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class TestMathLib : public TestFixture {
271271

272272
// from char
273273
ASSERT_EQUALS((int)('A'), MathLib::toLongNumber("'A'"));
274+
ASSERT_EQUALS((int)('A'), MathLib::toLongNumber("'ABC'"));
274275
ASSERT_EQUALS((int)('\0'), MathLib::toLongNumber("'\\0'"));
275276
ASSERT_EQUALS((int)('\r'), MathLib::toLongNumber("'\\r'"));
276277
ASSERT_EQUALS((int)('\x12'), MathLib::toLongNumber("'\\x12'"));

0 commit comments

Comments
 (0)