Login
You're viewing the functional.cafe public feed.
  • Jun 26, 2026, 10:13 AM

    The pattern how I write C function is the following:

    ```c
    int project_module_verb(module_t speaking_name, args) {
    int rv = 0;

    // If precondition checks fail, set just return
    <preconditon checks>

    // if something goes wrong here, set error code and goto out.
    <alter state>

    out:
    if (rv < 0) {
    <recover to error state>
    }
    <cleanup>
    return rv;
    }
    ```

    `rv` stands for "return value", a pattern that I got used to while working on xbps.

    💬 1🔄 0⭐ 0

Replies

  • Jun 26, 2026, 10:15 AM

    I'm using a slighlty different variant of this, if the function returns a pointer:

    pointer_t *project_module_verb(module_t speaking_name, args..., int *err) {
    // same as above

    out:
    if (err != NULL) {
    *err = rv;
    }
    if (rv < 0) {
    <recover to error state>
    }
    <cleanup>

    return result;
    }

    💬 0🔄 0⭐ 0
  • Jun 26, 2026, 4:28 PM

    @Gottox That tracks. Depending on the function, I've found I don't need a variable to hold the rv: I know what value I'll return on success and also in every error case, so I just write the value verbatim with the return statement.

    Granted, the error codes I use are mostly stored in errno, which makes tracking the return value simpler since there's only one for all errors.

    💬 1🔄 0⭐ 0
  • 💬 1🔄 0⭐ 0
  • Jun 28, 2026, 1:27 PM

    @Gottox Not once since I switched to musl 😝 (it uses "No error information" for errno code 0)

    But it really depends on what you're doing. It's rare that I have to report an error from user code; mostly I'm reporting errors from system calls, which always set errno.
    It's just a habit to have, I don't find it more or less fragile than anything else.

    💬 1🔄 0⭐ 0
  • Jun 28, 2026, 1:36 PM
    @ska @Gottox i find it awkward when i have my own error types

    so i either need to wrap every possible syscall errno into them, or have a LIBFOO_ERROR_SYSTEM as say "when a system error is returned, check errno" which feels... awkward? but maybe it's fine
    💬 2🔄 0⭐ 0
  • Jun 28, 2026, 1:41 PM

    @navi @Gottox yeah at low levels errno is good; at higher levels I sometimes use errno as well when the codes map relatively well to the semantics I want. Sometimes it's just not possible, in which case I need to define specific error codes, but this is annoying to do and even more annoying to document; I try to avoid that out of sheer laziness.

    Note that LIBFOO_ERROR_SYSTEM is exactly what getaddrinfo() does: it has an EAI_SYSTEM error value. I think it's fine for this kind of function; impractical, yes, but about the best you can do.

    💬 0🔄 0⭐ 0
  • 💬 0🔄 0⭐ 0
  • Jun 28, 2026, 7:27 PM

    @Gottox @navi Most implementations of getaddrinfo() use negative values for EAI_* codes, so it's still possible to encode everything in one byte 😅 But yeah, in any case you need some kind of trick, and yours is smart as well.

    💬 0🔄 0⭐ 0