]> Repositorios git - scryer-prolog.git/commitdiff
Fix phrase_from_stream infinite loop
authorJavier Sagredo <[email protected]>
Fri, 29 May 2026 00:34:03 +0000 (02:34 +0200)
committerJavier Sagredo <[email protected]>
Fri, 29 May 2026 00:34:03 +0000 (02:34 +0200)
src/lib/pio.pl

index 0204a19e20f91bbbdee5a7708bcb0db0bc1d6142..880381e09c8e938d2144cd83e58ed089adc76263 100644 (file)
@@ -140,11 +140,17 @@ buffer_prepare_for_n(Stream, BufferId, BufferPosId, BufferLenId, N) :-
             ;   chars_to_read(CharsToRead),
                 get_n_chars(Stream, CharsToRead, Chars),
                 length(Chars, NChars),
-                partial_string(Chars, BufferTail, _),
-                bb_put(BufferId, Buffer),
-                BufferLen1 is BufferLen + NChars,
-                bb_put(BufferLenId, BufferLen1),
-                buffer_prepare_for_n(Stream, BufferId, BufferPosId, BufferLenId, N)
+                (   NChars =:= 0 ->
+                    % get_n_chars returning [] means EOF, even on streams
+                    % whose at_end_of_stream/1 never reports true (pipes).
+                    BufferTail = [],
+                    bb_put(BufferId, Buffer)
+                ;   partial_string(Chars, BufferTail, _),
+                    bb_put(BufferId, Buffer),
+                    BufferLen1 is BufferLen + NChars,
+                    bb_put(BufferLenId, BufferLen1),
+                    buffer_prepare_for_n(Stream, BufferId, BufferPosId, BufferLenId, N)
+                )
             )
         ;   true
         )