Skip to content
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

Merged
merged 1 commit into from Feb 20, 2023

Conversation

jnidzwetzki
Copy link
Contributor

@jnidzwetzki jnidzwetzki commented Feb 16, 2023

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

@codecov
Copy link

codecov bot commented Feb 16, 2023

Codecov Report

Merging #5340 (46a6ada) into main (8a51a76) will decrease coverage by 0.01%.
The diff coverage is n/a.

@@            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     
Impacted Files Coverage Δ
src/nodes/hypertable_modify.c 71.19% <0.00%> (-24.02%) ⬇️
src/uuid.c 78.57% <0.00%> (-6.05%) ⬇️
tsl/src/remote/healthcheck.c 63.76% <0.00%> (-6.05%) ⬇️
src/planner/add_hashagg.c 48.95% <0.00%> (-5.46%) ⬇️
src/histogram.c 83.83% <0.00%> (-4.80%) ⬇️
src/extension_utils.c 90.74% <0.00%> (-4.72%) ⬇️
tsl/src/fdw/fdw_utils.c 80.76% <0.00%> (-4.65%) ⬇️
src/time_utils.c 93.72% <0.00%> (-3.78%) ⬇️
src/utils.h 80.00% <0.00%> (-3.34%) ⬇️
tsl/src/remote/copy_fetcher.c 85.19% <0.00%> (-3.02%) ⬇️
... and 192 more

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@
- strlcpy(E1, E2, NAMEDATALEN);
+ /* You are using strlcpy with NAMEDATALEN, please consider using NameData and namestrcpy instead. */
+ namestrcpy(&E1, E2);
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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!

Copy link
Contributor

@mkindahl mkindahl Feb 17, 2023

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.

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.

Copy link
Contributor Author

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.

@jnidzwetzki jnidzwetzki force-pushed the cocci_strlcpy branch 2 times, most recently from 0404f75 to dbe2353 Compare February 17, 2023 12:29
scripts/coccinelle.sh Outdated Show resolved Hide resolved
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants