lib/core: add blocking eof implementation
authorLucas Bajolet <lucas.bajolet@gmail.com>
Wed, 9 May 2018 01:54:07 +0000 (21:54 -0400)
committerLucas Bajolet <lucas.bajolet@gmail.com>
Fri, 11 May 2018 18:41:12 +0000 (14:41 -0400)
eof is a problematic operation for Streams as its state may be decided
by reading input (in sockets for instance).

Because of that, operations relying on it may read one extra byte at the
end of a file, causing the apparition of bad characters (null byte, or
replacement character).

By changing the semantic of eof and making it blocking, we ensure that
querying eof will always produce the appropriate error.

Signed-off-by: Lucas Bajolet <lucas.bajolet@gmail.com>

lib/core/file.nit
lib/core/stream.nit

index d8be114..26f7398 100644 (file)
@@ -143,10 +143,15 @@ class FileReader
        end
 
        redef fun eof do
-               if lookahead_length != 0 then
-                       return false
+               var fl = _file
+               if fl == null then return true
+               if fl.address_is_null then return true
+               if last_error != null then return true
+               if super then
+                       if last_error != null then return true
+                       return fl.feof
                end
-               return _file.as(not null).feof
+               return false
        end
 
        # Open the file at `path` for reading.
index c86ea1b..03838a6 100644 (file)
@@ -412,7 +412,11 @@ abstract class Reader
 
        # Is there something to read.
        # This function returns 'false' if there is something to read.
-       fun eof: Bool is abstract
+       fun eof: Bool do
+               if lookahead_length > 0 then return false
+               lookahead_length = raw_read_bytes(lookahead, 1)
+               return lookahead_length <= 0
+       end
 
        # Read the next sequence of non whitespace characters.
        #