[PATCH] Do not ignore the return value

Tomas Carnecky tom at dbservice.com
Wed Feb 4 11:14:14 PST 2009


hw/xfree86/common/xf86Helper.c:
Check the return value from setpriority() and nice() and print
a warning if either fails.

hw/xfree86/os-support/linux/lnx_init.c:
Check the return value from chown() and print a warning if it
fails.

os/utils.c:
Check the return value from write() when writing the PID to the
lock file and make it a fatal error if that fails.

xkb/xkmread.c:
Check the return value from fread() and make it a XkbLibError
if that fails (_XkbErrIllegalContents).

os/log.c:
Now these are a bit tricky. I choose to simply ignore the return
values. I don't see any other solution. We can't print a warning
when fwrite() fails, because that would cause another call to
LogVWrite() and we'd likely end up in an endless loop.

Signed-off-by: Tomas Carnecky <tom at dbservice.com>
---

gcc warns about the unused result (because the functions in question
have been annotated with __attribute__ ((warn_unused_result)))

Note that simply prepending (void) doesn't convince gcc, I had
to explicitely create a local variable (for the change in os/log.c)


 hw/xfree86/common/xf86Helper.c         |   16 ++++++++++++----
 hw/xfree86/os-support/linux/lnx_init.c |    9 +++++++--
 os/log.c                               |    9 +++++----
 os/utils.c                             |    3 ++-
 xkb/xkmread.c                          |   11 +++++++++--
 5 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/hw/xfree86/common/xf86Helper.c b/hw/xfree86/common/xf86Helper.c
index 9aeba27..732a1fd 100644
--- a/hw/xfree86/common/xf86Helper.c
+++ b/hw/xfree86/common/xf86Helper.c
@@ -2061,18 +2061,26 @@ xf86SetPriority(Bool up)
     if (up) {
 #ifdef HAS_SETPRIORITY
 	saved_nice = getpriority(PRIO_PROCESS, 0);
-	setpriority(PRIO_PROCESS, 0, -20);
+	if (setpriority(PRIO_PROCESS, 0, -20) < 0)
+	    xf86Msg(X_WARNING,"xf86SetPriority: setpriority() failed: %s\n",
+		    strerror(errno));
 #endif
 #if defined(SYSV) || defined(SVR4) || defined(linux)
 	saved_nice = nice(0);
-	nice(-20 - saved_nice);
+	if (nice(-20 - saved_nice) < 0)
+	    xf86Msg(X_WARNING,"xf86SetPriority: nice() failed: %s\n",
+		    strerror(errno));
 #endif
     } else {
 #ifdef HAS_SETPRIORITY
-	setpriority(PRIO_PROCESS, 0, saved_nice);
+	if (setpriority(PRIO_PROCESS, 0, saved_nice) < 0)
+	    xf86Msg(X_WARNING,"xf86SetPriority: setpriority() failed: %s\n",
+		    strerror(errno));
 #endif
 #if defined(SYSV) || defined(SVR4) || defined(linux)
-	nice(20 + saved_nice);
+	if (nice(20 + saved_nice) < 0)
+	    xf86Msg(X_WARNING,"xf86SetPriority: nice() failed: %s\n",
+		    strerror(errno));
 #endif
     }
 }
diff --git a/hw/xfree86/os-support/linux/lnx_init.c b/hw/xfree86/os-support/linux/lnx_init.c
index 17502a1..86939be 100644
--- a/hw/xfree86/os-support/linux/lnx_init.c
+++ b/hw/xfree86/os-support/linux/lnx_init.c
@@ -81,8 +81,13 @@ static void
 restoreVtPerms(void)
 {
     /* Set the terminal permissions back to before we started. */
-    chown("/dev/tty0", vtPermSave[0], vtPermSave[1]);
-    chown(vtname, vtPermSave[2], vtPermSave[3]);
+    if (chown("/dev/tty0", vtPermSave[0], vtPermSave[1]) < 0)
+	xf86Msg(X_WARNING,"restoreVtPerms: chown %s failed: %s\n",
+		"/dev/tty0", strerror(errno));
+
+    if (chown(vtname, vtPermSave[2], vtPermSave[3]) < 0)
+	xf86Msg(X_WARNING,"restoreVtPerms: chown %s failed: %s\n",
+		vtname, strerror(errno));
 }
 
 static void *console_handler;
