From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7829 invoked by alias); 24 Nov 2006 13:05:25 -0000 Received: (qmail 7812 invoked by uid 22791); 24 Nov 2006 13:05:24 -0000 X-Spam-Check-By: sourceware.org Received: from sunsite.ms.mff.cuni.cz (HELO sunsite.mff.cuni.cz) (195.113.15.26) by sourceware.org (qpsmtpd/0.31) with ESMTP; Fri, 24 Nov 2006 13:05:04 +0000 Received: from sunsite.mff.cuni.cz (sunsite.mff.cuni.cz [127.0.0.1]) by sunsite.mff.cuni.cz (8.13.1/8.13.1) with ESMTP id kAOD4nlC017692; Fri, 24 Nov 2006 14:04:49 +0100 Received: (from jj@localhost) by sunsite.mff.cuni.cz (8.13.1/8.13.1/Submit) id kAOD4nhJ017688; Fri, 24 Nov 2006 14:04:49 +0100 Date: Fri, 24 Nov 2006 13:05:00 -0000 From: Jakub Jelinek To: Ulrich Drepper , Arjan van de Ven Cc: Glibc hackers Subject: [PATCH] Fix wide stdio Message-ID: <20061124130448.GK3849@sunsite.mff.cuni.cz> Reply-To: Jakub Jelinek Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.1i Mailing-List: contact libc-hacker-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-hacker-owner@sourceware.org X-SW-Source: 2006-11/txt/msg00017.txt.bz2 Hi! On #include #include #include #include #include #include int main (int argc, char **argv) { char name[] = "/tmp/wprintf.out.XXXXXX"; int fd; FILE *fp; fd = mkstemp (name); fp = fdopen (dup (fd), "a"); if (fp == NULL) error (EXIT_FAILURE, errno, "fdopen(,\"a\")"); setvbuf (fp, NULL, _IONBF, 0); fwprintf (fp, L"hello.\n"); fclose (fp); return EXIT_SUCCESS; } testcase from Arjan we attempt to munmap fp->_shortbuf (which is fortunately not page aligned and thus the munmap just fails). The problem seems to be that we use just one _IO_USER_BUF bit, for two different buffers (fp->_IO_buf_base and fp->_wide_data->_IO_buf_base). If wide orientation used just one of these buffers, we could just clear the narrow buffers in _IO_fwide when switching to wide orientation, but when wide oriented, we actually use both buffers at the same time, and they aren't necessarily both user buffers or both non-user buffers. So, IMHO we just have to use a different bit for the fp->_wide_data->_IO_buf_base allocation status (whether we are supposed to free it or not). Additionally, as _IO_new_file_close_it unconditionally overwrites fp->_flags, we need to free not just the wide buffer for wide oriented streams, but also the narrow one. 2006-11-24 Jakub Jelinek * libio/libio.h (_IO_FLAGS2_USER_WBUF): Define. * libio/wgenops.c (_IO_wsetb, _IO_wdefault_finish): Test and set _IO_FLAGS2_USER_WBUF bit in _flags2 instead of _IO_USER_BUF bit in _flags. * libio/wstrops.c (_IO_wstr_overflow, enlarge_userbuf, _IO_wstr_finish): Likewise. * libio/wmemstream.c (open_wmemstream): Likewise. * libio/fileops.c (_IO_new_file_close_it): Call _IO_set[bgp] even for wide streams. --- libc/libio/libio.h.jj 2006-11-24 12:51:29.000000000 +0100 +++ libc/libio/libio.h 2006-11-24 13:30:23.000000000 +0100 @@ -142,6 +142,7 @@ #ifdef _LIBC # define _IO_FLAGS2_FORTIFY 4 #endif +#define _IO_FLAGS2_USER_WBUF 8 /* These are "formatting flags" matching the iostream fmtflags enum values. */ #define _IO_SKIPWS 01 --- libc/libio/wgenops.c.jj 2006-11-24 12:51:29.000000000 +0100 +++ libc/libio/wgenops.c 2006-11-24 13:30:23.000000000 +0100 @@ -115,14 +115,14 @@ _IO_wsetb (f, b, eb, a) wchar_t *eb; int a; { - if (f->_wide_data->_IO_buf_base && !(f->_flags & _IO_USER_BUF)) + if (f->_wide_data->_IO_buf_base && !(f->_flags2 & _IO_FLAGS2_USER_WBUF)) FREE_BUF (f->_wide_data->_IO_buf_base, _IO_wblen (f) * sizeof (wchar_t)); f->_wide_data->_IO_buf_base = b; f->_wide_data->_IO_buf_end = eb; if (a) - f->_flags &= ~_IO_USER_BUF; + f->_flags2 &= ~_IO_FLAGS2_USER_WBUF; else - f->_flags |= _IO_USER_BUF; + f->_flags2 |= _IO_FLAGS2_USER_WBUF; } INTDEF(_IO_wsetb) @@ -198,7 +198,7 @@ _IO_wdefault_finish (fp, dummy) int dummy; { struct _IO_marker *mark; - if (fp->_wide_data->_IO_buf_base && !(fp->_flags & _IO_USER_BUF)) + if (fp->_wide_data->_IO_buf_base && !(fp->_flags2 & _IO_FLAGS2_USER_WBUF)) { FREE_BUF (fp->_wide_data->_IO_buf_base, _IO_wblen (fp) * sizeof (wchar_t)); --- libc/libio/wstrops.c.jj 2006-11-24 12:51:29.000000000 +0100 +++ libc/libio/wstrops.c 2006-11-24 13:30:23.000000000 +0100 @@ -88,7 +88,7 @@ _IO_wstr_overflow (fp, c) pos = fp->_wide_data->_IO_write_ptr - fp->_wide_data->_IO_write_base; if (pos >= (_IO_size_t) (_IO_wblen (fp) + flush_only)) { - if (fp->_flags & _IO_USER_BUF) /* not allowed to enlarge */ + if (fp->_flags2 & _IO_FLAGS2_USER_WBUF) /* not allowed to enlarge */ return WEOF; else { @@ -182,7 +182,7 @@ enlarge_userbuf (_IO_FILE *fp, _IO_off64 _IO_ssize_t oldend = wd->_IO_write_end - wd->_IO_write_base; /* Try to enlarge the buffer. */ - if (fp->_flags & _IO_USER_BUF) + if (fp->_flags2 & _IO_FLAGS2_USER_WBUF) /* User-provided buffer. */ return 1; @@ -335,7 +335,7 @@ _IO_wstr_finish (fp, dummy) _IO_FILE *fp; int dummy; { - if (fp->_wide_data->_IO_buf_base && !(fp->_flags & _IO_USER_BUF)) + if (fp->_wide_data->_IO_buf_base && !(fp->_flags2 & _IO_FLAGS2_USER_WBUF)) (((_IO_strfile *) fp)->_s._free_buffer) (fp->_wide_data->_IO_buf_base); fp->_wide_data->_IO_buf_base = NULL; --- libc/libio/wmemstream.c.jj 2006-11-24 12:51:29.000000000 +0100 +++ libc/libio/wmemstream.c 2006-11-24 13:30:23.000000000 +0100 @@ -92,7 +92,7 @@ open_wmemstream (bufloc, sizeloc) _IO_fwide (&new_f->fp._sf._sbf._f, 1); _IO_wstr_init_static (&new_f->fp._sf._sbf._f, buf, _IO_BUFSIZ / sizeof (wchar_t), buf); - new_f->fp._sf._sbf._f._flags &= ~_IO_USER_BUF; + new_f->fp._sf._sbf._f._flags2 &= ~_IO_FLAGS2_USER_WBUF; new_f->fp._sf._s._allocate_buffer = (_IO_alloc_type) malloc; new_f->fp._sf._s._free_buffer = (_IO_free_type) free; --- libc/libio/fileops.c.jj 2006-11-24 12:51:29.000000000 +0100 +++ libc/libio/fileops.c 2006-11-24 13:34:37.000000000 +0100 @@ -174,14 +174,8 @@ _IO_new_file_close_it (fp) close_status = _IO_SYSCLOSE (fp); /* Free buffer. */ - if (fp->_mode <= 0) - { - INTUSE(_IO_setb) (fp, NULL, NULL, 0); - _IO_setg (fp, NULL, NULL, NULL); - _IO_setp (fp, NULL, NULL); - } #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T - else + if (fp->_mode > 0) { if (_IO_have_wbackup (fp)) INTUSE(_IO_free_wbackup_area) (fp); @@ -190,6 +184,9 @@ _IO_new_file_close_it (fp) _IO_wsetp (fp, NULL, NULL); } #endif + INTUSE(_IO_setb) (fp, NULL, NULL, 0); + _IO_setg (fp, NULL, NULL, NULL); + _IO_setp (fp, NULL, NULL); INTUSE(_IO_un_link) ((struct _IO_FILE_plus *) fp); fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS; Jakub