On 12/05/2016 11:52 AM, Martin Sebor wrote:
What's the concern with using std::abs?
My concern, when I wrote the reply n Friday, was that not all C++98
implementations may get std::abs right, declare it in the right header,
avoid defining the abs macro, or put it in namespace std. (IIRC,
the standard itself wasn't quite right.)
I also need to avoid calling abs with a TYPE_MIN argument because
that's undefined and flagged by ubsan (as per the bug in the subject,
though it was not a result of calling abs but rather that of negating
it).
I'm less concerned about the older C++ implementations as I am about the
TYPE_MIN overflow. Thanks for clarifying.
Besides avoiding the undefined behavior in the compiler I also need
diagnose it (in the program). The test case for it goes like this:
int n = sprintf (0, 0, "%*i", INT_MIN, 0);
where the INT_MIN is interpreted as the left justification flag
followed by a positive width of -(unsigned long)INT_MIN. The
problem is that the function (declared to return int0 is being
asked to return INT_MAX + 1 which is undefined (in the program).
Understood. Thanks again.
jeff