[Nut-upsdev] gcc4 compiler warnings

Peter Selinger selinger at mathstat.dal.ca
Fri Feb 10 15:29:15 UTC 2006


Gentlemen,

attached my proposed changes. I figured, since we discussed them for
so long, it might be a good idea to let you see them before I commit.
I changed many of the functions in serial.h to use unsigned char (but
not all of them; I conservatively stayed away from ser_get_line_alert
and friends, because they actually compare characters that are passed
by value).

I resisted the temptation to muck with the drivers themselves (except
for undoing the changes to the belkinunv and bcmxcp_ser drivers from
earlier today). This means, in the short run, there will be *more*
compiler warnings, but that is a good thing, as it will hopefully lead
to some of the drivers rethinking their char/uchar choices, and others
to just put in the typecasts.

If no complaints about the attached patch, I will commit it later. 
-- Peter

Arjen de Korte wrote:
> 
> 
> >> I believe the type (void *) should only be used for data whose value
> >> is intended to be typecast (such as malloc()), or where the nature of
> >> the data truly does not matter (such as memset()).
> > I agree completely on this. Using void * just to get rid of
> > compiler warnings is the wrong way to go, it'll bite you in the long
> > run.
> 
> *SLAP!*
> 
> I stand corrected, that was definitly not one of my finest ideas today.
> I'll convert my driver later today, to use unsigned char's.
> 
> Regards, Arjen
> 
> 

-------------- next part --------------
Index: drivers/bcmxcp_ser.c
===================================================================
RCS file: /cvsroot/nut/nut/drivers/Attic/bcmxcp_ser.c,v
retrieving revision 1.1.2.3
diff -u -r1.1.2.3 bcmxcp_ser.c
--- drivers/bcmxcp_ser.c	9 Feb 2006 19:27:05 -0000	1.1.2.3
+++ drivers/bcmxcp_ser.c	10 Feb 2006 15:02:51 -0000
@@ -31,7 +31,7 @@
 		buf[3]=calc_checksum(buf);	/* checksum */
 		
 		if (retry == 4) ser_send_char(upsfd, 0x1d);	/* last retry is preceded by a ESC.*/
-			sent = ser_send_buf(upsfd, (char*)buf, 4);
+			sent = ser_send_buf(upsfd, buf, 4);
 		retry += 1;
 	}
 }
@@ -57,7 +57,7 @@
 	sent = 0;
 
 	while ((sent != (command_length)) && (retry < PW_MAX_TRY)) {
-		sent = ser_send_buf(upsfd, (char*)sbuf, (command_length));
+		sent = ser_send_buf(upsfd, sbuf, (command_length));
 		if (sent != (command_length)) printf("Error sending command %x\n", (unsigned char)sbuf[2]);
 			retry += 1;
 	}
@@ -79,7 +79,7 @@
 
 		do {
 			/* Read PW_COMMAND_START_BYTE byte */
-			res = ser_get_char(upsfd, (char*)my_buf, 1, 0);
+			res = ser_get_char(upsfd, my_buf, 1, 0);
 			if (res != 1) {
 				upsdebugx(1,"Receive error (PW_COMMAND_START_BYTE): %d!!!\n", res);
 				return -1;
@@ -94,7 +94,7 @@
 		}
 
 		/* Read block number byte */
-		res = ser_get_char(upsfd, (char*)(my_buf+1), 1, 0);
+		res = ser_get_char(upsfd, my_buf+1, 1, 0);
 		if (res != 1) {
 			ser_comm_fail("Receive error (Block number): %d!!!\n", res);
 			return -1;
@@ -120,7 +120,7 @@
 		}
 
 		/* Read data length byte */
-		res = ser_get_char(upsfd, (char*)(my_buf+2), 1, 0);
+		res = ser_get_char(upsfd, my_buf+2, 1, 0);
 		if (res != 1) {
 			ser_comm_fail("Receive error (length): %d!!!\n", res);
 			return -1;
@@ -133,7 +133,7 @@
 		}
 
 		/* Read sequence byte */
-		res = ser_get_char(upsfd, (char*)(my_buf+3), 1, 0);
+		res = ser_get_char(upsfd, my_buf+3, 1, 0);
 		if (res != 1) {
 			ser_comm_fail("Receive error (sequence): %d!!!\n", res);
 			return -1;
@@ -152,14 +152,14 @@
 		pre_sequence = sequence;
 
 		/* Try to read all the remainig bytes */
-		res = ser_get_buf_len(upsfd, (char*)(my_buf+4), length, 1, 0);
+		res = ser_get_buf_len(upsfd, my_buf+4, length, 1, 0);
 		if (res != length) {
 			ser_comm_fail("Receive error (data): got %d bytes instead of %d!!!\n", res, length);
 			return -1;
 		}
 
 		/* Get the checksum byte */
-		res = ser_get_char(upsfd, (char*)(my_buf+(4+length)), 1, 0);
+		res = ser_get_char(upsfd, my_buf+(4+length), 1, 0);
 
 		if (res != 1) {
 			ser_comm_fail("Receive error (checksum): %x!!!\n", res);
Index: drivers/belkinunv.c
===================================================================
RCS file: /cvsroot/nut/nut/drivers/belkinunv.c,v
retrieving revision 1.1.1.1.10.4
diff -u -r1.1.1.1.10.4 belkinunv.c
--- drivers/belkinunv.c	9 Feb 2006 16:01:09 -0000	1.1.1.1.10.4
+++ drivers/belkinunv.c	10 Feb 2006 15:02:52 -0000
@@ -248,7 +248,7 @@
 	if (n+1 > bufsize) {
 		return -1;
 	}
-	r = ser_get_buf_len(upsfd, (char *)&buf[0], 1, 3, 0);
+	r = ser_get_buf_len(upsfd, &buf[0], 1, 3, 0);
 	if (r==-1) {
 		upslogx(LOG_ERR, "No response from UPS");
 		return -1;
@@ -262,7 +262,7 @@
 	if (n+3 > bufsize) {
 		return -1;
 	}
-	r = ser_get_buf_len(upsfd, (char *)&buf[1], 3, 3, 0);
+	r = ser_get_buf_len(upsfd, &buf[1], 3, 3, 0);
 	if (r!=3) {
 		upslogx(LOG_ERR, "Short read from UPS");
 		return -1;
@@ -275,7 +275,7 @@
 	if (n+len > bufsize) {
 		return -1;
 	}
-	r = ser_get_buf_len(upsfd, (char *)&buf[4], len, 3, 0);
+	r = ser_get_buf_len(upsfd, &buf[4], len, 3, 0);
 	if (r!=len) {
 		upslogx(LOG_ERR, "Short read from UPS");
 		return -1;
@@ -305,7 +305,7 @@
 	buf[4] = 0;
 	buf[5] = belkin_checksum(buf, 5);
 
-	r = ser_send_buf(upsfd, (char *)buf, 6);
+	r = ser_send_buf(upsfd, buf, 6);
 	if (r<0) {
 		upslogx(LOG_ERR, "Failed write to UPS");
 		return NULL;
@@ -346,7 +346,7 @@
 	buf[4] = 0;
 	buf[5] = belkin_checksum(buf, 5);
 
-	r = ser_send_buf(upsfd, (char *)buf, 6);
+	r = ser_send_buf(upsfd, buf, 6);
 	if (r<0) {
 		upslogx(LOG_ERR, "Failed write to UPS");
 		return -1;
@@ -392,7 +392,7 @@
 	buf[5] = (val>>8) & 0xff;
 	buf[6] = belkin_checksum(buf, 6);
   
-	r = ser_send_buf(upsfd, (char *)buf, 7);
+	r = ser_send_buf(upsfd, buf, 7);
 	if (r<0) {
 		upslogx(LOG_ERR, "Failed write to UPS");
 		return -1;
Index: drivers/serial.c
===================================================================
RCS file: /cvsroot/nut/nut/drivers/serial.c,v
retrieving revision 1.1.1.1.10.1
diff -u -r1.1.1.1.10.1 serial.c
--- drivers/serial.c	20 Jan 2006 15:32:39 -0000	1.1.1.1.10.1
+++ drivers/serial.c	10 Feb 2006 15:02:52 -0000
@@ -245,13 +245,13 @@
 }
 
 /* send buflen bytes from buf with no delay */
-int ser_send_buf(int fd, const char *buf, size_t buflen)
+int ser_send_buf(int fd, const unsigned char *buf, size_t buflen)
 {
 	return (write(fd, buf, buflen));
 }
 
 /* send buflen bytes from buf with d_usec delay after each char */
-int ser_send_buf_pace(int fd, unsigned long d_usec, const char *buf, 
+int ser_send_buf_pace(int fd, unsigned long d_usec, const unsigned char *buf, 
 	size_t buflen)
 {
 	int	ret;
@@ -269,7 +269,7 @@
 	return buflen;
 }
 
-static int get_buf(int fd, char *buf, size_t buflen, long d_sec, long d_usec)
+static int get_buf(int fd, unsigned char *buf, size_t buflen, long d_sec, long d_usec)
 {
 	int	ret;
 	fd_set	rfds;
@@ -301,17 +301,17 @@
 	return ret;
 }
 
-int ser_get_char(int fd, char *ch, long d_sec, long d_usec)
+int ser_get_char(int fd, unsigned char *ch, long d_sec, long d_usec)
 {
 	return get_buf(fd, ch, 1, d_sec, d_usec);
 }
 
 /* keep reading until buflen bytes are received or a timeout occurs */
-int ser_get_buf_len(int fd, char *buf, size_t buflen, long d_sec, long d_usec)
+int ser_get_buf_len(int fd, unsigned char *buf, size_t buflen, long d_sec, long d_usec)
 {
-	int	i, ret;
-	char	tmp[64];
-	size_t	count = 0, tmplen;
+	int	       i, ret;
+	unsigned char  tmp[64];
+	size_t	       count = 0, tmplen;
 
 	memset(buf, '\0', buflen);
 
@@ -360,7 +360,7 @@
 	maxcount = buflen - 1;		/* for trailing \0 */
 
 	while (count < maxcount) {
-		ret = get_buf(fd, tmp, sizeof(tmp), d_sec, d_usec);
+		ret = get_buf(fd, (unsigned char*)tmp, sizeof(tmp), d_sec, d_usec);
 
 		if (ret < 1)
 			return -1;
@@ -404,7 +404,7 @@
 	int	ret, extra = 0;
 	char	ch;
 
-	while ((ret = ser_get_char(fd, &ch, 0, 0)) > 0) {
+	while ((ret = ser_get_char(fd, (unsigned char*)&ch, 0, 0)) > 0) {
 
 		if (strchr(ignset, ch))
 			continue;
Index: drivers/serial.h
===================================================================
RCS file: /cvsroot/nut/nut/drivers/serial.h,v
retrieving revision 1.1.1.1.10.1
diff -u -r1.1.1.1.10.1 serial.h
--- drivers/serial.h	20 Jan 2006 15:32:39 -0000	1.1.1.1.10.1
+++ drivers/serial.h	10 Feb 2006 15:02:52 -0000
@@ -32,16 +32,16 @@
 	__attribute__ ((__format__ (__printf__, 2, 3)));
 
 /* send buflen bytes from buf with no delay */
-int ser_send_buf(int fd, const char *buf, size_t buflen);
+int ser_send_buf(int fd, const unsigned char *buf, size_t buflen);
 
 /* send buflen bytes from buf with d_usec delay after each char */
-int ser_send_buf_pace(int fd, unsigned long d_usec, const char *buf, 
+int ser_send_buf_pace(int fd, unsigned long d_usec, const unsigned char *buf, 
 	size_t buflen);
 
-int ser_get_char(int fd, char *ch, long d_sec, long d_usec);
+int ser_get_char(int fd, unsigned char *ch, long d_sec, long d_usec);
 
 /* keep reading until buflen bytes are received or a timeout occurs */
-int ser_get_buf_len(int fd, char *buf, size_t buflen, long d_sec, long d_usec);
+int ser_get_buf_len(int fd, unsigned char *buf, size_t buflen, long d_sec, long d_usec);
 
 /* reads a line up to <endchar>, discarding anything else that may follow,
    with callouts to the handler if anything matches the alertset */


More information about the Nut-upsdev mailing list