News for hackers. Stuff that matters.
Re:Checked integer arithmetic in the prospect of C23 by Someone on Monday December 19, 2022 @11:31AM (#34054007)
FTA: “Their working is quite simple: the arithmetic is as if performed in the set of mathematical integers and then the value is written to result. If it fits, the return value is false. If it doesn’t fit, the return value is true”*

They also give example code

  bool add_invalid = ckd_add(&result_add, a, b);
I can see that fits with “most of the time, anything positive means ‘no error’”, for example in malloc, write, read or printf, but these new functions return bool, not int, and the chosen method will require writing a double negation sometimes:

  if(!add_invalid) { … }
That’s not too bad, but if I were to see

  if(!ckd_add(&result_add, a, b)) { … }
I would expect that to test for failure, not success.

Because of that, I think I would have chosen to return true on success, false on failure. I’m curious as to what arguments led to the choice made.

Re:Checked integer arithmetic in the prospect of C23 by chongli on Monday December 19, 2022 @11:44AM (#34054200)
In C, the value 0 is equivalent to false and all nonzero values are equivalent to true. It’s a convention throughout the C standard library to return 0 on success and nonzero when some error occurred. The behaviour of the new checked arithmetic library is consistent with that convention.
Re:Checked integer arithmetic in the prospect of C23 by Someone on Monday December 19, 2022 @12:35PM (#34054895)
> It’s a convention throughout the C standard library to return 0 on success and nonzero when some error occurred.

If only it were so simple. read and write, for example, return a number less than zero on error and a non-negative number on success, and malloc returns zero on error, and nonzero on success.

The general rule for early C seems to be “whatever’s the best way to cram a return value or an error in an int” (probably the correct decision for the time)

Also, these new functions return a bool, which, in C23, gets integer-converted to zero for false and one for true (https://en.cppreference.com/w/c/language/bool_constant. C17 had macros for true and false, with false being zero)

and the reverse, converting to bool similarly has zero fro false (https://en.cppreference.com/w/c/language/conversion#Boolean_...):

“A value of any scalar type (including nullptr_t) (since C23) can be implicitly converted to _Bool. The values that compare equal to an integer constant expression of value zero are converted to 0 (false), all other values are converted to 1 (true).”

(https://en.cppreference.com/w/c/language/bool_constant)

Re:Checked integer arithmetic in the prospect of C23 by dahfizz on Monday December 19, 2022 @12:53PM (#34055160)
_when the return value is an error code_, zero means success and nonzero means failure. Functions like `read`, `recv`, etc etc don't just return an error code. They return an actual value.

Functions that only return an error code like `stat`, `connect`, and the proposed ckd_add, return 0 on success and nonzero on error.

Re:Checked integer arithmetic in the prospect of C23 by SAI_Peregrinus on Monday December 19, 2022 @01:27PM (#34055639)
And in shell it's convention for programs to return 0 for success and nonzero when an error occurred. The issue is that in shell, the `true` builtin returns `0` and `false` returns `1`, which is the opposite of C's `bool`. And almost every other language's Boolean type.
Re:Checked integer arithmetic in the prospect of C23 by gustedt on Monday December 19, 2022 @04:05PM (#34057700)
Unfortunately there are several error conventions in the C standard.

Here, the committee just standardized existing practice, namely the gcc builtins. We just adjusted the call sequence in putting the pointer parameter for the result first.

Re:Checked integer arithmetic in the prospect of C23 by dooglius on Monday December 19, 2022 @12:18PM (#34054661)
I find the most readable, unambiguous thing is to define explicit macros or constants, e.g.

  if(ckd_add(&result_add, a, b) == CKD_SUCCESS) { ... }
or alternatively,

  if(CKD_SUCCESS(ckd_add(&result_add, a, b))) { ... }
Re:Checked integer arithmetic in the prospect of C23 by kevin_thibedeau on Monday December 19, 2022 @04:17PM (#34057838)
The return value is the overflow condition so just name it CKD_OVERFLOW.
Re:Checked integer arithmetic in the prospect of C23 by dahfizz on Monday December 19, 2022 @12:50PM (#34055109)
The proposal fits my intuition. When the return value is an error code, truthy values are an error.

    int rc = func();
    if( rc ) { /*handle error*/ }

examples from the stdlib: connect(), stat(), etc. Hell, even main is defined to return 0 on success, nonzero on error.
Re:Checked integer arithmetic in the prospect of C23 by RustyRussell on Monday December 19, 2022 @05:17PM (#34058529)
Not for bool though.
Re:Checked integer arithmetic in the prospect of C23 by RustyRussell on Monday December 19, 2022 @05:16PM (#34058521)
Yes, it's backwards. And counter-intuitive use of bool :(

They felt fine changing the argument order, why stick with the reverse polarity?