News for hackers. Stuff that matters.
Re:Catch-23: The New C Standard Sets the World on Fire by xigoi on Sunday April 02, 2023 @05:16AM (#35408650)
> all you have to do to avoid UB on signed integer overflow is check for overflow before doing the operation (and C23 finally adds features to do that for you).

…making your code practically unreadable, since you have to write ckd_add(ckd_add(ckd_mul(a,a),ckd_mul(ckd_mul(2,a),b)),ckd_mul(b,b)) instead of a * a + 2 * a * b + b * b.

Re:Catch-23: The New C Standard Sets the World on Fire by chongli on Sunday April 02, 2023 @06:11AM (#35408992)
That's not the correct syntax for the ckd_ operations. They take 3 operands, the first being a pointer to an integer where the result should be stored. And they return a bool, which you need to check in a conditional. If you're just going to throw out the bool and ignore the overflows, why bother with checked operations in the first place?
Re:Catch-23: The New C Standard Sets the World on Fire by xigoi on Sunday April 02, 2023 @06:45AM (#35409171)
Yeah, I realize that now. That's even worse. So you'll have to write something like

    int aa,twoa,twoab,bb,aaplustwoab,aaplustwoabplusbb;
    if (ckd_mul(a,a,&aa)) { return error; }
    if (ckd_mul(2,a,&twoa)) { return error; }
    // …
    if (ckd_add(aaplustwoab,bb,aaplustwoabplusbb)) { return error; }
    return aaplustwoabplusbb;
So ergonomic!

> If you're just going to throw out the bool and ignore the overflows, why bother with checked operations in the first place?

I'd expect the functions to return the result on success and crash on failure. Or better, raise an exception, but C doesn't have exceptions…

Re:Catch-23: The New C Standard Sets the World on Fire by chongli on Sunday April 02, 2023 @02:09PM (#35413403)
Why not just write:

    bool aplusb_sqr(int* c, int a, int b) {
        return c && ckd_add(c, a, b) && ckd_mul(c, *c, *c);
    }
Re:Catch-23: The New C Standard Sets the World on Fire by xigoi on Sunday April 02, 2023 @02:14PM (#35413451)
Obviously you could do that in this case, I just wanted to come up with a complicated formula.