Blame SOURCES/0574-timedatectl-stop-using-xstrftime.patch

17b0f1
From add02d6e5934100f023b45584d0227be90a297e8 Mon Sep 17 00:00:00 2001
17b0f1
From: Jan Synacek <jsynacek@redhat.com>
17b0f1
Date: Thu, 19 Oct 2017 09:53:56 +0200
17b0f1
Subject: [PATCH] timedatectl: stop using xstrftime
17b0f1
MIME-Version: 1.0
17b0f1
Content-Type: text/plain; charset=UTF-8
17b0f1
Content-Transfer-Encoding: 8bit
17b0f1
17b0f1
When using strftime in arbitrary locales, we cannot really say how big the
17b0f1
buffer should be. Let's make the buffer "large", which will work fine pretty
17b0f1
much always, and just print n/a if the timestamp does not fit. strftime returns
17b0f1
0 if the buffer is too small and a NUL-terminated string otherwise, so we
17b0f1
can drop the size specifications in string formatting.
17b0f1
17b0f1
$ export LANG=fa_IR.UTF-8
17b0f1
$ date
17b0f1
چهارشنبه ۱۸ اكتبر ۱۷، ساعت ۱۰:۵۴:۲۴ (+0330)
17b0f1
$ timedatectl
17b0f1
Assertion 'xstrftime: a[] must be big enough' failed at ../src/timedate/timedatectl.c:105, function print_status_info(). Aborting.
17b0f1
17b0f1
now:
17b0f1
17b0f1
$ timedatectl
17b0f1
        Local time: چهارشنبه 2017-10-18 16:29:40 CEST
17b0f1
    Universal time: چهارشنبه 2017-10-18 14:29:40 UTC
17b0f1
          RTC time: چهارشنبه 2017-10-18 14:29:40
17b0f1
17b0f1
17b0f1
(cherry picked from commit 14ce0c25c28ba58e80084e28b4f23884199900e4)
17b0f1
Resolves: #1503942
17b0f1
---
17b0f1
 src/shared/time-util.h     |  2 --
17b0f1
 src/timedate/timedatectl.c | 49 ++++++++++++++++++++------------------
17b0f1
 2 files changed, 26 insertions(+), 25 deletions(-)
17b0f1
17b0f1
diff --git a/src/shared/time-util.h b/src/shared/time-util.h
17b0f1
index f2789142fe..32e90902a8 100644
17b0f1
--- a/src/shared/time-util.h
17b0f1
+++ b/src/shared/time-util.h
17b0f1
@@ -108,5 +108,3 @@ int get_timezones(char ***l);
17b0f1
 bool timezone_is_valid(const char *name);
17b0f1
 
17b0f1
 clockid_t clock_boottime_or_monotonic(void);
17b0f1
-
17b0f1
-#define xstrftime(buf, fmt, tm) assert_se(strftime(buf, ELEMENTSOF(buf), fmt, tm) > 0)
17b0f1
diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c
17b0f1
index 1accccb688..3e9b657bc4 100644
17b0f1
--- a/src/timedate/timedatectl.c
17b0f1
+++ b/src/timedate/timedatectl.c
17b0f1
@@ -93,8 +93,8 @@ static const char *jump_str(int delta_minutes, char *s, size_t size) {
17b0f1
 }
17b0f1
 
