[PATCH v2 05/14] stdio-common: Introduce buffers for implementing printf

Florian Weimer fweimer@redhat.com
Thu Jun 2 19:12:56 GMT 2022


* Adhemerval Zanella:

> On 02/06/2022 15:41, Florian Weimer wrote:
>> * Adhemerval Zanella:
>> 
>>>>>> +void
>>>>>> +Xprintf (buffer_puts_1) (struct Xprintf_buffer *buf, const CHAR_T *s)
>>>>>> +{
>>>>>> +  if (__glibc_unlikely (Xprintf_buffer_has_failed (buf)))
>>>>>> +    return;
>>>>>> +
>>>>>> +  while (*s != 0)
>>>>>> +    {
>>>>>> +      if (buf->write_ptr == buf->write_end && !Xprintf_buffer_flush (buf))
>>>>>> +        return;
>>>>>> +      assert (buf->write_ptr != buf->write_end);
>>>>>> +      size_t to_copy = STRNLEN (s, buf->write_end - buf->write_ptr);
>>>>>> +      buf->write_ptr = MEMPCPY (buf->write_ptr, s, to_copy);
>>>>>> +      s += to_copy;
>>>>>> +    }
>>>>>> +}
>>>>>
>>>>> Does it really need a loop here?
>>>>
>>>> Yes, we might reach the end of the buffer with data left to write, so we
>>>> have to flush it.  This can happen with asprintf and of course in the
>>>> FILE * case.
>>>>
>>>
>>> But in this case case you will just return right? I am trying to understand
>>> where the loop will be executed twice in this case.
>> 
>> No, the return happens only if we need to flush (==, no room in the
>> buffer) and the flush fails, e.g. due to a write system call failure, or
>> maybe because we have reached the end of the buffer for snprintf.
>> 
>
> Sigh, I forgot about the stage buffer you keep for printf_buffer_to_file.

This buffer is also used for asprintf, obstack_printf, dprintf.  It's
not just the FILE * interfaces that can produce arbitrary-sized output.

Thanks,
Florian



More information about the Libc-alpha mailing list