From b1d6dcf5a5c5aa02843c026dede0638f77798cb4 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Mon, 29 Sep 2014 07:31:14 -0500 Subject: Do not format USEC_INFINITY as NULL systemctl would print 'CPUQuotaPerSecUSec=(null)' for no limit. This does not look right. Since USEC_INFINITY is one of the valid values, format_timespan() could return NULL, and we should wrap every use of it in strna() or similar. But most callers didn't do that, and it seems more robust to return a string ("infinity") that makes sense most of the time, even if in some places the result will not be grammatically correct. --- src/shared/time-util.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/shared') diff --git a/src/shared/time-util.c b/src/shared/time-util.c index 2dc01e6ed3..066ef973ac 100644 --- a/src/shared/time-util.c +++ b/src/shared/time-util.c @@ -279,11 +279,8 @@ char *format_timespan(char *buf, size_t l, usec_t t, usec_t accuracy) { assert(buf); assert(l > 0); - if (t == USEC_INFINITY) - return NULL; - - if (t <= 0) { - snprintf(p, l, "0"); + if (t == USEC_INFINITY || t <= 0) { + strncpy(p, t == USEC_INFINITY ? "infinity" : "0", l); p[l-1] = 0; return p; } @@ -628,7 +625,7 @@ int parse_sec(const char *t, usec_t *usec) { { "", USEC_PER_SEC }, /* default is sec */ }; - const char *p; + const char *p, *s; usec_t r = 0; bool something = false; @@ -636,6 +633,18 @@ int parse_sec(const char *t, usec_t *usec) { assert(usec); p = t; + + p += strspn(p, WHITESPACE); + s = startswith(p, "infinity"); + if (s) { + s += strspn(s, WHITESPACE); + if (*s != 0) + return -EINVAL; + + *usec = USEC_INFINITY; + return 0; + } + for (;;) { long long l, z = 0; char *e; -- cgit v1.2.3-54-g00ecf