Skip to content

remove undefined bahavior - #290

Merged
henryiii merged 2 commits into
CLIUtils:masterfrom
phlptp:checked_multiply_no_undef
Jul 29, 2019
Merged

henryiii merged 2 commits into
CLIUtils:masterfrom
phlptp:checked_multiply_no_undef

Conversation

@phlptp

@phlptp phlptp commented Jun 11, 2019

Copy link
Copy Markdown
Collaborator

change the checked_multiply function to not use undefined behavior to check for potential undefined behavior and wrapping.

See Issue #289

@phlptp phlptp added the bug label Jun 11, 2019
@codecov

codecov Bot commented Jun 11, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #290 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #290   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          12     12           
  Lines        2887   2894    +7     
=====================================
+ Hits         2887   2894    +7
Impacted Files Coverage Δ
include/CLI/Validators.hpp 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update dbd4933...7e99462. Read the comment docs.

@phlptp
phlptp force-pushed the checked_multiply_no_undef branch from aa41bae to 75b4aae Compare June 11, 2019 17:06
@himikof

himikof commented Jul 23, 2019 •

Copy link
Copy Markdown

I would like to point out that while the intent of this change is correct, the implementation fails in an edge case:

assert(checked_multiply(2, std::numeric_limits<int>::min() / 2));

I would suggest the following change, which should work for both signed and unsigned types (the second branch is never called for them):

    if (a > 0 == b > 0) {
        if ((std::numeric_limits<T>::max)() / absval(a) < absval(b))
          return false;
    }
    else {
        if ((std::numeric_limits<T>::min)() / absval(a) > -absval(b))
            return false;
    }

@henryiii

Copy link
Copy Markdown
Collaborator

@phlptp, will you add the test and fix from @himikof, or should I?

@phlptp

phlptp commented Jul 25, 2019

Copy link
Copy Markdown
Collaborator Author

I won't be able to get to it until tomorrow morning, if you want to wait I can do it then, otherwise go ahead. My one concern was the possibility of unused code warnings for the unsigned types.

@henryiii

Copy link
Copy Markdown
Collaborator

That's sooner than I could do it; I'm edging out a little time during the CoDaS-HEP workshop to catch up a little. Won't start having larger chunks of time till next week or so.

phlptp added 2 commits July 25, 2019 19:32
… check for potential undefined behavior and wrapping.
…signed numbers and min val correctly. This involved adding to templates to clear up warnings
@phlptp
phlptp force-pushed the checked_multiply_no_undef branch from 75b4aae to 7e99462 Compare July 26, 2019 12:50
@phlptp

phlptp commented Jul 27, 2019

Copy link
Copy Markdown
Collaborator Author

I think this is ready to go now. with the additions recommended by @himikof

@henryiii
henryiii merged commit ba7aac9 into CLIUtils:master Jul 29, 2019
@henryiii
henryiii deleted the checked_multiply_no_undef branch July 29, 2019 04:20
@henryiii henryiii added this to the v1.9 milestone Dec 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants