Skip to content

[bug] binary stdin/stdout mode cannot be enabled on windows #10258

@ZeeWanderer

Description

@ZeeWanderer

Describe the bug
Even when open_port is called with binary there is a CRLF -> LF transformation as if the mode is text

To Reproduce
Try this

%% File: stdio_port_repro.erl
-module(stdio_port_repro).
-export([run/0, child/0]).

run() ->
    {ok, Erl} = find_erl(),
    Port = open_port(
      {spawn_executable, Erl},
      [{args, ["-noshell","-noinput","-s","stdio_port_repro","child"]},
       use_stdio, stderr_to_stdout, exit_status, binary]
    ),
    wait_ready(Port),
    lists:foreach(
      fun({Name, Payload}) -> do_test(Port, Name, Payload) end,
      [{"CRLF", <<13,10>>}, {"LF", <<10>>}, {"CR", <<13>>}]
    ),
    port_close(Port),
    receive {Port, {exit_status, _}} -> ok after 1000 -> ok end.

find_erl() ->
    case os:find_executable("erl") of
        false -> exit(no_erl_in_path);
        Path  -> {ok, Path}
    end.

wait_ready(Port) ->
    receive
        {Port, {data, <<"READY">>}} -> ok;
        {Port, _} -> wait_ready(Port)
    after 5000 ->
        exit(timeout_wait_ready)
    end.

do_test(Port, Name, Payload) ->
    port_command(Port, Payload),
    Echo = receive {Port, {data, Bin}} -> Bin after 2000 -> timeout end,
    io:format("~s sent: ~s echoed: ~s~n", [Name, hex(Payload), hex(Echo)]),
    case Echo of
        Payload -> io:format("~s => OK~n", [Name]);
        _       -> io:format("~s => MISMATCH~n", [Name])
    end.

hex(timeout) -> <<"timeout">>;
hex(Bin) when is_binary(Bin) ->
    iolist_to_binary([io_lib:format("~2.16.0B ", [B]) || <<B:8>> <= Bin]).

child() ->
    io:setopts([binary]),
    P = open_port({fd, 0, 1}, [binary, stream, eof]),
    port_command(P, <<"READY">>),
    loop(P).

loop(P) ->
    receive
        {P, {data, Bin}} -> port_command(P, Bin), loop(P);
        {P, eof} -> ok
    end.

Output is

CRLF sent: 0D 0A  echoed: 0A
CRLF => MISMATCH
LF sent: 0A  echoed: 0A
LF => OK
CR sent: 0D  echoed: 0A
CR => MISMATCH

Expected behavior

CRLF sent: 0D 0A  echoed: 0D 0A 
CRLF => OK
LF sent: 0A  echoed: 0A 
LF => OK
CR sent: 0D  echoed: 0D 
CR => OK

Affected versions
OTP 28

Additional context
WhatsApp/edb#8

Metadata

Metadata

Assignees

Labels

bugIssue is reported as a bugteam:VMAssigned to OTP team VM

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions