Skip to contentSkip to conversation

Check some returns of common functions

PATCH In review Jeremy Katz 4 participants 5 messages Apr 26, 2006 – Apr 27, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Check some returns of common functions

When building with FORTIFY_SOURCE to ensure that return codes of common functions are checked to avoid some bugs, a few warnings pop up and become errors due to -Werror. Attached checks the return codes

tools/xenstore/xenstored_core.c renamed from tools/xenstore/xenstored_core.c.unused +11 -7
@@ -173,7 +173,7 @@
 	va_list arglist;
 	char *str;
 	char sbuf[1024];
-	int ret;
+	int ret, dummy;
 
 	if (tracefd < 0)
 		return;
@@ -184,7 +184,7 @@
 	va_end(arglist);
 
 	if (ret <= 1024) {
-		write(tracefd, sbuf, ret);
+		dummy = write(tracefd, sbuf, ret);
 		return;
 	}
 
@@ -192,7 +192,7 @@
 	va_start(arglist, fmt);
 	str = talloc_vasprintf(NULL, fmt, arglist);
 	va_end(arglist);
-	write(tracefd, str, strlen(str));
+	dummy = write(tracefd, str, strlen(str));
 	talloc_free(str);
 }
 
@@ -238,7 +238,8 @@
 static void trigger_reopen_log(int signal __attribute__((unused)))
 {
 	char c = 'A';
-	write(reopen_log_pipe[1], &c, 1);
+	int dummy;
+	dummy = write(reopen_log_pipe[1], &c, 1);
 }
 
 
@@ -1678,7 +1679,8 @@
 		exit(0);
 
 	len = sprintf(buf, "%d\n", getpid());
-	write(fd, buf, len);
+	if (write(fd, buf, len) != len)
+		barf_perror("Writing pid file %s", pidfile);
 }
 
 /* Stevens. */
@@ -1703,7 +1705,8 @@
 
 #ifndef TESTING	/* Relative paths for socket names */
 	/* Move off any mount points we might be in. */
-	chdir("/");
+	if (chdir("/") == -1)
+		barf_perror("Failed to chdir");
 #endif
 	/* Discard our parent's old-fashioned umask prejudices. */
 	umask(0);
@@ -1900,7 +1903,8 @@
 
 		if (FD_ISSET(reopen_log_pipe[0], &inset)) {
 			char c;
-			read(reopen_log_pipe[0], &c, 1);
+			if (read(reopen_log_pipe[0], &c, 1) != 1)
+				barf_perror("read failed");
 			reopen_log();
 		}
 

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 18 lines · Jeremy Katz

When building with FORTIFY_SOURCE to ensure that return codes of common functions are checked to avoid some bugs, a few warnings pop up and become errors due to -Werror. Attached checks the return codes

tools/xenstore/xenstored_core.c renamed from tools/xenstore/xenstored_core.c.unused +1 -1
@@ -173,7 +173,7 @@
 	va_list arglist;
 	char *str;
 	char sbuf[1024];
-	int ret;
+	int ret, dummy;
 
 	if (tracefd < 0)
 		return;

tools/xenstore/xenstored_core.c line 188

Quoted 6 lines
tools/xenstore/xenstored_core.c renamed from tools/xenstore/xenstored_core.c.unused +1 -1
@@ -184,7 +184,7 @@
 	va_end(arglist);
 
 	if (ret <= 1024) {
-		write(tracefd, sbuf, ret);
+		dummy = write(tracefd, sbuf, ret);
 		return;

Does (void)write(tracefd, sbuf, ref) not work? This ought to make it clear that the return value is being ignored without additional dummy variables.

Ewan.

#2 lore raw
Quoted 2 lines · Ewan Mellor

Does (void)write(tracefd, sbuf, ref) not work? This ought to make it clear that the return value is being ignored without additional dummy variables.

No. That would be too obvious and useful!

The following might be clearer than doing the workaround in-place though:

#define ignore_retval(x) do { long __r; __r = (long)(x); } while (0)

(Most useful scalar types can be cast to/from long.)

-- Keir

#3 lore raw

Keir Fraser wrote: [Wed Apr 26 2006, 04:52:16AM EDT]

Quoted 4 lines

The following might be clearer than doing the workaround in-place though:

#define ignore_retval(x) do { long __r; __r = (long)(x); } while (0)

How is a custom wrapper clearer than (void), which is generally recognized as intentionally ignoring the return value?

Aron

#4 lore raw
Quoted 7 lines · Aron Griffis
Quoted 4 lines

The following might be clearer than doing the workaround in-place though:

#define ignore_retval(x) do { long __r; __r = (long)(x); } while (0)

How is a custom wrapper clearer than (void), which is generally recognized as intentionally ignoring the return value?

Good point, except that casting the result to void does not avoid the warning/error in this particular case so we have to pick a less attractive workaround.

-- Keir