mirror of
https://github.com/systemd/systemd.git
synced 2025-01-11 09:18:07 +03:00
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.
This commit is contained in:
parent
12a7f04a2b
commit
3832cb90ba
@ -9,8 +9,13 @@
|
||||
#include "macro.h"
|
||||
#include "memory-util.h"
|
||||
|
||||
#define snprintf_ok(buf, len, fmt, ...) \
|
||||
((size_t) snprintf(buf, len, fmt, __VA_ARGS__) < (len))
|
||||
#define snprintf_ok(buf, len, fmt, ...) \
|
||||
({ \
|
||||
char *_buf = (buf); \
|
||||
size_t _len = (len); \
|
||||
int _snpf = snprintf(_buf, _len, (fmt), __VA_ARGS__); \
|
||||
_snpf >= 0 && (size_t) _snpf < _len ? _buf : NULL; \
|
||||
})
|
||||
|
||||
#define xsprintf(buf, fmt, ...) \
|
||||
assert_message_se(snprintf_ok(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__), "xsprintf: " #buf "[] must be big enough")
|
||||
|
Loading…
Reference in New Issue
Block a user