]> git.ipfire.org Git - thirdparty/systemd.git/commit
stdio-util: give snprintf_ok() some love
authorLennart Poettering <lennart@poettering.net>
Wed, 18 Aug 2021 12:03:10 +0000 (14:03 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 19 Aug 2021 07:19:03 +0000 (09:19 +0200)
commit3832cb90ba1557d5fe0e24f0b280bfefe44a2406
treed1f482858d63af7a7971c535f4321462171b5195
parent12a7f04a2b9135a4751dba71e2f688525d7c93e7
stdio-util: give snprintf_ok() some love

as per docs snprintf() can fail in which case it returns -1. The
snprintf_ok() macro so far unconditionally cast the return value of
snprintf() to size_t, which would turn -1 to (size_t) INT_MAX,
presumably, at least on 2 complements system.

Let's be more careful with types here, and first check if return value
is positive, before casting to size_t.

Also, while we are at it, let's return the input buffer as return value
or NULL instead of 1 or 0. It's marginally more useful, but more
importantly, is more inline with most of our other codebase that
typically doesn't use booleans to signal success.

All uses of snprintf_ok() don't care for the type of the return, hence
this change does not propagate anywhere else.
src/basic/stdio-util.h