News for hackers. Stuff that matters.
Re:The Linux kernel's inability to gracefully handle low mem... by Crinus on Wednesday August 07, 2019 @06:05AM (#20633297)
In C you can return a struct, however a better approach is to use a context object which also contains error information, like:

    ctx_t* ctx = ctx_new();
    if (!ctx) ... fail ...
    if (!ctx_parse(ctx, code)) {
        show_error_message(ctx_erline(ctx), ctx_ercol(ctx));
        ... more fail ...
        ctx_free(ctx); /* often done in a goto'd section to avoid missing frees*/
    }
This also allows you to extend the APIs functionality, error information, etc in the future while remaining backwards compatible.
Re:The Linux kernel's inability to gracefully handle low mem... by machinecoffee on Thursday August 08, 2019 @06:53AM (#20643338)
Which is great, except that ctx_new() requires a malloc, which then can fail, and now you can't even explain why the thing failed, as you have no context info.

You also have to worry about all of the ctx objects you've created along the way, to free them up as you recover from the low memory error.

Re:The Linux kernel's inability to gracefully handle low mem... by alyandon on Wednesday August 07, 2019 @10:38AM (#20635067)
That is very similar to the way I handled errors back in my C days.
Re:The Linux kernel's inability to gracefully handle low mem... by bjourne on Thursday August 08, 2019 @03:35AM (#20642572)
Yep, you're absolutely right. But don't tell me that is simple! :)