Blame SOURCES/0055-vsf_sysutil_get_tz-Check-the-return-value-of-syscall.patch

d7fdbd
From c7ac05fdf2a7b53d901bfc3afeb9a61916aaaaf1 Mon Sep 17 00:00:00 2001
d7fdbd
From: =?UTF-8?q?Ond=C5=99ej=20Lyson=C4=9Bk?= <olysonek@redhat.com>
d7fdbd
Date: Wed, 9 May 2018 20:26:37 +0200
d7fdbd
Subject: [PATCH 55/59] vsf_sysutil_get_tz: Check the return value of syscalls
d7fdbd
d7fdbd
Check the return value of syscalls. There's always the possibility that
d7fdbd
they'll fail. (Failure of close() is not handled though, apart from EINTR.
d7fdbd
The file is open read-only so it shouldn't fail, and even if it does,
d7fdbd
it's not tragic.)
d7fdbd
d7fdbd
We return NULL in case of syscall failure. One might be tempted to simply
d7fdbd
call die() when any kind of error occurs when parsing the timezone data,
d7fdbd
but I think it's more in line with the behaviour of tzset(3) not to do
d7fdbd
anything drastic in such a case (tzset() will silently use UTC when
d7fdbd
the value given in the TZ environment variable is invalid).
d7fdbd
---
d7fdbd
 sysutil.c | 46 +++++++++++++++++++++++++++++++++++++---------
d7fdbd
 1 file changed, 37 insertions(+), 9 deletions(-)
d7fdbd
d7fdbd
diff --git a/sysutil.c b/sysutil.c
d7fdbd
index de5f876..fd07d99 100644
d7fdbd
--- a/sysutil.c
d7fdbd
+++ b/sysutil.c
d7fdbd
@@ -2647,12 +2647,12 @@ error:
d7fdbd
   die("reopening standard file descriptors to /dev/null failed");
d7fdbd
 }
d7fdbd
 
d7fdbd
-char* vsf_sysutil_get_tz()
d7fdbd
+char* vsf_sysutil_get_tz(void)
d7fdbd
 {
d7fdbd
   char *ret_tz = NULL;
d7fdbd
   char buff[BUFTZSIZ];
d7fdbd
   off_t s_pos, e_pos;
d7fdbd
-  size_t rcnt, rest;
d7fdbd
+  ssize_t rcnt, rest;
d7fdbd
   int fd;
d7fdbd
 
d7fdbd
   if ((fd = open(F_LOCALTIME, O_RDONLY)) > -1)
d7fdbd
@@ -2663,8 +2663,12 @@ char* vsf_sysutil_get_tz()
d7fdbd
       return NULL;
d7fdbd
     }
d7fdbd
     s_pos = e_pos > BUFTZSIZ ? e_pos - BUFTZSIZ : 0;
d7fdbd
-    lseek(fd, s_pos, SEEK_SET);
d7fdbd
-    rcnt = read(fd, buff, BUFTZSIZ);
d7fdbd
+    if (lseek(fd, s_pos, SEEK_SET) == -1 ||
d7fdbd
+        (rcnt = vsf_sysutil_read(fd, buff, BUFTZSIZ)) == -1)
d7fdbd
+    {
d7fdbd
+      close(fd);
d7fdbd
+      return NULL;
d7fdbd
+    }
d7fdbd
 
d7fdbd
     if (rcnt && buff[rcnt-1] == '\n')
d7fdbd
     {
d7fdbd
@@ -2680,10 +2684,25 @@ char* vsf_sysutil_get_tz()
d7fdbd
          int len = e_pos - s_pos - offset;
d7fdbd
          if (len)
d7fdbd
          {
d7fdbd
-           lseek(fd, s_pos + offset, SEEK_SET);
d7fdbd
+           if (lseek(fd, s_pos + offset, SEEK_SET) == -1)
d7fdbd
+           {
d7fdbd
+             close(fd);
d7fdbd
+             return NULL;
d7fdbd
+           }
d7fdbd
            ret_tz = calloc(1, len+4);
d7fdbd
+           if (ret_tz == NULL)
d7fdbd
+           {
d7fdbd
+             close(fd);
d7fdbd
+             return NULL;
d7fdbd
+           }
d7fdbd
            memcpy(ret_tz, "TZ=", 3);
d7fdbd
-           rcnt = read(fd, ret_tz+3, len);
d7fdbd
+           rcnt = vsf_sysutil_read(fd, ret_tz+3, len);
d7fdbd
+           if (rcnt == -1)
d7fdbd
+           {
d7fdbd
+             free(ret_tz);
d7fdbd
+             close(fd);
d7fdbd
+             return NULL;
d7fdbd
+           }
d7fdbd
          }
d7fdbd
          break;
d7fdbd
        }
d7fdbd
@@ -2693,11 +2712,20 @@ char* vsf_sysutil_get_tz()
d7fdbd
        }
d7fdbd
        rest = s_pos > BUFTZSIZ ? s_pos - BUFTZSIZ : 0;
d7fdbd
        s_pos -= rest;
d7fdbd
-       lseek(fd, s_pos, SEEK_SET);
d7fdbd
-       rcnt = read(fd, buff, rest);
d7fdbd
+       if (lseek(fd, s_pos, SEEK_SET) == -1)
d7fdbd
+       {
d7fdbd
+         close(fd);
d7fdbd
+         return NULL;
d7fdbd
+       }
d7fdbd
+       rcnt = vsf_sysutil_read(fd, buff, rest);
d7fdbd
+       if (rcnt == -1)
d7fdbd
+       {
d7fdbd
+         close(fd);
d7fdbd
+         return NULL;
d7fdbd
+       }
d7fdbd
     } while (rcnt > 0);
d7fdbd
 
d7fdbd
-    close (fd);
d7fdbd
+    (void) vsf_sysutil_close_errno(fd);
d7fdbd
   }
d7fdbd
 
d7fdbd
   return ret_tz;
d7fdbd
-- 
d7fdbd
2.14.4
d7fdbd