-
Notifications
You must be signed in to change notification settings - Fork 905
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
Added coccinelle rule to find strlcpy on NameData #5340
Conversation
a2080c1
to
9db53b7
Compare
Codecov Report
@@ Coverage Diff @@
## main #5340 +/- ##
==========================================
- Coverage 90.68% 90.67% -0.01%
==========================================
Files 225 225
Lines 45042 52016 +6974
==========================================
+ Hits 40848 47168 +6320
- Misses 4194 4848 +654
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
coccinelle/namedata.cocci
Outdated
@@ | ||
- strlcpy(E1, E2, NAMEDATALEN); | ||
+ /* You are using strlcpy with NAMEDATALEN, please consider using NameData and namestrcpy instead. */ | ||
+ namestrcpy(&E1, E2); |
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.
Not sure if you should have the &
there: the expressions are already pointers, so if you take the address of a pointer this will not work in most cases.
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.
My assumption was that the variable is also changed from char[]
to NameData
. In this case, the &
operator is required here. However, since the change of the variable type is not part of this rule and it depends on the context, I have removed the suggestion and only annotate the strlcpy
call.
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 after this change most likely E1
in its original will not work anymore...the rule does drag attention to it - and I think that's what it should do!
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.
My assumption was that the variable is also changed from
char[]
toNameData
. In this case, the&
operator is required here.
Exactly.
However, since the change of the variable type is not part of this rule and it depends on the context, I have removed the suggestion and only annotate the
strlcpy
call.
Well. Given that the NAMEDATALEN is part of the rule, I think it quite clear that the target is a pointer to a buffer with this length, so it makes sense to still suggest it as a transformation. Especially considering the fact that it is a strlcpy
. It is quite common to use memcpy
as well, but this will copy all the data and does not suffer from this problem.
I actually think that you can add the "struct" rule that you used before. It is quite clearly the case that when you store something like this in a structure, you should use NameData
. Local variables is a different story.
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 points. I added the suggestion of the transformation for strlcpy
and the rule for struct members. In addition, I enabled Coccinelle checks for *.h files in the newest version of this PR.
9db53b7
to
ebe2b0a
Compare
0404f75
to
dbe2353
Compare
295527e
to
0849aba
Compare
NameData is a fixed-size type of 64 bytes. Using strlcpy to copy data into a NameData struct can cause problems because any data that follows the initial null-terminated string will also be part of the data.
0849aba
to
46a6ada
Compare
NameData is a fixed-size type of 64 bytes. Using strlcpy to copy data into a NameData struct can cause problems because any data that follows the initial null-terminated string will also be part of the data.
Note:
Should be merged after #5336, #5345, and #5317
Disable-check: force-changelog-changed