]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic: nicer xsprintf and xstrftime assert messages 1278/head
authorMichal Schmidt <mschmidt@redhat.com>
Mon, 14 Sep 2015 13:53:47 +0000 (15:53 +0200)
committerMichal Schmidt <mschmidt@redhat.com>
Wed, 16 Sep 2015 13:48:00 +0000 (15:48 +0200)
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.

src/basic/time-util.h
src/basic/util.h

index de881e8fe14d7a87636e4a4ff3f4812e3f812f29..1af01541fc23dc85426b86430c52b7ee41e4b4c8 100644 (file)
@@ -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);
index c7dff9a86d8a3f3b4f532fda891f17e21c9c907d..8abaa740b22d32048a081dc56080a812f7d7d7a5 100644 (file)
@@ -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);