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

Takashi Yano tyan0@sourceware.org
Wed Jun 24 12:33:29 GMT 2026


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=f2faf906732ba2cbcaa00199e8cd12b189e04c79

commit f2faf906732ba2cbcaa00199e8cd12b189e04c79
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Fri Jun 12 21:12:20 2026 +0900

    Cygwin: pty: Treat CR/NL in accept_input() the same as in transfer_input()
    
    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.
    
    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.
    
    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.
    
    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.
    
    Fixes: f20641789427 ("Cygwin: pty: Reduce unecessary input transfer.")
    Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
    Reviewed-by: Mark Geisert <mark@maxrnd.com>
    (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 = mbbuf;
 	  bytes_left = nlen;
 	}
+
+      char *p0 = p;
+      if (get_ttyp ()->pcon_activated)
+	while ((p0 = (char *) memchr (p0, '\n', bytes_left - (p0 - p))))
+	  *p0 = '\r';
+      else
+	while ((p0 = (char *) memchr (p0, '\r', bytes_left - (p0 - p))))
+	  *p0 = '\n';
     }
 
   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
 
 - Fix race issue between starting and exiting non-cygwin apps in pty.
+
+- Fix CR/NL conversion in accept_input() for pty.


More information about the Cygwin-cvs mailing list