File 5781-Fix-a-bug-that-file-read-standard_io-.-unexpectedly-.patch of Package erlang
From 3139ec6cb704e91035ee04a2da8ea11104c65ec1 Mon Sep 17 00:00:00 2001
From: Takeru Ohta <phjgt308@gmail.com>
Date: Fri, 17 Feb 2023 15:19:15 +0900
Subject: [PATCH] Fix a bug that `file:read(standard_io, ..)` unexpectedly
returns `eof`.
---
lib/kernel/src/group.erl | 4 +++-
lib/stdlib/src/io_lib.erl | 24 ++++++++----------------
2 files changed, 11 insertions(+), 17 deletions(-)
diff --git a/lib/kernel/src/group.erl b/lib/kernel/src/group.erl
index 88556e4b9b..665b326ea0 100644
--- a/lib/kernel/src/group.erl
+++ b/lib/kernel/src/group.erl
@@ -518,8 +518,10 @@ get_chars_apply(Pbs, M, F, Xa, Drv, Shell, Buf, State0, Line, Encoding) ->
get_chars_n_loop(Pbs, M, F, Xa, Drv, Shell, Buf0, State, Encoding) ->
try M:F(State, cast(Buf0, get(read_mode), Encoding), Encoding, Xa) of
+ {stop,Result,eof} ->
+ {ok,Result,eof};
{stop,Result,Rest} ->
- {ok, Result, Rest};
+ {ok,Result,append(Rest, [], Encoding)};
State1 ->
case get_chars_echo_off(Pbs, Drv, Shell) of
interrupted ->
diff --git a/lib/stdlib/src/io_lib.erl b/lib/stdlib/src/io_lib.erl
index 9ef70008d6..7f8292e46f 100644
--- a/lib/stdlib/src/io_lib.erl
+++ b/lib/stdlib/src/io_lib.erl
@@ -808,23 +808,19 @@ collect_chars(Tag, Data, N) ->
%% Now we are aware of encoding...
collect_chars(start, Data, unicode, N) when is_binary(Data), is_integer(N) ->
{Size,Npos} = count_and_find_utf8(Data,N),
- if Size > N ->
+ if Size >= N ->
{B1,B2} = split_binary(Data, Npos),
{stop,B1,B2};
Size < N ->
- {binary,[Data],N-Size};
- true ->
- {stop,Data,eof}
+ {binary,[Data],N-Size}
end;
collect_chars(start, Data, latin1, N) when is_binary(Data), is_integer(N) ->
Size = byte_size(Data),
- if Size > N ->
+ if Size >= N ->
{B1,B2} = split_binary(Data, N),
{stop,B1,B2};
Size < N ->
- {binary,[Data],N-Size};
- true ->
- {stop,Data,eof}
+ {binary,[Data],N-Size}
end;
collect_chars(start,Data,_,N) when is_list(Data), is_integer(N) ->
collect_chars_list([], N, Data);
@@ -834,23 +830,19 @@ collect_chars({binary,Stack,_N}, eof, _,_) ->
{stop,binrev(Stack),eof};
collect_chars({binary,Stack,N}, Data,unicode, _) when is_integer(N) ->
{Size,Npos} = count_and_find_utf8(Data,N),
- if Size > N ->
+ if Size >= N ->
{B1,B2} = split_binary(Data, Npos),
{stop,binrev(Stack, [B1]),B2};
Size < N ->
- {binary,[Data|Stack],N-Size};
- true ->
- {stop,binrev(Stack, [Data]),eof}
+ {binary,[Data|Stack],N-Size}
end;
collect_chars({binary,Stack,N}, Data,latin1, _) when is_integer(N) ->
Size = byte_size(Data),
- if Size > N ->
+ if Size >= N ->
{B1,B2} = split_binary(Data, N),
{stop,binrev(Stack, [B1]),B2};
Size < N ->
- {binary,[Data|Stack],N-Size};
- true ->
- {stop,binrev(Stack, [Data]),eof}
+ {binary,[Data|Stack],N-Size}
end;
collect_chars({list,Stack,N}, Data, _,_) when is_integer(N) ->
collect_chars_list(Stack, N, Data);
--
2.35.3