From 8aaa6f57dad3769ee25cf0d7c91e653386c65c4f Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Fri, 29 May 2026 02:34:03 +0200 Subject: [PATCH] Fix phrase_from_stream infinite loop --- src/lib/pio.pl | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib/pio.pl b/src/lib/pio.pl index 0204a19e..880381e0 100644 --- a/src/lib/pio.pl +++ b/src/lib/pio.pl @@ -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 ) -- 2.54.0