-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add guideline to not invoke undefined behavior #2298
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
Open
rothmichaels
wants to merge
1
commit into
isocpp:master
Choose a base branch
from
rothmichaels:feature/no-ub
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+37
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
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.
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.
Question: How to you manage things like POSIX standard functions that have undefined behavior but must be used in the code for one reason or another?
Granted this might effect C code more than C++; but most code bases are probably more mixed between the two.
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.
Oh good question I hadn't thought about this. Can you provide some examples for me to think about? The only one that comes to mind off the top of my head is casting the
void*fromdlsymto a function pointer.Uh oh!
There was an error while loading. Please reload this page.
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.
@rothmichaels ones that easily come to mind are the POSIX printf family of functions since I've managed them across platforms before. For example,
snprintfhas some undefined behavior around the buffer provided, the output buffer, and the return values for being able to dynamically determine the size of the buffer needed to hold the output contents. Windows vs Linux vs Mac vs any Unix may operate differently on these methods. Granted, C++ advises using streams instead; but you there may still be requirements for projects to interface with these or similar and they're required to be available for POSIX compliant systems.Some references, though they more reference C99 and C89 than POSIX; but the methods are POSIX required:
https://linux.die.net/man/3/snprintf
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=msvc-170
For Microsoft, see snprintf vs _snprintf - both are POSIX compliant; one is C99 compliant which does help, but it comes down to what does your platform support since POSIX defines some things as UNDEFINED as a result of the history of UNIX and building a standard.
Uh oh!
There was an error while loading. Please reload this page.
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.
Eh? I don't think the suggested guideline means "don't use functions which can ever have UB under some set of conditions". So it's not saying "never use snprintf".
It's saying don't use snprintf in such a way that invokes undefined behaviour.
So using snprintf is ok, using it incorrectly is not.