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

Make newer compilers not complain about string truncations that are not broken. #122

Open
rhaleblian opened this issue May 18, 2020 · 3 comments

Comments

@rhaleblian
Copy link
Contributor

Up to N chars are being copied into strings with less than N+1 chars of storage.

@rhaleblian
Copy link
Contributor Author

rhaleblian commented May 18, 2020

Many of these format-truncation messages are triggered by snprintf() calls which will in fact truncate as desired.

rhaleblian added a commit to plateofshrimp/linapple that referenced this issue May 18, 2020
GCC quieted for cases where snprintf() truncates source strings.
@rhaleblian rhaleblian changed the title Close string overrun leaks Patch string overrun leaks May 18, 2020
@rhaleblian rhaleblian changed the title Patch string overrun leaks Patch string buffer overrun leaks. May 18, 2020
@ThorstenBr
Copy link
Contributor

The n+1 thing is not required. snprintf(…,n,...) prints at most n characters to the buffer, including the terminating null character. So the previous definition of "char fullPath[MAX_PATH];" etc was already sufficient.

See:
https://www.cplusplus.com/reference/cstdio/snprintf/

int snprintf ( char * s, size_t n, const char * format, ... );
The generated string has a length of at most n-1, leaving space for the additional terminating null character.

@rhaleblian
Copy link
Contributor Author

rhaleblian commented May 21, 2020

It makes sense. C code all over the world would be leaking otherwise.
Fixed the PR so that it only does a dance to convince GCC that truncation is being handled responsibly.

@rhaleblian rhaleblian changed the title Patch string buffer overrun leaks. Make newer compilers not complain about string truncations that are not broken. May 21, 2020
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

No branches or pull requests

2 participants