-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fixed #13673/#14076 - fixed signedCharArrayIndex detection
#7740
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
Draft
firewave
wants to merge
1
commit into
danmar:main
Choose a base branch
from
firewave:sign-index
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,14 @@ class TestCharVar : public TestFixture { | |
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("int buf[256];\n" | ||
| "void foo()\n" | ||
| "{\n" | ||
| " signed char ch = 0x80;\n" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("[test.cpp:5:5]: (warning) Signed 'char' type used as array index. [signedCharArrayIndex]\n", errout_str()); | ||
|
Owner
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 not required to change in this PR but I think the warning message should say that the index is negative. |
||
|
|
||
| check("int buf[256];\n" | ||
| "void foo()\n" | ||
| "{\n" | ||
|
|
@@ -71,7 +79,7 @@ class TestCharVar : public TestFixture { | |
| check("int buf[256];\n" | ||
| "void foo()\n" | ||
| "{\n" | ||
| " char ch = 0;\n" | ||
| " unsigned char ch = 0;\n" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
@@ -87,10 +95,17 @@ class TestCharVar : public TestFixture { | |
| check("int buf[256];\n" | ||
| "void foo()\n" | ||
| "{\n" | ||
| " char ch = 0x80;\n" | ||
| " char ch = 0;\n" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("[test.cpp:5:5]: (portability) 'char' type used as array index. [unknownSignCharArrayIndex]\n", errout_str()); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("int buf[256];\n" | ||
| "void foo(unsigned char ch)\n" | ||
| "{\n" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("int buf[256];\n" | ||
| "void foo(signed char ch)\n" | ||
|
|
@@ -106,13 +121,41 @@ class TestCharVar : public TestFixture { | |
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void foo(char* buf)\n" | ||
| "{\n" | ||
| " unsigned char ch = 0x80;" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void foo(char* buf)\n" | ||
| "{\n" | ||
| " signed char ch = 0x80;" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("[test.cpp:3:31]: (warning) Signed 'char' type used as array index. [signedCharArrayIndex]\n", errout_str()); | ||
|
|
||
| check("void foo(char* buf)\n" | ||
| "{\n" | ||
| " char ch = 0x80;" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("[test.cpp:3:24]: (portability) 'char' type used as array index. [unknownSignCharArrayIndex]\n", errout_str()); | ||
|
|
||
| check("void foo(char* buf)\n" | ||
| "{\n" | ||
| " unsigned char ch = 0;" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void foo(char* buf)\n" | ||
| "{\n" | ||
| " signed char ch = 0;" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void foo(char* buf)\n" | ||
| "{\n" | ||
| " char ch = 0;" | ||
|
|
@@ -126,6 +169,18 @@ class TestCharVar : public TestFixture { | |
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void foo(char* buf, unsigned char ch)\n" | ||
| "{\n" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void foo(char* buf, signed char ch)\n" | ||
| "{\n" | ||
| " buf[ch] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void foo(char* buf, char ch)\n" | ||
| "{\n" | ||
| " buf[ch] = 0;\n" | ||
|
|
@@ -135,7 +190,7 @@ class TestCharVar : public TestFixture { | |
| check("int flags[256];\n" | ||
| "void foo(const char* str)\n" | ||
| "{\n" | ||
| " flags[*str] = 0;\n" | ||
| " flags[(signed char)*str] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
|
|
@@ -146,6 +201,13 @@ class TestCharVar : public TestFixture { | |
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("int flags[256];\n" | ||
| "void foo(const char* str)\n" | ||
| "{\n" | ||
| " flags[*str] = 0;\n" | ||
| "}"); | ||
| ASSERT_EQUALS("", errout_str()); | ||
|
|
||
| check("void foo(const char str[])\n" | ||
| "{\n" | ||
| " map[str] = 0;\n" | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 fear there will be false positives when there is not an array. I.e. this code is safe:
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.
With the array check there are false negatives. It also matches the code for the unknown signedness below. So that FP might apply to that, too. Will check later and if that is the case will file a ticket and would treat it as out-of-scope for now.
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.
Good catch. This is indeed reported. The index will be negative and the memory access still valid but I would not call it "safe".
But we do not care if the access is actually valid because if you reduce the offset, it would not be reported.
If you remove
signedyou will get a-Wchar-subscriptsClang warning though.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.
ok sure. it can very well be unintentional access. But it's not undefined behavior at least. And if it's not undefined behavior I don't feel that error/warning severity should be used. So I am not 100% against such warning but make it non-warning/error.. and maybe review the message doesn't it explicitly say "array index".
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.
Yes, this needs some more looking into as this is a rather confusing check. And there's also
negativeIndexlurking around which might have overlap.