Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] Silence compilation warnings about setuid, setgid



Hello,
on a Linux box I see:

options.c:772:2: warning: ignoring return value of ‘setuid’, declared with attribute warn_unused_res
ult [-Wunused-result]
  setuid(getuid());
  ^~~~~~~~~~~~~~~~
options.c:773:2: warning: ignoring return value of ‘setgid’, declared with attribute warn_unused_res
ult [-Wunused-result]
  setgid(getgid());
  ^~~~~~~~~~~~~~~~

Looking at the source, the reported calls are "extra" ones, they are followed by proper setuid, setgid calls. I've found some way out from this situation, of using the report value and reporting it (gmail paste, proper patch is attached):

diff --git a/Src/options.c b/Src/options.c
index 590652ea9..e085af796 100644
--- a/Src/options.c
+++ b/Src/options.c
@@ -769,13 +769,23 @@ dosetopt(int optno, int value, int force, char *new_opts)
     } else if(optno == PRIVILEGED && !value) {
        /* unsetting PRIVILEGED causes the shell to make itself unprivileged */
 #ifdef HAVE_SETUID
-       setuid(getuid());
-       setgid(getgid());
+       int uerr = 0, gerr = 0;
+
+       if(setuid(getuid())) {
+           uerr = errno;
+       }
+       if(setgid(getgid())) {
+           gerr = errno;
+       }
         if (setuid(getuid())) {
             zwarn("failed to change user ID: %e", errno);
+            if (uerr)
+                zwarn("(error of additional preceding setuid() call: %e)", uerr);
             return -1;
        } else if (setgid(getgid())) {
             zwarn("failed to change group ID: %e", errno);
+            if (gerr)
+                zwarn("(error of additional preceding setgid() call: %e)", gerr);
             return -1;
         }
 #else

diff --git a/Src/options.c b/Src/options.c
index 590652ea9..e085af796 100644
--- a/Src/options.c
+++ b/Src/options.c
@@ -769,13 +769,23 @@ dosetopt(int optno, int value, int force, char *new_opts)
     } else if(optno == PRIVILEGED && !value) {
 	/* unsetting PRIVILEGED causes the shell to make itself unprivileged */
 #ifdef HAVE_SETUID
-	setuid(getuid());
-	setgid(getgid());
+	int uerr = 0, gerr = 0;
+
+	if(setuid(getuid())) {
+	    uerr = errno;
+	}
+	if(setgid(getgid())) {
+	    gerr = errno;
+	}
         if (setuid(getuid())) {
             zwarn("failed to change user ID: %e", errno);
+            if (uerr)
+                zwarn("(error of additional preceding setuid() call: %e)", uerr);
             return -1;
 	} else if (setgid(getgid())) {
             zwarn("failed to change group ID: %e", errno);
+            if (gerr)
+                zwarn("(error of additional preceding setgid() call: %e)", gerr);
             return -1;
         }
 #else


Messages sorted by: Reverse Date, Date, Thread, Author