Skip to content

Fix int documentation on overflow (which is UB) #23

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

Merged
merged 1 commit into from
Aug 28, 2017

Conversation

cousteaulecommandant
Copy link
Contributor

The documentation says that if you add 1 to an int variable whose value is 32767 it'll just "roll over" to -32768, but this is actually undefined behavior (for signed integers of any length) and can't be trusted in behaving consistently. Example:

boolean will_overflow(int x) {
    int y = x+1;
    return y < x; // compiler assumes this will never happen => will always return false
}

    will_overflow(INT_MAX)  // returns false instead of true

Notice that this is not the case for unsigned integer types, which are required to "roll over" as documented.

@cousteaulecommandant
Copy link
Contributor Author

Alternatively, reject this PR and instead fix Arduino so that it adds the -fwrapv flag, which makes signed integers behave in the currently documented way (which is probably easier to understand for inexperienced programmers so it might be a better option).

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

Successfully merging this pull request may close these issues.

3 participants