This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] abort: Only flush file-based stdio streams before termination
On 18/08/2017 11:25, Carlos O'Donell wrote:
> On 08/18/2017 10:11 AM, Florian Weimer wrote:
>> On 08/18/2017 03:45 PM, Carlos O'Donell wrote:
>>
>>> Do we have a technical objection to flushing the open memory streams?
>>
>> abort needs to be async-signal-safe and our current implementation of
>> open_memstream allocates on flush, for example if exactly 8192 bytes
>> have been written:
>
> Thank you for checking this.
>
> Adhemerval, Is there any way to remove the allocation on flush?
>
I think so, the open_memstream malloc on Florian tests came from
the _IO_str_overflow at:
libio/memstream.c:
107 static int
108 _IO_mem_sync (_IO_FILE *fp)
109 {
110 struct _IO_FILE_memstream *mp = (struct _IO_FILE_memstream *) fp;
111
112 if (fp->_IO_write_ptr == fp->_IO_write_end)
113 {
114 _IO_str_overflow (fp, '\0');
115 --fp->_IO_write_ptr;
116 }
117
118 *mp->bufloc = fp->_IO_write_base;
119 *mp->sizeloc = fp->_IO_write_ptr - fp->_IO_write_base;
120
121 return 0;
122 }
However this '\0' overflow check is superflous, underlying FILE operation
used for data input already keep the buffer NULL-terminated (because
_IO_str_overflow which is used for buffer enlarge zeros the buffer).
Removing the code and just setting _IO_mem_sync does not trigger any
regression and avoid the memory allocation.