isocpp / isocpp/CppCoreGuidelines

I.7: State postconditions - undefined behavior in first "consider using"

Open
#49 8 comments 0 reactions 2 assignees View on GitHub

@gdr-at-ms is already working on this.

Since Oct 10, 2016.

waiting
Dominant language
CSS
Stars
45.3k
Forks
5.6k
PR merge metrics
No merged PRs in 30d

Description

The solution to the first example is:

int area(int height, int width)
{
    auto res = height*width;
    Ensures(res>0);
    return res;
}

When the multiplication overflows, undefined behavior is invoked. This makes the Ensures(res>0) depend on undefined behavior.

I'm not sure what the correct solution should be like. Possibly something like clang's and gcc's __builtin_sadd_overflow or one of the techniques described in the book Hacker's Delight. The former would be clang/gcc-specific, the latter quite involved.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.