[net.bugs.4bsd] unsigned {short,char} vs. negative constant in 4.?bsd

ado@elsie.UUCP (Arthur David Olson) (11/20/84)

> The following program demonstrates an interesting bug on the VAX
> (4.1 and 4.2 BSD UNIX).  The comparison of unsigned short x = 0xffff
> and the integer constant 0xffffffff should fail. . .
> 
> main()
> {
>     unsigned short x;
>     int y;
> 
>     x = 0xffff; 
> 
>     if (x == 0xffffffff)
> 	printf("x == 0xffffffff, x = %x, 0xffffffff = %x\n", x, 0xffffffff);
>     else
> 	printf("x != 0xffffffff, x = %x, 0xffffffff = %x\n", x, 0xffffffff);
> }

The good news is that "lint" checks for and warns about such comparisons.

The other news is that the 4.?bsd compiler mishandles such comparisons.
It also mishandles comparisons involving unsigned chars.
Here's the addition I've made to 4.1bsd's "/usr/src/cmd/pcc/local2.c"
to try to make things right.  As usual, the trade secret status of the code
precludes a more useful posting.

- ed /usr/src/cmd/pcc/local2.c
- /^optim2/;/switch/a
- #ifndef OLDVERSION
- /*
- ** Do these mappings:
- **	unsigned char expr < negative constant		unsigned char expr, 0
- **	unsigned char expr == negative constant		unsigned char expr, 0
- **	unsigned char expr <= negative constant		unsigned char expr, 0
- **	unsigned char expr > negative constant		unsigned char expr, 1
- **	unsigned char expr != negative constant		unsigned char expr, 1
- **	unsigned char expr >= negative constant		unsigned char expr, 1
- **	unsigned short expr < negative constant		unsigned short expr, 0
- **	unsigned short expr == negative constant	unsigned short expr, 0
- **	unsigned short expr <= negative constant	unsigned short expr, 0
- **	unsigned short expr > negative constant		unsigned short expr, 1
- **	unsigned short expr != negative constant	unsigned short expr, 1
- **	unsigned short expr >= negative constant	unsigned short expr, 1
- ** Doing this allows us to have optimized "table.c" entries for comparisons
- ** involving unsigned {short,char}s and SSCONs.
- */
- 	case UGE:
- 	case ULE:
- 	case UGT:
- 	case ULT:
- 	case EQ:
- 	case NE:
- 		if (p->in.left->in.type != UCHAR &&
- 			p->in.left->in.type != USHORT)
- 				break;
- 		if (p->in.right->in.op != ICON)
- 			break;
- 		if (p->in.right->in.name[0] != '\0')
- 			break;
- 		if (p->in.right->tn.lval >= 0)
- 			break;
- 		p->in.right->tn.lval = 	p->in.op == NE ||
- 					p->in.op == UGE ||
- 					p->in.op == UGT;
- 		p->in.op = COMOP;
- 		break;
- #endif
- .
- w
- q

--
Bugs and UNIX are Warner Brothers and AT&T Bell Laboratories trademarks.
--
	..decvax!seismo!elsie!ado			(301) 496-5688
	DEC, VAX and Elsie are Digital Equipment and Borden trademarks