17b0f1
 static void print_status_info(const StatusInfo *i) {
17b0f1
-        char a[FORMAT_TIMESTAMP_MAX];
17b0f1
-        char b[FORMAT_TIMESTAMP_MAX];
17b0f1
+        char a[LINE_MAX];
17b0f1
+        char b[LINE_MAX];
17b0f1
         char s[32];
17b0f1
         struct tm tm;
17b0f1
         time_t sec;
17b0f1
@@ -104,6 +104,7 @@ static void print_status_info(const StatusInfo *i) {
17b0f1
         int dn = 0;
17b0f1
         bool is_dstc = false, is_dstn = false;
17b0f1
         int r;
17b0f1
+        size_t n;
17b0f1
 
17b0f1
         assert(i);
17b0f1
 
17b0f1
@@ -123,11 +124,11 @@ static void print_status_info(const StatusInfo *i) {
17b0f1
                 fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
17b0f1
 
17b0f1
         if (have_time) {
17b0f1
-                xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
17b0f1
-                printf("      Local time: %.*s\n", (int) sizeof(a), a);
17b0f1
+                n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
17b0f1
+                printf("      Local time: %s\n", n > 0 ? a : "n/a");
17b0f1
 
17b0f1
-                xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
17b0f1
-                printf("  Universal time: %.*s\n", (int) sizeof(a), a);
17b0f1
+                n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
17b0f1
+                printf("  Universal time: %s\n", n > 0 ? a : "n/a");
17b0f1
         } else {
17b0f1
                 printf("      Local time: %s\n", "n/a");
17b0f1
                 printf("  Universal time: %s\n", "n/a");
17b0f1
@@ -137,24 +138,26 @@ static void print_status_info(const StatusInfo *i) {
17b0f1
                 time_t rtc_sec;
17b0f1
 
17b0f1
                 rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
17b0f1
-                xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
17b0f1
-                printf("        RTC time: %.*s\n", (int) sizeof(a), a);
17b0f1
+                n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
17b0f1
+                printf("        RTC time: %s\n", n > 0 ? a : "n/a");
17b0f1
         } else
17b0f1
                 printf("        RTC time: %s\n", "n/a");
17b0f1
 
17b0f1
         if (have_time)
17b0f1
-                xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
17b0f1
+                n = strftime(a, sizeof a, "%Z, %z", localtime_r(&sec, &tm));
17b0f1
 
17b0f1
-        printf("       Time zone: %s (%.*s)\n"
17b0f1
+        printf("       Time zone: %s (%s)\n"
17b0f1
                "     NTP enabled: %s\n"
17b0f1
                "NTP synchronized: %s\n"
17b0f1
                " RTC in local TZ: %s\n",
17b0f1
-               strna(i->timezone), (int) sizeof(a), have_time ? a : "n/a",
17b0f1
+               strna(i->timezone), have_time && n > 0 ? a : "n/a",
17b0f1
                i->ntp_capable ? yes_no(i->ntp_enabled) : "n/a",
17b0f1
                yes_no(i->ntp_synced),
17b0f1
                yes_no(i->rtc_local));
17b0f1
 
17b0f1
         if (have_time) {
17b0f1
+                size_t m;
17b0f1
+
17b0f1
                 r = time_get_dst(sec, "/etc/localtime",
17b0f1
                                  &tc, &zc, &is_dstc,
17b0f1
                                  &tn, &dn, &zn, &is_dstn);
17b0f1
@@ -164,26 +167,26 @@ static void print_status_info(const StatusInfo *i) {
17b0f1
                         printf("      DST active: %s\n", yes_no(is_dstc));
17b0f1
 
17b0f1
                         t = tc - 1;
17b0f1
-                        xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
17b0f1
+                        n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
17b0f1
 
17b0f1
-                        xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm));
17b0f1
+                        m = strftime(b, sizeof b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tc, &tm));
17b0f1
                         printf(" Last DST change: DST %s at\n"
17b0f1
-                               "                  %.*s\n"
17b0f1
-                               "                  %.*s\n",
17b0f1
+                               "                  %s\n"
17b0f1
+                               "                  %s\n",
17b0f1
                                is_dstc ? "began" : "ended",
17b0f1
-                               (int) sizeof(a), a,
17b0f1
-                               (int) sizeof(b), b);
17b0f1
+                               n > 0 ? a : "n/a",
17b0f1
+                               m > 0 ? b : "n/a");
17b0f1
 
17b0f1
                         t = tn - 1;
17b0f1
-                        xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
17b0f1
-                        xstrftime(b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm));
17b0f1
+                        n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&t, &tm));
17b0f1
+                        m = strftime(b, sizeof b, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&tn, &tm));
17b0f1
                         printf(" Next DST change: DST %s (the clock jumps %s) at\n"
17b0f1
-                               "                  %.*s\n"
17b0f1
-                               "                  %.*s\n",
17b0f1
+                               "                  %s\n"
17b0f1
+                               "                  %s\n",
17b0f1
                                is_dstn ? "begins" : "ends",
17b0f1
                                jump_str(dn, s, sizeof(s)),
17b0f1
-                               (int) sizeof(a), a,
17b0f1
-                               (int) sizeof(b), b);
17b0f1
+                               n > 0 ? a : "n/a",
17b0f1
+                               m > 0 ? b : "n/a");
17b0f1
                 }
17b0f1
         } else
17b0f1
                 printf("      DST active: %s\n", yes_no(is_dstc));