input.c: nextLine EOF bug + patch

Sven M. Hallberg pesco@gmx.de
Mon, 29 Jul 2002 12:57:39 +0200


--G4iJoqBmSsgzjUCe
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Moin,

I've just stumbled across a bug in Hugs98 Dec2001 on my PowerPC running
Debian GNU/Linux.

The bug is in src/input.c, line 516:

    arctic:~/build/deb/hugs98-98.200112/src% make input.o
    gcc -c -g   -O2 input.c
    input.c: In function `nextLine':
    input.c:516: warning: comparison is always false due to limited range of data type
    arctic:~/build/deb/hugs98-98.200112/src% gcc --version
    2.95.4

The comparison in question is the check for EOF, which is #defined as -1 in
my stdio.h. This sent my Hugs into an infinite loop when loading the Prelude.

The attached patch fixes the problem by storing the result of fgetc() in an
int and using that for the comparison with EOF.


Happy hacking,
Sven Moritz

--G4iJoqBmSsgzjUCe
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="hugs98-Dec2001-nextLine-EOF-patch.diff"

diff -Nur hugs98-Dec2001/src/input.c hugs98-Dec2001_patched/src/input.c
--- hugs98-Dec2001/src/input.c	2001-12-13 06:25:55.000000000 +0100
+++ hugs98-Dec2001_patched/src/input.c	2002-07-29 12:29:16.000000000 +0200
@@ -511,10 +511,14 @@
 /* Returns line length (including \n) or 0 upon EOF. */
 static Int local nextLine()
 {
+    int char_code;
     for (lineLength = 0; lineLength < LINEBUFFER_SIZE-1; lineLength++) {
-        lineBuffer[lineLength] = fgetc(inputStream);
-        if (lineBuffer[lineLength] == EOF)
+        /* EOF is an int, so we can't cast to char right away. */
+        char_code = fgetc(inputStream);
+        if (char_code == EOF)
             break;
+
+        lineBuffer[lineLength] = (char) char_code;
 #if MULTI_LINEFEED
         if (lineBuffer[lineLength] == '\r') {
             char c = fgetc(inputStream);

--G4iJoqBmSsgzjUCe--