lib/core: fix Reader::read
authorLucas Bajolet <lucas.bajolet@gmail.com>
Fri, 11 May 2018 00:48:31 +0000 (20:48 -0400)
committerLucas Bajolet <lucas.bajolet@gmail.com>
Fri, 11 May 2018 03:16:40 +0000 (23:16 -0400)
Reader::read would crash if trying to read from an empty socket, as the
codec::decode_string would be called with a negative length.

We add a failsafe to prevent that.

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

lib/core/stream.nit

index d884b62..9c73b1d 100644 (file)
@@ -148,13 +148,16 @@ abstract class Reader
 
        # Reads a String of at most `i` length
        fun read(i: Int): String do
+               assert i >= 0
                var cs = new CString(i)
                var rd = read_bytes_to_cstring(cs, i)
+               if rd < 0 then return ""
                return codec.decode_string(cs, rd)
        end
 
        # Reads up to `max` bytes from source
        fun read_bytes(max: Int): Bytes do
+               assert max >= 0
                var cs = new CString(max)
                var rd = read_bytes_to_cstring(cs, max)
                return new Bytes(cs, rd, max)