]> Repositorios git - scryer-prolog.git/commitdiff
fix type_error with instantiated EOF -1 byte literal in get_byte/2
authorManos Pitsidianakis <[email protected]>
Wed, 26 Oct 2022 10:49:38 +0000 (13:49 +0300)
committerManos Pitsidianakis <[email protected]>
Wed, 26 Oct 2022 10:49:38 +0000 (13:49 +0300)
According to ISO Prolog, get_byte/2 predicate can receive an
instantiated input byte:

http://www.gprolog.org/manual/html_node/gprolog037.html#sec156

    get_byte(+stream_or_alias, ?in_byte)

Since in_byte can be -1 if EOF is reached, instantiating it with -1
should work but does not because the implementation is trying to convert
it to a u8 which is unsigned:

?- open("/dev/null", read, S, [type(binary)]), get_byte(S, -1).
   error(type_error(in_byte,-1),get_byte/2).

This commit adds an extra check for -1 before checking for a valid u8
instantiated value if in_byte is an input:

?- open("/dev/null", read, S, [type(binary)]), get_byte(S, -1).
   S = '$stream'(0x55601e65c998).

Closes #1625

src/machine/system_calls.rs

index 46d4801182ec4a6f297794e97cde3523575b32aa..7e3f5568cd11152d218fe8b1b4b04cff9cfcff43 100644 (file)
@@ -2432,6 +2432,12 @@ impl Machine {
             addr
         } else {
             match Number::try_from(addr) {
+                Ok(Number::Integer(ref n)) if **n == -1_i64 => {
+                    fixnum_as_cell!(Fixnum::build_with(-1))
+                }
+                Ok(Number::Fixnum(n)) if n.get_num() == -1_i64 => {
+                    fixnum_as_cell!(Fixnum::build_with(-1))
+                }
                 Ok(Number::Integer(n)) => {
                     if let Some(nb) = n.to_u8() {
                         fixnum_as_cell!(Fixnum::build_with(nb as i64))