[net.sources.bugs] Bug in compress

willcox@ccvaxa.UUCP (01/11/85)

I hope that this is the right place to report bugs from mod.sources.

Thanks for "compress".  It is impressive.  I wonder how many of the
floppies for my micro I can free up.

I did run into one bug that shows up when you try to run compress on a
machine that correctly implements comparison between signed and
unsigned ints.  The following code appears in two places in
compress.c:

    while ( (c = getchar()) != (unsigned) EOF ) {

The problem is that ((unsigned) -1) is not the same thing as ((int) -1).
The vax (and many other machines) do 32-bit compares, and say they are
equal, but other machines (correctly) do not.  The program drops into
an infinite loop looking for EOF.

The "(unsigned)" in the above expression should be removed.  It won't
break the program on machines that do the compare wrong, and will make
it work on machines that do it right.

With this change, compress works on the Gould machines.

David A. Willcox
Gould CSD, North
Urbana, IL 61801
(217) 384-8500
{ihnp4,decvax}!uiucdcs!ccvaxa

joe@petsd.UUCP (Joe Orost) (01/14/85)

In article <53800002@ccvaxa.UUCP> willcox@ccvaxa.UUCP writes:
>I did run into one bug that shows up when you try to run compress on a
>machine that correctly implements comparison between signed and
>unsigned ints.  The following code appears in two places in
>compress.c:
>
>    while ( (c = getchar()) != (unsigned) EOF ) {
>
>The problem is that ((unsigned) -1) is not the same thing as ((int) -1).
>The vax (and many other machines) do 32-bit compares, and say they are
>equal, but other machines (correctly) do not.  The program drops into
>an infinite loop looking for EOF.
>
>The "(unsigned)" in the above expression should be removed.  It won't
>break the program on machines that do the compare wrong, and will make
>it work on machines that do it right.
>
>With this change, compress works on the Gould machines.

You are absolutely correct.  The above code was inserted for the
Perkin-Elmer machine which outputs a 4 times slower compare instruction
without the "(unsigned)".  The correct fix follows:

Index: compress.c
710a711
> #ifdef interdata
711a713,715
> #else !interdata
>     while ( (c = getchar()) != EOF ) {
> #endif interdata
741a746
> #ifdef interdata
742a748,750
> #else !interdata
>     while ( (c = getchar()) != EOF ) {
> #endif interdata

					regards,
					joe

--
Full-Name:  Joseph M. Orost
UUCP:       ..!{decvax,ucbvax,ihnp4}!vax135!petsd!joe
US Mail:    MS 313; Perkin-Elmer; 106 Apple St; Tinton Falls, NJ 07724
Phone:      (201) 870-5844
Location:   40 19'49" N / 74 04'37" W

ka@hou3c.UUCP (Kenneth Almquist) (01/16/85)

> The following code appears in two places in compress.c:
> 
>     while ( (c = getchar()) != (unsigned) EOF ) {
> 
> The problem is that ((unsigned) -1) is not the same thing as ((int) -1).
> The vax (and many other machines) do 32-bit compares, and say they are
> equal, but other machines (correctly) do not.  The program drops into
> an infinite loop looking for EOF.

The bug is in your C compiler, not in compress.  When one operand of
a binary operator is of type int and the other is of type unsigned,
both operands should be converted to unsigned.
					Kenneth Almquist


"Of course I don't read the C reference manual; it didn't come over the USENET!"

rpw3@redwood.UUCP (Rob Warnock) (01/16/85)

+---------------
| You are absolutely correct.  The above code was inserted for the
| Perkin-Elmer machine which outputs a 4 times slower compare instruction
| without the "(unsigned)".  The correct fix follows:
| --
| Full-Name:  Joseph M. Orost
| UUCP:       ..!{decvax,ucbvax,ihnp4}!vax135!petsd!joe
| US Mail:    MS 313; Perkin-Elmer; 106 Apple St; Tinton Falls, NJ 07724
+---------------

Uhh... This may be a stupid question, but if it's that slow, would there
be any problem in simply defining "EOF" to be "((unsigned) -1)" in the
Interdata "stdio.h"? That way, everyone in the world doesn't have to
put "#ifdef interdata" in the code.


Rob Warnock
Systems Architecture Consultant

UUCP:	{ihnp4,ucbvax!dual}!fortune!redwood!rpw3
DDD:	(415)572-2607
USPS:	510 Trinidad Lane, Foster City, CA  94404