Bug 31183 - Wide stream buffer size reduced MB_LEN_MAX bytes after bug 17522 fix
Summary: Wide stream buffer size reduced MB_LEN_MAX bytes after bug 17522 fix
Status: RESOLVED FIXED
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: 2.21
: P2 normal
Target Milestone: 2.39
Assignee: Florian Weimer
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-20 15:43 UTC by Florian Weimer
Modified: 2024-01-02 13:43 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Florian Weimer 2023-12-20 15:43:37 UTC
After the fix from bug 17522, we do not use the remaining buffer space if the already-written buffer space is smaller than MB_LEN_MAX (16 bytes).  As we start out with no written bytes (zero), we never use the full buffer, greatly reducing performance.

The fix probably explains it better:

-	  if (fp->_IO_write_ptr - fp->_IO_write_base < sizeof (mb_buf))
+	  if (fp->_IO_buf_end - fp->_IO_write_ptr < sizeof (mb_buf))
 	    {
 	      /* Make sure we have room for at least one multibyte
 		 character.  */
 	      write_ptr = write_base = mb_buf;
 	      buf_end = mb_buf + sizeof (mb_buf);
 	    }
Comment 1 Florian Weimer 2023-12-20 16:17:14 UTC
Patch posted:

[PATCH] libio: Check remaining buffer size in _IO_wdo_write (bug 31183)
<https://inbox.sourceware.org/libc-alpha/87bkakx3b0.fsf@oldenburg.str.redhat.com/>
Comment 2 Florian Weimer 2024-01-02 13:43:45 UTC
Fixed for 2.39 via:

commit ecc7c3deb9f347649c2078fcc0f94d4cedf92d60
Author: Florian Weimer <fweimer@redhat.com>
Date:   Tue Jan 2 14:36:17 2024 +0100

    libio: Check remaining buffer size in _IO_wdo_write (bug 31183)
    
    The multibyte character needs to fit into the remaining buffer space,
    not the already-written buffer space.  Without the fix, we were never
    moving the write pointer from the start of the buffer, always using
    the single-character fallback buffer.
    
    Fixes commit 04b76b5aa8b2d1d19066e42dd1 ("Don't error out writing
    a multibyte character to an unbuffered stream (bug 17522)").