-
Notifications
You must be signed in to change notification settings - Fork 1.8k
C++: Fix CWE-119 memcpy tests #20876
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
base: main
Are you sure you want to change the base?
Conversation
sizeof(pointer) only gives the pointer size, not the buffer size, so use explicit 10/20 lengths in tests.cpp and update OverflowBuffer.expected to accept the resulting memcpy diagnostics. Signed-off-by: Mingjie Shen <[email protected]>
jketema
left a comment
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.
Thanks for noticing this. We would like to be a bit more explicit, see below. Otherwise this looks fine.
| memcpy(bigbuffer, smallbuffer, 10); // GOOD | ||
| memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read | ||
| memcpy(smallbuffer, bigbuffer, 10); // GOOD | ||
| memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write |
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.
We would prefer the following here:
| memcpy(bigbuffer, smallbuffer, 10); // GOOD | |
| memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read | |
| memcpy(smallbuffer, bigbuffer, 10); // GOOD | |
| memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write | |
| memcpy(bigbuffer, smallbuffer, sizeof(char) * 10); // GOOD | |
| memcpy(bigbuffer, smallbuffer, sizeof(char) * 20); // BAD: over-read | |
| memcpy(smallbuffer, bigbuffer, sizeof(char) * 10); // GOOD | |
| memcpy(smallbuffer, bigbuffer, sizeof(char) * 20); // BAD: over-write |
| memcpy(bigbuffer, smallbuffer, 10); // GOOD | ||
| memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read | ||
| memcpy(smallbuffer, bigbuffer, 10); // GOOD | ||
| memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write |
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.
We would prefer the following:
| memcpy(bigbuffer, smallbuffer, 10); // GOOD | |
| memcpy(bigbuffer, smallbuffer, 20); // BAD: over-read | |
| memcpy(smallbuffer, bigbuffer, 10); // GOOD | |
| memcpy(smallbuffer, bigbuffer, 20); // BAD: over-write | |
| memcpy(bigbuffer, smallbuffer, sizeof(char[10])); // GOOD | |
| memcpy(bigbuffer, smallbuffer, sizeof(char[20])); // BAD: over-read | |
| memcpy(smallbuffer, bigbuffer, sizeof(char[10])); // GOOD | |
| memcpy(smallbuffer, bigbuffer, sizeof(char[20])); // BAD: over-write |
sizeof(pointer) only gives the pointer size, not the buffer size, so use explicit 10/20 lengths in tests.cpp and update OverflowBuffer.expected to accept the resulting memcpy diagnostics.