News for hackers. Stuff that matters.
Re:Errors and Exceptions by bluetomcat on Friday August 21, 2015 @04:34AM (#10096506)
> Much of the trouble with error returns comes from the strange C convention that functions with return values can be called as if they didn't return a value.

The "warn_unused_result" GCC function attribute makes the compiler emit a warning when you do `func(something);`: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attribute...

Re:Errors and Exceptions by w0000t on Friday August 21, 2015 @05:24AM (#10096625)
The problem is it needs to be applied to every function that returns a value. And I'm pretty sure you can't apply it to C library functions. Gcc seriously needs a global flag for this.
Re:Errors and Exceptions by jcranmer on Friday August 21, 2015 @12:19PM (#10098496)
You might be surprised at the warnings you'd get. Did you know, for example, that printf returns a value? It's also a return value that rarely, if ever, tends to be useful.
Re:Errors and Exceptions by bluetomcat on Friday August 21, 2015 @12:44PM (#10098688)
As a joke, in case you are really paranoic about error handling, you might end up with this :-)

    int written = printf("Got here");

    if (written != sizeof("Got here") - 1) {
        written = fprintf(stderr, "Could not print my sacred message\n");

        if (written != sizeof("Could not print my sacred message\n") - 1) {
            exit(I_DONT_KNOW_WTF_IS_HAPENNING_HERE);
        }
    }
Re:Errors and Exceptions by w0000t on Friday August 21, 2015 @01:42PM (#10099055)
That sort of thing is actually a completely valid approach.

You made a small mistake, since fprintf and printf call the same underlying code, the error handling should be either log the error in any way still available, and then depending on the design of the whole program, exit or continue. If printf fails you know exactly what is going on or have to exit. ( And printf would be wrapped in a function if you plan to use it repeatedly. )

Re:Errors and Exceptions by w0000t on Friday August 21, 2015 @01:44PM (#10099064)
Yes, I did. But so far I haven't been regularly checking printf return value. :-( I wonder if checking the result of every printf or asserting it, depending on the circumstances, would reduce the amount of time spent on writing+debugging the code on the long run.