<p>What is the single most effective practice to prevent <a href="http://en.wikipedia.org/wiki/Arithmetic_overflow" rel="nofollow noreferrer">arithmetic overflow</a> and <a href="http://en.wikipedia.org/wiki/Arithmetic_underflow" rel="nofollow noreferrer">underflow</a>?</p>

<p>Some examples that come to mind are:</p>

<ul>
<li>testing based on valid input ranges</li>
<li>validation using formal methods</li>
<li>use of invariants</li>
<li>detection at runtime using language features or libraries (this does not prevent it)</li>
</ul>

[{'answer_id': 78936, 'author': 'Eclipse', 'author_id': 8701, 'author_profile': 'https://Stackoverflow.com/users/8701', 'pm_score': 3, 'selected': True, 'text': '<p>One possibility is to use a language that has arbitrarily sized integers that never overflow / underflow.</p>\n\n<p>Otherwise, if this is something you\'re really concerned about, and if your language allows it, write a wrapper class that acts like an integer, but checks every operation for overflow.  You could even have it do the check on debug builds, and leave things optimized for release builds.  In a language like C++, you could do this, and it would behave almost exactly like an integer for release builds, but for debug builds you\'d get full run-time checking.</p>\n\n<pre><code>class CheckedInt\n{\nprivate: \n    int Value;\n\npublic:\n    // Constructor\n    CheckedInt(int src) : Value(src) {}\n\n    // Conversions back to int\n    operator int&amp;() { return Value; }\n    operator const int &amp;() const { return Value; }\n\n    // Operators\n    CheckedInt operator+(CheckedInt rhs) const\n    {\n        if (rhs.Value &lt; 0 &amp;&amp; rhs.Value + Value &gt; Value)\n            throw OverflowException();\n        if (rhs.Value &gt; 0 &amp;&amp; rhs.Value + Value &lt; Value)\n            throw OverflowException();\n        return CheckedInt(rhs.Value + Value);\n    }\n\n    // Lots more operators...\n};\n</code></pre>\n\n<p>Edit:</p>\n\n<p>Turns out someone is <a href="http://www.codeplex.com/SafeInt" rel="nofollow noreferrer">doing this already for C++</a> - the current implementation is focused for Visual Studio, but it looks like they\'re getting support for gcc as well.</p>\n'}, {'answer_id': 78941, 'author': 'itsmatt', 'author_id': 7862, 'author_profile': 'https://Stackoverflow.com/users/7862', 'pm_score': 1, 'selected': False, 'text': '<p>I write a lot of test code to do range/validity checking on my code.  This tends to catch most of these types of situations - and definitely helps me write more bulletproof code.</p>\n'}, {'answer_id': 78949, 'author': 'postfuturist', 'author_id': 1892, 'author_profile': 'https://Stackoverflow.com/users/1892', 'pm_score': 1, 'selected': False, 'text': '<p>Use high precision floating point numbers like a <a href="http://en.wikipedia.org/wiki/Long_double" rel="nofollow noreferrer">long double</a>.</p>\n'}, {'answer_id': 79074, 'author': 'Jörg W Mittag', 'author_id': 2988, 'author_profile': 'https://Stackoverflow.com/users/2988', 'pm_score': 1, 'selected': False, 'text': "<p>I think you are missing one very important option in your list: choose the right programming language for the job. There are many programming languages which do not have these problems, because they don't have fixed size integers.</p>\n"}, {'answer_id': 79470, 'author': 'pro3carp3', 'author_id': 7899, 'author_profile': 'https://Stackoverflow.com/users/7899', 'pm_score': 0, 'selected': False, 'text': "<p>There are more important considerations when choosing which language you use than the size of the integer.  Simply check your input if you don't know if the value is in bounds, or use exception handling if the case is extremely rare.</p>\n"}, {'answer_id': 1028197, 'author': 'mturquette', 'author_id': 123330, 'author_profile': 'https://Stackoverflow.com/users/123330', 'pm_score': 0, 'selected': False, 'text': "<p>A wrapper that checks for inconsistencies will make sense in many cases.  If an additive operation (ie, addition or multiplication) on two or more integers results in a smaller value than the operands then you know something went wrong.  Every additive operation should be followed by,</p>\n\n<pre><code>if (sum &lt; operand1 || sum &lt; operand2)\n    omg_error();\n</code></pre>\n\n<p>Likewise any operation that should logically result in a smaller value should be check to see if it was accidentally embiggin'd.</p>\n"}, {'answer_id': 9234479, 'author': 'Jay Abraham', 'author_id': 893216, 'author_profile': 'https://Stackoverflow.com/users/893216', 'pm_score': 0, 'selected': False, 'text': '<p>Have you investigated the use of formal methods to check your code to prove that it is free of overflows? A formal methods technique known as abstract interpretation can check the robustness of your software to prove that your software will not suffer from an overflow, underflow, divide by zero, overflow, or other similar run-time error.  It is a mathematical technique that exhaustively analyzes your software. The technique was pioneered by Patrick Cousot in the 1970s.  It was successfully used to diagnose an overflow condition in the Arian 5 rocket where an overflow caused the destruction of the launch vehicle.  The overflow was caused while converting a floating point number to an integer. You can find more information about this technique <a href="http://www.mathworks.com/discovery/formal-methods.html" rel="nofollow">here</a> and also on <a href="http://en.wikipedia.org/wiki/Abstract_interpretation" rel="nofollow">Wikipedia</a>.</p>\n'}]
['https://Stackoverflow.com/questions/78913', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3836/']