Fix EOF == -1 assumption in 1994/shapiro

The problem is that the getc() functions return EOF on error/EOF but it
assumed that it returns -1. EOF is not required to be -1 but merely an
int < 0 so on systems where it was not -1 it could be a problem. But
changing the != -1 check to be != EOF caused a new compiler warning and
errors. However since EOF < 0 and since the code functions fine with the
check being >= 0 the code now checks that the return value of getc() >= 0
as the condition of the for loop. This takes care of any problem where
EOF != -1.

Fixed accidental change in shapiro.md code excerpt. Note that that one
(the first one) does not actually work but it never did (or if it did it
doesn't in modern systems). I'm not sure this is a problem though as the
author stated that without the comments the program will not do anything
right. I believe the author was showing the algorithm without comments.
This commit is contained in:
Cody Boone Ferguson committed 2023-04-02 10:10:23 -07:00
1 parent e09f677b37
commit 0c543beb0f
4 files changed
+17 -89

No files matched your search

+15 -11
View File
@@ -10,17 +10,21 @@ USA
make all
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) pointed out a potential
bug on systems where `EOF != -1`. The problem is that `getc()` and the related
functions do not return `-1` on EOF or error but rather `EOF` but the program
assumes `-1` (conversely when using the `getopt()` functions one should not
check for EOF but -1 as the functions return -1 not EOF but the standard does
not require `EOF` to be -1, just a negative number. However changing it to check
for `!=EOF` caused a problem due to the clever encoding of the code. See
[bugs.md](/bugs.md) for more details if you wish to try and fix this (Cody is
here and there working on it along with other things and will change this
message when it's fixed assuming it's feasible).
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed a bug on systems
where `EOF != -1`. The problem is that `getc()` and the related functions do not
return `-1` on EOF or error but rather `EOF`. However `EOF` is not required to be
`-1` but merely an int < 0 and this program assumed that `getc()` will return
`-1` on EOF or error, not `EOF`. On systems where `EOF != -1` it could be a
problem. For the same reason one should not use `EOF` for the `getopt()`
functions as they return `-1` when all options are parsed (for details on the
definition of `EOF` see `7.21 Input/output<stdio.h>` subsection 1 of the
standard). An interesting problem occurred where changing the `-1` to `EOF`
caused both `warning: illegal character encoding in string literal` and `error:
source file is not valid UTF-8`. But since `EOF` is simply an int < 0 and making
the loop condition check that the return value is >= 0 does not cause a
compilation error and it functions correctly it will address the systems where
`EOF != -1` just as if it checked for `!= EOF`. Thank you Cody for your
assistance!
## To run: