Integer overflow and SMT-solvers
yurichev.com
yurichev.com
Rust also makes it fairly easy to define a `newtype` wrapper, for when you need semantics different from the primitive types. You can, for example, define an integer wrapper that always panics on overlow (as opposed to only in debug builds, which is the default for Rust). Recently, I've used this pattern to define a networking "sequence number" type that does not implement ordering operators (less than, greater than, etc.), but instead provides `older_than`, `newer_than`, etc. I made the switch after correcting multiple bugs caused by implicit and incorrect assumptions of total ordering; making the switch uncovered several more of such bugs . Of course, such a pattern is possible in C/C++ as well, but it is not as ergonomic and thus not as common.
Yes, this is very common. For example, Google requires that C++ code always uses int instead of unsigned.
If the language creators put a feature there, use it.
I really don't buy the love that some have for using signed variables when they don't make sense.
As a start, you only need one comparison to check if it's within range, as it's always > 0
If you've heard of Frama-C, these issues are highlighted by automatically inserted checks.
Will it, or will it just fail and return null (which may crash later code if it doesn't check)? I believe the spec mandates the latter.
Many implementations do not conform to that definition. But it's also possible that the expression results in a value smaller than PTRDIFF_MAX (or similarly, small enough the operating system does not need to overcommit), and then even a malloc conforming to that definition is free to return a pointer to some virtual memory (committed or not).