Skip to content

Commit 5781ffc

Browse files
committed
Fix #14180 FN intToPointerCast with binary integer literal
1 parent 2285c1d commit 5781ffc

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

lib/checkother.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,8 @@ void CheckOtherImpl::warningIntToPointerCast()
464464
format = "decimal";
465465
else if (MathLib::isOct(from->str()))
466466
format = "octal";
467+
else if (MathLib::isBin(from->str()))
468+
format = "binary";
467469
else
468470
continue;
469471
intToPointerCastError(tok, format);

test/testother.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,6 +2362,9 @@ class TestOther : public TestFixture {
23622362
checkIntToPointerCast("struct S { int i; };\n" // #13886, don't crash
23632363
"int f() { return sizeof(((struct S*)0)->i); }");
23642364
ASSERT_EQUALS("", errout_str());
2365+
2366+
checkIntToPointerCast("auto p = (int*)0b10;"); // #14180
2367+
ASSERT_EQUALS("[test.cpp:1:10]: (portability) Casting non-zero binary integer literal to pointer. [intToPointerCast]\n", errout_str());
23652368
}
23662369

23672370
struct CheckInvalidPointerCastOptions
@@ -4864,6 +4867,33 @@ class TestOther : public TestFixture {
48644867
ASSERT_EQUALS("[test.cpp:1:12]: (style) Parameter 'p' can be declared as pointer to const [constParameterPointer]\n"
48654868
"[test.cpp:1:20]: (style) Parameter 'q' can be declared as pointer to const [constParameterPointer]\n",
48664869
errout_str());
4870+
4871+
check("struct S {\n" // #14817
4872+
" explicit S(int *a) : m{ a[0], a[1] } {}\n"
4873+
" int m[2];\n"
4874+
"};"
4875+
"struct T {\n"
4876+
" explicit T(int *a) : m{ &a[0], &a[1] } {}\n"
4877+
" int* m[2];\n"
4878+
"};\n"
4879+
"struct X{ int& r1, &r2; }\n"
4880+
"int f(int* a) {\n"
4881+
" S m[2]{a[0], a[1]};\n"
4882+
" return m[0].r1 + m[1].r2;\n"
4883+
"}\n");
4884+
ASSERT_EQUALS("[test.cpp:2:21]: (style) Parameter 'a' can be declared as pointer to const [constParameterPointer]\n",
4885+
errout_str());
4886+
4887+
check("class A {\n" // #11471
4888+
"public:\n"
4889+
" A(const int& i, int) : m_i(&i) {}\n"
4890+
" const int* m_i;\n"
4891+
"};\n"
4892+
"A f(int& s) {\n"
4893+
" return A(s, 0);\n"
4894+
"}\n");
4895+
ASSERT_EQUALS("[test.cpp:6:10]: (style) Parameter 's' can be declared as reference to const [constParameterReference]\n",
4896+
errout_str());
48674897
}
48684898

48694899
void constArray() {

0 commit comments

Comments
 (0)