diff --git a/os/log.c b/os/log.c
index b01965a..9af2afb 100644
--- a/os/log.c
+++ b/os/log.c
@@ -159,6 +159,7 @@ const char *
 LogInit(const char *fname, const char *backup)
 {
     char *logFileName = NULL;
+    int err;
 
     if (fname && *fname) {
 	/* xalloc() can't be used yet. */
@@ -195,7 +196,7 @@ LogInit(const char *fname, const char *backup)
 
 	/* Flush saved log information. */
 	if (saveBuffer && bufferSize > 0) {
-	    fwrite(saveBuffer, bufferPos, 1, logFile);
+	    err = fwrite(saveBuffer, bufferPos, 1, logFile);
 	    fflush(logFile);
 #ifndef WIN32
 	    fsync(fileno(logFile));
@@ -253,7 +254,7 @@ void
 LogVWrite(int verb, const char *f, va_list args)
 {
     static char tmpBuffer[1024];
-    int len = 0;
+    int err, len = 0;
 
     /*
      * Since a va_list can only be processed once, write the string to a
@@ -265,10 +266,10 @@ LogVWrite(int verb, const char *f, va_list args)
 	len = strlen(tmpBuffer);
     }
     if ((verb < 0 || logVerbosity >= verb) && len > 0)
-	fwrite(tmpBuffer, len, 1, stderr);
+	err = fwrite(tmpBuffer, len, 1, stderr);
     if ((verb < 0 || logFileVerbosity >= verb) && len > 0) {
 	if (logFile) {
-	    fwrite(tmpBuffer, len, 1, logFile);
+	    err = fwrite(tmpBuffer, len, 1, logFile);
 	    if (logFlush) {
 		fflush(logFile);
 #ifndef WIN32
diff --git a/os/utils.c b/os/utils.c
index 56095b1..80b9921 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -314,7 +314,8 @@ LockServer(void)
   if (lfd < 0)
     FatalError("Could not create lock file in %s\n", tmp);
   (void) sprintf(pid_str, "%10ld\n", (long)getpid());
-  (void) write(lfd, pid_str, 11);
+  if (write(lfd, pid_str, 11) < 11)
+    FatalError("Could not write PID to the lock file\n");
   (void) chmod(tmp, 0444);
   (void) close(lfd);
 
diff --git a/xkb/xkmread.c b/xkb/xkmread.c
index dc8ab61..e08d1df 100644
--- a/xkb/xkmread.c
+++ b/xkb/xkmread.c
@@ -1140,7 +1140,11 @@ unsigned i,size_toc;
 	}
 	return 0;
     }
-    fread(file_info,SIZEOF(xkmFileInfo),1,file);
+    if (fread(file_info,SIZEOF(xkmFileInfo),1,file) < 1) {
+	    _XkbLibError(_XkbErrIllegalContents,"XkmReadTOC",0);
+	    return 0;
+    }
+
     size_toc= file_info->num_toc;
     if (size_toc>max_toc) {
 	DebugF("Warning! Too many TOC entries; last %d ignored\n",
@@ -1148,7 +1152,10 @@ unsigned i,size_toc;
 	size_toc= max_toc;
     }
     for (i=0;i<size_toc;i++) {
-	fread(&toc[i],SIZEOF(xkmSectionInfo),1,file);
+	if (fread(&toc[i],SIZEOF(xkmSectionInfo),1,file) < 1) {
+	    _XkbLibError(_XkbErrIllegalContents,"XkmReadTOC",0);
+	    return 0;
+	}
     }
     return 1;
 }
-- 
1.6.1.2





More information about the xorg mailing list