From cbfb64bf03960ff3157e527c981f619c329c1fc1 Mon Sep 17 00:00:00 2001
From: Carl Drougge <bearded@longhaired.org>
Date: Fri, 25 Feb 2022 22:35:24 +0100
Subject: warnings: Use some error returns

to give up and report the error.
---
 src/display.c | 11 +++++++----
 src/fileio.c  | 12 ++++++++----
 src/loadav.c  |  5 ++---
 src/screen.c  | 31 +++++++++++++++++++------------
 src/socket.c  |  5 +++--
 src/window.c  |  9 ++++++---
 6 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/src/display.c b/src/display.c
index d0fe93e..b475a80 100644
--- a/src/display.c
+++ b/src/display.c
@@ -4017,7 +4017,8 @@ char **cmdv;
       if (slave != -1)
 	{
 	  close(0);
-	  dup(slave);
+	  if (dup(slave))
+	    Panic(errno, "RunBlanker: dup slave fd to 0");
 	  close(slave);
 	  closeallfiles(D_blankerev.fd);
 	  slave = dup(0);
@@ -4041,9 +4042,11 @@ char **cmdv;
       close(1);
       close(2);
       if (open(m, O_RDWR))
-	Panic(errno, "Cannot open %s", m);
-      dup(0);
-      dup(0);
+	Panic(errno, "RunBlanker: open %s to 0", m);
+      if (dup(0) != 1)
+	Panic(errno, "RunBlanker: dup %s to 1", m);
+      if (dup(0) != 2)
+	Panic(errno, "RunBlanker: dup %s to 2", m);
       close(D_blankerev.fd);
       if (slave != -1)
 	close(slave);
diff --git a/src/fileio.c b/src/fileio.c
index d488166..90432b1 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -427,9 +427,12 @@ WriteFile(struct acluser *user, char *fn, int dump)
 # ifdef HAVE_LSTAT
 			if (exists) {
 				if ((fd = open(fn, O_WRONLY, 0666)) >= 0) {
-					if (fstat(fd, &stb2) == 0 && stb.st_dev == stb2.st_dev && stb.st_ino == stb2.st_ino)
-						ftruncate(fd, 0);
-					else {
+					if (
+						fstat(fd, &stb2) ||
+						stb.st_dev != stb2.st_dev ||
+						stb.st_ino != stb2.st_ino ||
+						ftruncate(fd, 0)
+					) {
 						close(fd);
 						fd = -1;
 					}
@@ -721,7 +724,8 @@ printpipe(struct win *p, char *cmd)
 			fclose(dfp);
 #endif
 		close(0);
-		dup(pi[0]);
+		if (dup(pi[0]))
+			Panic(errno, "printpipe: dup to 0");
 		closeallfiles(0);
 		if (setgid(real_gid) || setuid(real_uid))
 			Panic(errno, "printpipe setuid");
diff --git a/src/loadav.c b/src/loadav.c
index 953b34e..4be42bc 100644
--- a/src/loadav.c
+++ b/src/loadav.c
@@ -85,13 +85,12 @@ GetLoadav()
 
   if ((fp = secfopen("/proc/loadavg", "r")) == NULL)
     return 0;
-  *buf = 0;
-  fgets(buf, sizeof(buf), fp);
+  s = fgets(buf, sizeof(buf), fp);
   fclose(fp);
+  if (!s) return 0;
   /* can't use fscanf because the decimal point symbol depends on
    * the locale but the kernel uses always '.'.
    */
-  s = buf;
   for (i = 0; i < (LOADAV_NUM > 3 ? 3 : LOADAV_NUM); i++)
     {
       d = e = 0;
diff --git a/src/screen.c b/src/screen.c
index 1b8ce2f..fa191dd 100644
--- a/src/screen.c
+++ b/src/screen.c
@@ -984,8 +984,8 @@ int main(int ac, char** av)
 
 #define SET_GUID() do \
   { \
-    setgid(real_gid); \
-    setuid(real_uid); \
+    if (setgid(real_gid)) Panic(errno, "setgid"); \
+    if (setuid(real_uid)) Panic(errno, "setuid"); \
     eff_uid = real_uid; \
     eff_gid = real_gid; \
   } while (0)
@@ -1323,13 +1323,16 @@ int main(int ac, char** av)
   }
   else
     n = -1;
-  freopen("/dev/null", "r", stdin);
-  freopen("/dev/null", "w", stdout);
+  if (!freopen("/dev/null", "r", stdin))
+    Panic(errno, "stdin = /dev/null");
+  if (!freopen("/dev/null", "w", stdout))
+    Panic(errno, "stdout = /dev/null");
 
 #ifdef DEBUG
   if (dfp != stderr)
 #endif
-  freopen("/dev/null", "w", stderr);
+  if (!freopen("/dev/null", "w", stderr))
+    Panic(errno, "stderr = /dev/null");
   debug("-- screen.back debug started\n");
 
   /*  This guarantees that the session owner is listed, even when we
@@ -1668,8 +1671,10 @@ static sigret_t CoreDump SIGDEFARG
 #if defined(SYSVSIGS) && defined(SIGHASARG)
   signal(sigsig, SIG_IGN);
 #endif
-  setgid(getgid());
-  setuid(getuid());
+  if (setgid(getgid()))
+    abort();
+  if (setuid(getuid()))
+    abort();
   unlink("core");
 
 #ifdef SIGHASARG
@@ -1862,9 +1867,9 @@ void eexit(int e)
   debug("eexit\n");
   if (ServerSocket != -1) {
     debug1("we unlink(%s)\n", SockPath);
-    setgid(real_gid);
-    setuid(real_uid);
-    (void) unlink(SockPath);
+    /* Probably safest to not do it if setuid fails. */
+    if (!setgid(real_gid) && !setuid(real_uid))
+      (void) unlink(SockPath);
   }
   exit(e);
 }
@@ -2178,16 +2183,18 @@ DEFINE_VARARGS_FN(Panic)
     }
 #ifdef MULTIUSER
   if (tty_oldmode >= 0) {
+    int setuid_failed = 0;
 
 # ifdef USE_SETEUID
     if (setuid(own_uid))
       xseteuid(own_uid);	/* may be a loop. sigh. */
 # else
-      setuid(own_uid);
+      setuid_failed = setuid(own_uid);
 # endif
 
     debug1("Panic: changing back modes from %s\n", attach_tty);
-    chmod(attach_tty, tty_oldmode);
+    if (!setuid_failed)
+      chmod(attach_tty, tty_oldmode);
   }
 #endif
   eexit(1);
diff --git a/src/socket.c b/src/socket.c
index bc6d7bb..6336770 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -741,6 +741,7 @@ char *tty, *buf;
   int s;
   struct msg m;
   bool is_socket;
+  int wrote;
 
   debug2("SendErrorMsg: %s %s\n", tty, buf);
   strncpy(m.m.message, buf, sizeof(m.m.message) - 1);
@@ -754,9 +755,9 @@ char *tty, *buf;
   m.m_tty[sizeof(m.m_tty) - 1] = 0;
   m.protocol_revision = MSG_REVISION;
   debug1("SendErrorMsg(): writing to '%s'\n", SockPath);
-  (void) write(s, (char *) &m, sizeof m);
+  wrote = write(s, (char *) &m, sizeof m);
   close(s);
-  return 0;
+  return wrote != sizeof m;
 }
 
 static void
diff --git a/src/window.c b/src/window.c
index dbd6e60..e890725 100644
--- a/src/window.c
+++ b/src/window.c
@@ -1296,7 +1296,8 @@ char **args, *ttyn;
       if (slave != -1)
 	{
 	  close(0);
-	  dup(slave);
+	  if (dup(slave))
+	    Panic(errno, "ForkWindow: dup slave to 0");
 	  close(slave);
 	  closeallfiles(win->w_ptyfd);
 	  slave = dup(0);
@@ -1347,11 +1348,13 @@ char **args, *ttyn;
 		    Panic(errno, "Cannot open %s", ttyn);
 		}
 	      else
-		dup(newfd);
+		if (dup(newfd) != i)
+		  Panic(errno, "ForkWindow: dup newfd to %d", i);
 	    }
 	  else
 	    {
-	      dup(win->w_ptyfd);
+	      if (dup(win->w_ptyfd) != i)
+	        Panic(errno, "ForkWindow: dup w_ptyfd to %d", i);
 	      wfdused = 1;
 	    }
 	}
-- 
2.30.2

