0

How to code defensively against the Microsoft C26451 (arithmetic overflow) warning in my example? I feel like this should be trivial to fix. So frustrating!

    // Linear interpolation
    // target  - the target point, 0.0 - 1.0
    // x...    - two values {X1} {X2}
    inline static double Linear(float target, double x1, double x2) {
        return (target * x2) + ((1.0 - (double)target) * x1);
    }

I read through Arithmetic overflow: Using operator '*' on a 4 byte value then casting the result to a 8 byte value but can't seem to fix my C26451 warning: "Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2).

What would I do to remove the warning?

The Microsoft doc on their compile error did not really help. https://learn.microsoft.com/en-us/cpp/code-quality/c26451

Lisa
  • 4,333
  • 2
  • 27
  • 34
  • 3
    What compiler options are you using to get that warning? I don't get it with `/W4` in VS2019. Sidenote: You don't need the cast to `double` in this: `(1.0 - (double)target)`. It'll be implicitly converted to a `double` anyway. – Ted Lyngmo Apr 27 '21 at 06:16
  • @TedLyngmo VS2019 but I'm using the Visual Studio 2015 toolset (v140) which ought to be the same as using VS2015. – Lisa Apr 27 '21 at 06:19
  • I only mentioned VS2019 because you tagged the question VS2017. Still, what compiler options are you using? FYI: `target` will (_should_) be implicitly converted to `double` in both places so I wonder _why_ it complains. Does it still complain if you take `target` as a `double` like this? `inline static double Linear(double target, double x1, double x2) { return (target * x2) + ((1.0 - target) * x1); }` – Ted Lyngmo Apr 27 '21 at 06:28
  • 1
    If I recall correctly, this is an Intellisense warning, not a compile one. It usualy takes a while for that software to notice you've fixed the problem – IWonderWhatThisAPIDoes Apr 27 '21 at 06:30
  • Oh, if it's intellisense (and not the compiler) that warns as @IWonderWhatThisAPIDoes mentions - just disregard it. I'd still try taking `target` as a `double` (and skip the casting) though. – Ted Lyngmo Apr 27 '21 at 06:31
  • @TedLyngmo Sorry I meant to tag this as VS2015 not 17. – Lisa Apr 27 '21 at 06:39
  • @TedLyngmo Yes still has warning if the target parameter is received as type double. – Lisa Apr 27 '21 at 06:40
  • @Lisa No problem. So, is it intellisense or the compiler that warns? – Ted Lyngmo Apr 27 '21 at 06:40
  • @TedLyngmo This is a compiler warning (that's why the Cxxx code I think). I get it on build (which succeeds); nothing underlined in the IDE. – Lisa Apr 27 '21 at 06:45
  • @Lisa Ok, I added a workaround that you could use until you upgrade to a newer Visual Studio version. – Ted Lyngmo Apr 27 '21 at 06:58

1 Answers1

2

The compiler warning doesn't make sense and newer Visual Studio versions does not give the same warning. I'd disable it for this line only:

inline static double Linear(double target, double x1, double x2) {
    #pragma warning( push )
    #pragma warning( disable : 26451 )
    return (target * x2) + ((1.0 - target) * x1);
    #pragma warning( pop )
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    This is an elegant way to squash the issue for the purpose of packaging up the code to pass on to someone else. Thank you! – Lisa Apr 28 '21 at 04:50