by fulafel · 9y ago · 0 comments
int add(int i, int j) {
return i + j;
}Or std::vector<T>::operator[] invokes undefined behaviour if the argument is invalid (out of band).
Or printf() invokes undefined behaviour if an argument is of the wrong type (for example "%s" with a corresponding int).
Or strcpy invokes undefined behaviour if you give it invalid pointers (or non-null terminated strings, etc).
So, yeah, many functions can invoke undefined behaviour when they are called with bad arguments, that doesn’t make them bad functions.
The Python vs C++ joke here is that the minimal a + b example turns out to be unsafe to call with random numbers.
Take any language's FFI and bind to the C library atoi function. Poof, you have instant undefined behavior for bad string to int conversions.
If you want a safe, robust module that is based on FFI, you have to write some padding in the higher level language that avoids misusing the foreign API in any way.
Directly exposed FFI stuff is not safe and cannot reasonably be safe; it makes no sense to expect that.
I wonder if the "best" fix here, would be to use assembler intrinsics to maintain performance, possibly with a fallback to a type cast/promotion to a bigger int for the sum, and some form of defined behaviour for overflow on the python side? (reinventing the python number tower with full automatic promotion seems counter-productive here, as presumably the idea is to work with fast/simple/native (signed) integer math...)
Or just redefine the function to use unsigned ints.
add(INT64_MAX, 1)
Is undefined.If using Ada, similar kind of code would produce "raised CONSTRAINT_ERROR : program.adb:7 overflow check failed"
To be fair, Ada's behavior is easy to reproduce with a checked integer class like https://accu.org/index.php/journals/324
There millions of lines of code, from the best C/C++ programmers, even the kernel, that add two ints in all kinds of programs.
Why is this suddenly a valid concern, especially for a code example, not NASA's missile code or Tesla's self-driving libs?
"Many years later we asked our customers whether they wished us to provide an option to switch off these checks in the interests of efficiency on production runs. Unanimously, they urged us not to--they already knew how frequently subscript errors occur on production runs where failure to detect them could be disastrous. I note with fear and horror that even in 1980, language designers and users have not learned this lesson. In any respectable branch of engineering, failure to observe such elementary precautions would have long been against the law."
-- Tony Hoare, "The 1980 ACM Turing Award Lecture"
Millions of people also drive without security belt or helmet, apparently they are an useless extra.
Yeah, there are places in code like that, and many of those lines have been security vulnerabilities.
For anyone who is surprised by this and is handling untrusted data in C/C++, it's a good idea to read up on the subject.
It's been a valid concern for years. I'm not sure where you get "suddenly" from.
And where do you think the people writing NASA or Tesla code tomorrow are learning code from today?
Man, these languages are seriously !!fun!!
CL-USER> (sqrt -1)
#C(0.0 1.0)(There isn't a way in C/C++ to request an exception or fault on integer overflow, which is what you'd want here, since you're running inside Python. A better conceptual fix is just doing the addition in Python, which is a more robust language than C/C++ for signed integer addition, but that kind of defeats the point of the example.)
This is incorrect.
See compiler options: -ftrapv, -fsanitize=undefined. See compiler builtins: __builtin_add_overflow and friends.