Skip to content

Conversation

firewave
Copy link
Collaborator

No description provided.

@firewave firewave force-pushed the sign-index branch 2 times, most recently from d63e081 to 1899482 Compare August 14, 2025 10:44
@firewave firewave changed the title fixed #13673 - fixed signedCharArrayIndex detection fixed #13673/#14076 - fixed signedCharArrayIndex detection Aug 15, 2025
@firewave firewave marked this pull request as ready for review August 15, 2025 08:39
Copy link

" 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());
Copy link
Owner

@danmar danmar Aug 22, 2025

Choose a reason for hiding this comment

The 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.
In this case it's a known value so imho a "error" would be fine.

@@ -2023,7 +2023,7 @@ void CheckOther::checkCharVariable()
if (!tok->variable()->isArray() && !tok->variable()->isPointer())
continue;
const Token *index = tok->next()->astOperand2();
if (warning && tok->variable()->isArray() && astIsSignedChar(index) && index->getValueGE(0x80, *mSettings))
if (warning && astIsSignedChar(index) && index->getValueLE(-1, *mSettings))
Copy link
Owner

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:

 void foo() {
      int buf[1000];
      int *p = buf + 512;
      signed char c = 0x80;
      p[c] = 123;
 }

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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:

 void foo() {
      int buf[1000];
      int *p = buf + 512;
      signed char c = 0x80;
      p[c] = 123;
 }

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 signed you will get a -Wchar-subscripts Clang warning though.

Copy link
Owner

Choose a reason for hiding this comment

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

I would not call it "safe".

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".

Copy link
Collaborator Author

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 negativeIndex lurking around which might have overlap.

@firewave firewave marked this pull request as draft August 29, 2025 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants