moused(8): char signed-ness problem with gcc 3.1

"Akinori MUSHA" <[email protected]>
Newsgroups gmane.os.freebsd.devel.audit
Organization Associated I. Daemons
Message-ID <[email protected]>
I observed gcc 2.95.4 and gcc 3.1 interpret (or maybe optimize) the
following code differently (CFLAGS=-O):

int main(void)
{
  unsigned char i = 127;
  printf("%d\n", ((char)(i << 1)) / 2);
  return 0;
}

gcc 2.95.4 says it's -1, whereas gcc 3.1 says it's 127.  On FreeBSD
char should be signed, so I suspect it's a (optimization) bug of gcc
3.1 which should be fixed.  Or we'll have to do a mass audit of the
whole src tree to check and fix the similar expressions.

In any case, this behavior makes moused(8) return a stupid value of
127 when you roll the mouse wheel up under ps/2-sysmouse-intellimouse
protocol.  Attached is a patch that makes the whole expression look
more logical and works around the above behavior at the same time.

-- 
                     /
                    /__  __            Akinori.org / MUSHA.org
                   / )  )  ) )  /     FreeBSD.org / Ruby-lang.org
Akinori MUSHA aka / (_ /  ( (__(  @ iDaemons.org / and.or.jp

"Somewhere out of a memory.. of lighted streets on quiet nights.."

Index: moused.c
===================================================================
RCS file: /home/ncvs/src/usr.sbin/moused/moused.c,v
retrieving revision 1.55
diff -u -r1.55 moused.c
--- moused.c	28 Apr 2002 11:59:30 -0000	1.55
+++ moused.c	15 May 2002 17:16:40 -0000
@@ -1972,7 +1972,7 @@
 	act->dx =    (char)(pBuf[1]) + (char)(pBuf[3]);
 	act->dy = - ((char)(pBuf[2]) + (char)(pBuf[4]));
 	if (rodent.level == 1) {
-	    act->dz = ((char)(pBuf[5] << 1) + (char)(pBuf[6] << 1))/2;
+	    act->dz = ((char)(pBuf[5] << 1) + (char)(pBuf[6] << 1)) >> 1;
 	    act->button |= ((~pBuf[7] & MOUSE_SYS_EXTBUTTONS) << 3);
 	}
 	break;

To Unsubscribe: send mail to [email protected]
with "unsubscribe freebsd-audit" in the body of the message
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.