diff options
author | Michal Schmidt <mschmidt@redhat.com> | 2015-09-14 15:53:47 +0200 |
---|---|---|
committer | Michal Schmidt <mschmidt@redhat.com> | 2015-09-16 15:48:00 +0200 |
commit | a8b626100b88b50c6c73fccf81b278d64e7e25a2 (patch) | |
tree | 7c1e487c66797d6a73c1a0cc55a73294896489e5 /src | |
parent | 34c38d2aaa2535cb40d0157b0e4a84e6be72ee9a (diff) |
basic: nicer xsprintf and xstrftime assert messages
It's nicer if the assertion failure message from a bad use of xsprintf
actually mentions xsprintf instead of the expression the macro is
implemented as.
The assert_message_se macro was added in the previous commit as an
internal helper, but it can also be used for customizing assertion
failure messages like in this case.
Example:
char buf[10];
xsprintf(buf, "This is a %s message.\n", "long");
Before:
Assertion '(size_t) snprintf(buf, ELEMENTSOF(buf), "This is a %s
message.\n", "long") < ELEMENTSOF(buf)' failed at foo.c:6, function
main(). Aborting.
After:
Assertion 'xsprintf: buf[] must be big enough' failed at foo.c:6,
function main(). Aborting.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/time-util.h | 4 | ||||
-rw-r--r-- | src/basic/util.h | 4 |
2 files changed, 6 insertions, 2 deletions
diff --git a/src/basic/time-util.h b/src/basic/time-util.h index de881e8fe1..1af01541fc 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -112,6 +112,8 @@ bool timezone_is_valid(const char *name); clockid_t clock_boottime_or_monotonic(void); -#define xstrftime(buf, fmt, tm) assert_se(strftime(buf, ELEMENTSOF(buf), fmt, tm) > 0) +#define xstrftime(buf, fmt, tm) \ + assert_message_se(strftime(buf, ELEMENTSOF(buf), fmt, tm) > 0, \ + "xstrftime: " #buf "[] must be big enough") int get_timezone(char **timezone); diff --git a/src/basic/util.h b/src/basic/util.h index c7dff9a86d..8abaa740b2 100644 --- a/src/basic/util.h +++ b/src/basic/util.h @@ -374,7 +374,9 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(cpu_set_t*, CPU_FREE); cpu_set_t* cpu_set_malloc(unsigned *ncpus); -#define xsprintf(buf, fmt, ...) assert_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf)) +#define xsprintf(buf, fmt, ...) \ + assert_message_se((size_t) snprintf(buf, ELEMENTSOF(buf), fmt, __VA_ARGS__) < ELEMENTSOF(buf), \ + "xsprintf: " #buf "[] must be big enough") int files_same(const char *filea, const char *fileb); |