-
Notifications
You must be signed in to change notification settings - Fork 241
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
Moar string stuff: snprintf(3) #796
Closed
alejandro-colomar
wants to merge
10
commits into
shadow-maint:master
from
alejandro-colomar:snprintf
Closed
Moar string stuff: snprintf(3) #796
alejandro-colomar
wants to merge
10
commits into
shadow-maint:master
from
alejandro-colomar:snprintf
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3447e35
to
9f9d8c4
Compare
d516d42
to
14c2c46
Compare
afbbae7
to
cdba035
Compare
fbfe7a7
to
71192a6
Compare
0e2232e
to
54a076a
Compare
54a076a
to
4c53738
Compare
4c53738
to
687285a
Compare
v2 changes:
|
v3:
|
dd9a7d9
to
664ecbf
Compare
v3 changes:
|
664ecbf
to
e6baaa7
Compare
v3b changes:
|
e6baaa7
to
81c4b98
Compare
v4 changes:
|
v5 changes:
|
e92c037
to
0716bef
Compare
v5b changes:
|
Queued after #854. |
0716bef
to
791873a
Compare
v6 changes:
|
791873a
to
b0bc924
Compare
v7 changes:
|
b0bc924
to
2e214f3
Compare
v8 changes:
|
If anyone thinks this has too many commits and prefers that I split it into several smaller PRs, feel free to suggest so. I kind of think it too. |
2e214f3
to
a72481e
Compare
v8b changes:
|
We don't need to terminate them manually after the call. Remove all that paranoid code, which in some cases was even wrong. While at it, let's do a few more things: - Use sizeof(buf) for the size of the buffer. I found that a few cases were passing one less byte (probably because the last one was manually zeroed later). This caused a double NUL. snprintf(3) wants the size of the entire buffer to properly terminate it. Passing the exact value hardcoded is brittle, so use sizeof(). - Align and improve style of variable declarations. This makes them appear in this diff, which will help review the patch. Signed-off-by: Alejandro Colomar <[email protected]>
It wraps snprintf(3) so that it performs some steps that one might forget, or might be prone to accidents: - It calculates the size of the destination buffer, and makes sure it's an array (otherwise, using sizeof(s) would be very bad). - It calculates if there's truncation or an error, returning -1 if so. BTW, this macro doesn't have any issues of double evaluation, because sizeof() doesn't evaluate its argument (unless it's a VLA, but then the static_assert(3) within NITEMS() makes sure VLAs are not allowed). This macro is very similar to STRTCPY(), defined in lib/strtcpy.h. Signed-off-by: Alejandro Colomar <[email protected]>
Group them at the end of the list of variable definitions, and use '#if defined()' instead of '#if[n]def'. Also indent nested ones. Signed-off-by: Alejandro Colomar <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
The variable declarations for the buffers have been aligned in this commit, so that they appear in the diff, making it easier to review. Some important but somewhat tangent changes included in this commit: - lib/nss.c: The size was being defined as 65, but then used as 64. That was a bug, although not an important one; we were just wasting one byte. Fix that while we replace snprintf() by SNPRINTF(), which will get the size from sizeof(), and thus will use the real size. Signed-off-by: Alejandro Colomar <[email protected]>
These wrappers are like [v]snprintf(), but exit on failure. The macro calculates the size of the array internally (and guarantess that it is really an array), for added safety. Signed-off-by: Alejandro Colomar <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
These functions are like [v]snprintf(3), but return -1 on truncation, which makes it easier to test. snprintf(3) is iseful in two cases: - We don't care if the output is truncated. snprintf(3) is fine for those, and the return value can be ignored. - Truncation is bad. In that case, it's as bad as a hard error (-1) from snprintf, so merging both problems into the same error code makes it easier to handle errors. Return the length if no truncation so that we can use it if necessary. Signed-off-by: Alejandro Colomar <[email protected]>
And where we don't need snprintf_(), remove the cast to (void), since using snprintf(3) and not one of its wrappers necessarily means that the return value is ignored. By this, I'm not claiming that those calls to snprintf(3) without error handling are correct; it's just that I'm not sure, so I leave them alone. Signed-off-by: Alejandro Colomar <[email protected]>
Signed-off-by: Alejandro Colomar <[email protected]>
a72481e
to
42495a5
Compare
v8c changes:
|
I'm splitting it into smaller PRs, starting at #864. I'll close this one. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Merge after #793 (done)