[newlib-cygwin/cygwin-3_6-branch] Cygwin: pty: Treat CR/NL in accept_input() the same as in transfer_input()

Takashi Yano via Cygwin-cvs <[email protected]> Wed, 24 Jun 2026 12:33:29 +0000 (GMT)
Newsgroups gmane.os.cygwin.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Df2faf906732=
ba2cbcaa00199e8cd12b189e04c79

commit f2faf906732ba2cbcaa00199e8cd12b189e04c79
Author: Takashi Yano <[email protected]>
Date:   Fri Jun 12 21:12:20 2026 +0900

    Cygwin: pty: Treat CR/NL in accept_input() the same as in transfer_inpu=
t()
   =20
    In transfer_input(), CR and NL in the data transferred to nat-pipe
    is treated as follows:
      1) If pseudo console is activated, convert NL to CR.
      2) If pseudo console is disabled, convert CR to NL.
    This conversion is necessary to ensure non-cygwin apps can handle
    CR/NL as expected. Therefor, CR and NL should be treated as the
    same way in accept_input() if the data is sent to nat-pipe.
   =20
    In the previous implementation, problems rarely occurred because
    accept_input() normally does not handle input for non-cygwin apps
    when the pseudo console is active. Under typical conditions, such
    input is set to pseudo console directly by WriteFile(), so
    accept_input() is not involved and no conversion issues arise.
   =20
    There is, however, a brief period during pseudo console initialization
    in which accept_input *does* handle the input. This happens because
    master::write() invokes line_edit() while in pcons_start mode. During
    this short window, the input is processed in pseudo-console-enabled
    mode, and the usual conversion behaviour may not apply.
   =20
    When the pseudo console is disabled, accept_input() always handles
    the input, and in most cases the ICRNL flag is set by shell, so
    line_edit() performs the CR->NL conversion. But if the flag is not
    set, this conversion does not occur. Therefore, the additional
    conversion introduced by this patch is required to ensure consistent
    behaviour in both cases.
   =20
    Fixes: f20641789427 ("Cygwin: pty: Reduce unecessary input transfer.")
    Signed-off-by: Takashi Yano <[email protected]>
    Reviewed-by: Mark Geisert <[email protected]>
    (cherry picked from commit 0f6dad639e1791d95cffd7a2490b82dff354eec2)

Diff:
---
 winsup/cygwin/fhandler/pty.cc | 8 ++++++++
 winsup/cygwin/release/3.6.10  | 2 ++
 2 files changed, 10 insertions(+)

diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index b1e42dafb..d27ba4e59 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -525,6 +525,14 @@ fhandler_pty_master::accept_input ()
 	  p =3D mbbuf;
 	  bytes_left =3D nlen;
 	}
+
+      char *p0 =3D p;
+      if (get_ttyp ()->pcon_activated)
+	while ((p0 =3D (char *) memchr (p0, '\n', bytes_left - (p0 - p))))
+	  *p0 =3D '\r';
+      else
+	while ((p0 =3D (char *) memchr (p0, '\r', bytes_left - (p0 - p))))
+	  *p0 =3D '\n';
     }
=20
   if (!bytes_left)
diff --git a/winsup/cygwin/release/3.6.10 b/winsup/cygwin/release/3.6.10
index e37ccb391..4d847829b 100644
--- a/winsup/cygwin/release/3.6.10
+++ b/winsup/cygwin/release/3.6.10
@@ -16,3 +16,5 @@ Fixes:
   Addresses: https://cygwin.com/pipermail/cygwin/2026-June/259776.html
=20
 - Fix race issue between starting and exiting non-cygwin apps in pty.
+
+- Fix CR/NL conversion in accept_input() for pty.