[PATCH:xdm 1/2] Add a cast to avoid a compiler error.

Thomas Klausner wiz at netbsd.org
Sat Jun 29 06:50:56 PDT 2013


On Fri, Jun 28, 2013 at 09:00:51PM +0200, Geert Uytterhoeven wrote:
> On Fri, Jun 28, 2013 at 7:35 PM, Thomas Klausner <wiz at netbsd.org> wrote:
> > -       if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
> > +       if (atomicio((ssize_t (*)(int, void *, size_t))write, fd, msg, sizeof(msg)) != sizeof(msg)) {
> 
> This cast looks really gross to me...
> 
> What about wrapping write() in a function that takes care of casting the second
> parameter of the function?

Sure, that's also possible. New version of the patch attached.
 Thomas

-------------- next part --------------
>From f64e6445f2aed6e7c7ebb1ba26914009441cf5f6 Mon Sep 17 00:00:00 2001
From: Thomas Klausner <wiz at NetBSD.org>
Date: Sun, 2 Jun 2013 22:20:41 +0200
Subject: [PATCH:xdm 2/2] Add wrapper function for write.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

gcc-4.5.4 on NetBSD complains:
xdm/prngc.c: In function ?get_prngd_bytes?:
xdm/prngc.c:133:2: error: passing argument 1 of ?atomicio? from incompatible pointer type
xdm/prngc.c:46:16: note: expected ?ssize_t (*)(int,  void *, size_t)? but argument is of type ?ssize_t (*)(int,  const void *, size_t)?

The problem is that the read(2) and write(2) syscalls differ
in the const-ness of their second argument. The wrapper
function gets rid of the difference.
---
 xdm/prngc.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/xdm/prngc.c b/xdm/prngc.c
index f0db8eb..535a59e 100644
--- a/xdm/prngc.c
+++ b/xdm/prngc.c
@@ -44,6 +44,7 @@
 #endif
 
 static ssize_t atomicio(ssize_t (*)(int, void *, size_t), int, void *, size_t);
+static ssize_t voidwrite(int, void *, size_t);
 
 #ifndef offsetof
 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
@@ -130,7 +131,7 @@ reopen:
 	msg[0] = 0x02;
 	msg[1] = len;
 
-	if (atomicio(write, fd, msg, sizeof(msg)) != sizeof(msg)) {
+	if (atomicio(voidwrite, fd, msg, sizeof(msg)) != sizeof(msg)) {
 		if (errno == EPIPE && errors < 10) {
 			close(fd);
 			errors++;
@@ -188,3 +189,9 @@ atomicio(ssize_t (*f)(int, void *, size_t), int fd, void *_s, size_t n)
 	}
 	return (pos);
 }
+
+static ssize_t
+voidwrite(int d, void *buf, size_t nbytes)
+{
+	return write(d, (const char *)buf, nbytes);
+}
-- 
1.8.3.1



More information about the xorg-devel mailing list