[newlib-cygwin/cygwin-3_5-branch] Cygwin: pipe: fix shift value

Corinna Vinschen corinna@sourceware.org
Thu Oct 31 12:16:54 GMT 2024


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

commit 3d364b98c0c31c72ec2df382af2631ca414e4ff8
Author:     Corinna Vinschen <corinna@vinschen.de>
AuthorDate: Thu Oct 31 11:52:26 2024 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Thu Oct 31 13:16:17 2024 +0100

    Cygwin: pipe: fix shift value
    
    The expression computing the next-less-power of 2 for the next write
    when the pipe buffer is getting filled up allows negative shift values.
    This works on Intel CPUs because the shift expression only evaluates the
    5 LSBs, but it's undefined behaviour per the C standard.  Use the
    correct expression to get a positive shift value.
    
    Fixes: 170e6badb621 ("Cygwin: pipe: improve writing when pipe buffer is almost full")
    Reported-by: Takashi Yano <takashi.yano@nifty.ne.jp>
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/fhandler/pipe.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc
index 3b78cc183e36..6a1ef03b69a4 100644
--- a/winsup/cygwin/fhandler/pipe.cc
+++ b/winsup/cygwin/fhandler/pipe.cc
@@ -580,7 +580,7 @@ fhandler_pipe_fifo::raw_write (const void *ptr, size_t len)
 	  else if (avail >= PIPE_BUF)
 	    len1 = avail & ~(PIPE_BUF - 1);
 	  else
-	    len1 = 1 << (31 - __builtin_clzl (avail));
+	    len1 = 1 << (63 - __builtin_clzl (avail));
 	  short_write_once = true;
 	}
       if (isclosed ())  /* A signal handler might have closed the fd. */


More information about the Cygwin-cvs mailing list