From owner-freebsd-audit Wed May 15 13:22:42 2002 Delivered-To: freebsd-audit@freebsd.org Received: from dignus.com (sdsl-64-32-254-102.dsl.iad.megapath.net [64.32.254.102]) by hub.freebsd.org (Postfix) with ESMTP id 98B5037B407; Wed, 15 May 2002 13:22:25 -0700 (PDT) Received: from lakes.dignus.com (lakes.dignus.com [10.0.0.3]) by dignus.com (8.11.6/8.11.3) with ESMTP id g4FKHVa15972; Wed, 15 May 2002 16:17:31 -0400 (EDT) (envelope-from rivers@dignus.com) Received: (from rivers@localhost) by lakes.dignus.com (8.11.6/8.11.3) id g4FKIc946014; Wed, 15 May 2002 16:18:38 -0400 (EDT) (envelope-from rivers) Date: Wed, 15 May 2002 16:18:38 -0400 (EDT) From: Thomas David Rivers Message-Id: <200205152018.g4FKIc946014@lakes.dignus.com> To: audit@FreeBSD.ORG, knu@iDaemons.org Subject: Re: moused(8): char signed-ness problem with gcc 3.1 Cc: current@FreeBSD.ORG In-Reply-To: <86sn4t8fzp.wl@archon.local.idaemons.org> Sender: owner-freebsd-audit@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG > > 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. Let's examine the what the "right" answer should be: First - in the expression (i << 1) - the unsigned char `i' will be promoted to a signed int through the correct integral promotion rules, then left-shifted 1 bit. The result of that is an int. So - this becomes: ((char)(254)) / 2 ; The expression: (char)(254) is then also promoted to int when it participates in the division operation. So, the value 254 is converted into a (signed) char, and then converted to an int. Converting 254 to a signed character should result in an integer value of -2. Then, -2 / 2 becomes -1. If characters were unsigned by default, you do get the value 127... So - yes - it seems gcc 3.1 does have a problem... - Dave Rivers - -- rivers@dignus.com Work: (919) 676-0847 Get your mainframe programming tools at http://www.dignus.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message