-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #12422 (False positive: subtracting pointers in same struct) #5971
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5397,15 +5397,15 @@ class TestBufferOverrun : public TestFixture { | |
" return (&i)[1];\n" | ||
"}"); | ||
ASSERT_EQUALS( | ||
"[test.cpp:3] -> [test.cpp:3]: (error) The address of local variable 'i' is accessed at non-zero index.\n", | ||
"[test.cpp:3] -> [test.cpp:3]: (error) The address of variable 'i' is accessed at non-zero index.\n", | ||
errout.str()); | ||
|
||
check("int f(int j) {\n" | ||
" int i;\n" | ||
" return (&i)[j];\n" | ||
"}"); | ||
ASSERT_EQUALS( | ||
"[test.cpp:3] -> [test.cpp:3]: (warning) The address of local variable 'i' might be accessed at non-zero index.\n", | ||
"[test.cpp:3] -> [test.cpp:3]: (warning) The address of variable 'i' might be accessed at non-zero index.\n", | ||
errout.str()); | ||
|
||
check("int f() {\n" | ||
|
@@ -5464,7 +5464,17 @@ class TestBufferOverrun : public TestFixture { | |
" return m[0][1];\n" | ||
"}"); | ||
ASSERT_EQUALS( | ||
"[test.cpp:4] -> [test.cpp:5]: (error) The address of local variable 'x' is accessed at non-zero index.\n", | ||
"[test.cpp:4] -> [test.cpp:5]: (error) The address of variable 'x' is accessed at non-zero index.\n", | ||
errout.str()); | ||
|
||
check("int x = 0;\n" | ||
"int f() {\n" | ||
" std::map<int, int*> m;\n" | ||
" m[0] = &x;\n" | ||
" return m[0][1];\n" | ||
"}"); | ||
ASSERT_EQUALS( | ||
"[test.cpp:4] -> [test.cpp:5]: (error) The address of variable 'x' is accessed at non-zero index.\n", | ||
errout.str()); | ||
|
||
check("int f(int * y) {\n" | ||
|
@@ -5554,7 +5564,7 @@ class TestBufferOverrun : public TestFixture { | |
check("uint32_t f(uint32_t u) {\n" | ||
" return ((uint8_t*)&u)[4];\n" | ||
"}\n"); | ||
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str()); | ||
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (error) The address of variable 'u' is accessed at non-zero index.\n", errout.str()); | ||
|
||
check("uint32_t f(uint32_t u) {\n" | ||
" return reinterpret_cast<unsigned char*>(&u)[3];\n" | ||
|
@@ -5564,7 +5574,7 @@ class TestBufferOverrun : public TestFixture { | |
check("uint32_t f(uint32_t u) {\n" | ||
" return reinterpret_cast<unsigned char*>(&u)[4];\n" | ||
"}\n"); | ||
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str()); | ||
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (error) The address of variable 'u' is accessed at non-zero index.\n", errout.str()); | ||
|
||
check("uint32_t f(uint32_t u) {\n" | ||
" uint8_t* p = (uint8_t*)&u;\n" | ||
|
@@ -5576,7 +5586,7 @@ class TestBufferOverrun : public TestFixture { | |
" uint8_t* p = (uint8_t*)&u;\n" | ||
" return p[4];\n" | ||
"}\n"); | ||
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str()); | ||
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) The address of variable 'u' is accessed at non-zero index.\n", errout.str()); | ||
|
||
check("uint32_t f(uint32_t* pu) {\n" | ||
" uint8_t* p = (uint8_t*)pu;\n" | ||
|
@@ -5597,15 +5607,13 @@ class TestBufferOverrun : public TestFixture { | |
" char b;\n" | ||
"};\n" | ||
"void f() {\n" | ||
" X s;\n" | ||
" int* y = &s.a;\n" | ||
" const X s;\n" | ||
" const int* y = &s.a;\n" | ||
" (void)y[0];\n" | ||
" (void)y[1];\n" | ||
" (void)y[2];\n" | ||
"}\n"); | ||
ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:9]: (error) The address of local variable 'a' is accessed at non-zero index.\n" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's unfortunate that it does not warn anymore but I feel the implementation and message was sloppy:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. about these comments: I created ticket https://trac.cppcheck.net/ticket/12428 so we can act on those later when we have time. |
||
"[test.cpp:7] -> [test.cpp:10]: (error) The address of local variable 'a' is accessed at non-zero index.\n", | ||
errout.str()); | ||
TODO_ASSERT_EQUALS("error", "", errout.str()); | ||
} | ||
|
||
void checkPipeParameterSize() { // #3521 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this change makes sense overall, though there maybe some places expecting a variable(ie
getLifetimeVariable
). So I was always concerned about causing issues, but it looks like only one test failed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah wait nevermind, this is changing
a.x
to point toa
instead ofx
. I thought it was changing it to point toa.x
. This would be a better change, but I guess there is a lot more test failures.For a quick fix, it seems like #5970 is a better solution for a quick fix.