mirror of
https://sourceware.org/git/glibc.git
synced 2025-08-08 17:42:12 +03:00
libio: Convert __vdprintf_internal to buffers
The internal buffer size is set to 2048 bytes. This is less than the original BUFSIZ value used by buffered_vfprintf before the conversion, but it hopefully covers all cases where write boundaries matter. Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
This commit is contained in:
@@ -50,6 +50,7 @@ enum __printf_buffer_mode
|
|||||||
__printf_buffer_mode_sprintf_chk,
|
__printf_buffer_mode_sprintf_chk,
|
||||||
__printf_buffer_mode_to_file,
|
__printf_buffer_mode_to_file,
|
||||||
__printf_buffer_mode_asprintf,
|
__printf_buffer_mode_asprintf,
|
||||||
|
__printf_buffer_mode_dprintf,
|
||||||
__printf_buffer_mode_strfmon,
|
__printf_buffer_mode_strfmon,
|
||||||
__printf_buffer_mode_fp, /* For __printf_fp_l_buffer. */
|
__printf_buffer_mode_fp, /* For __printf_fp_l_buffer. */
|
||||||
__printf_buffer_mode_fp_to_wide, /* For __wprintf_fp_l_buffer. */
|
__printf_buffer_mode_fp_to_wide, /* For __wprintf_fp_l_buffer. */
|
||||||
@@ -308,6 +309,9 @@ void __printf_buffer_flush_to_file (struct __printf_buffer_to_file *)
|
|||||||
struct __printf_buffer_asprintf;
|
struct __printf_buffer_asprintf;
|
||||||
void __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *)
|
void __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *)
|
||||||
attribute_hidden;
|
attribute_hidden;
|
||||||
|
struct __printf_buffer_dprintf;
|
||||||
|
void __printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *)
|
||||||
|
attribute_hidden;
|
||||||
struct __printf_buffer_fp;
|
struct __printf_buffer_fp;
|
||||||
void __printf_buffer_flush_fp (struct __printf_buffer_fp *)
|
void __printf_buffer_flush_fp (struct __printf_buffer_fp *)
|
||||||
attribute_hidden;
|
attribute_hidden;
|
||||||
@@ -341,4 +345,9 @@ void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *)
|
|||||||
(final, correctly sized) heap allocation. */
|
(final, correctly sized) heap allocation. */
|
||||||
#define PRINTF_BUFFER_SIZE_ASPRINTF 200
|
#define PRINTF_BUFFER_SIZE_ASPRINTF 200
|
||||||
|
|
||||||
|
/* This should cover most of the packet-oriented file descriptors,
|
||||||
|
where boundaries between writes could be visible to readers. But
|
||||||
|
it is still small enough not to cause too many stack overflow issues. */
|
||||||
|
#define PRINTF_BUFFER_SIZE_DPRINTF 2048
|
||||||
|
|
||||||
#endif /* PRINTF_BUFFER_H */
|
#endif /* PRINTF_BUFFER_H */
|
||||||
|
@@ -24,41 +24,52 @@
|
|||||||
This exception applies to code released by its copyright holders
|
This exception applies to code released by its copyright holders
|
||||||
in files containing the exception. */
|
in files containing the exception. */
|
||||||
|
|
||||||
#include <libioP.h>
|
#include <array_length.h>
|
||||||
|
#include <math_ldbl_opt.h>
|
||||||
|
#include <printf.h>
|
||||||
#include <stdio_ext.h>
|
#include <stdio_ext.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <printf_buffer.h>
|
||||||
|
|
||||||
|
struct __printf_buffer_dprintf
|
||||||
|
{
|
||||||
|
struct __printf_buffer base;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
char buf[PRINTF_BUFFER_SIZE_DPRINTF];
|
||||||
|
};
|
||||||
|
|
||||||
|
void
|
||||||
|
__printf_buffer_flush_dprintf (struct __printf_buffer_dprintf *buf)
|
||||||
|
{
|
||||||
|
char *p = buf->buf;
|
||||||
|
char *end = buf->base.write_ptr;
|
||||||
|
while (p < end)
|
||||||
|
{
|
||||||
|
ssize_t ret = TEMP_FAILURE_RETRY (write (buf->fd, p, end - p));
|
||||||
|
if (ret < 0)
|
||||||
|
{
|
||||||
|
__printf_buffer_mark_failed (&buf->base);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
p += ret;
|
||||||
|
}
|
||||||
|
buf->base.write_ptr = buf->buf;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
__vdprintf_internal (int d, const char *format, va_list arg,
|
__vdprintf_internal (int d, const char *format, va_list arg,
|
||||||
unsigned int mode_flags)
|
unsigned int mode_flags)
|
||||||
{
|
{
|
||||||
struct _IO_FILE_plus tmpfil;
|
struct __printf_buffer_dprintf buf;
|
||||||
struct _IO_wide_data wd;
|
__printf_buffer_init (&buf.base, buf.buf, array_length (buf.buf),
|
||||||
int done;
|
__printf_buffer_mode_dprintf);
|
||||||
|
buf.fd = d;
|
||||||
#ifdef _IO_MTSAFE_IO
|
__printf_buffer (&buf.base, format, arg, mode_flags);
|
||||||
tmpfil.file._lock = NULL;
|
if (__printf_buffer_has_failed (&buf.base))
|
||||||
#endif
|
return -1;
|
||||||
_IO_no_init (&tmpfil.file, _IO_USER_LOCK, 0, &wd, &_IO_wfile_jumps);
|
__printf_buffer_flush_dprintf (&buf);
|
||||||
_IO_JUMPS (&tmpfil) = &_IO_file_jumps;
|
return __printf_buffer_done (&buf.base);
|
||||||
_IO_new_file_init_internal (&tmpfil);
|
|
||||||
if (_IO_file_attach (&tmpfil.file, d) == NULL)
|
|
||||||
{
|
|
||||||
_IO_un_link (&tmpfil);
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
tmpfil.file._flags |= _IO_DELETE_DONT_CLOSE;
|
|
||||||
|
|
||||||
_IO_mask_flags (&tmpfil.file, _IO_NO_READS,
|
|
||||||
_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING);
|
|
||||||
|
|
||||||
done = __vfprintf_internal (&tmpfil.file, format, arg, mode_flags);
|
|
||||||
|
|
||||||
if (done != EOF && _IO_do_flush (&tmpfil.file) == EOF)
|
|
||||||
done = EOF;
|
|
||||||
|
|
||||||
_IO_FINISH (&tmpfil.file);
|
|
||||||
|
|
||||||
return done;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@@ -28,6 +28,7 @@
|
|||||||
# pragma weak __printf_buffer_flush_snprintf
|
# pragma weak __printf_buffer_flush_snprintf
|
||||||
# pragma weak __printf_buffer_flush_to_file
|
# pragma weak __printf_buffer_flush_to_file
|
||||||
# pragma weak __printf_buffer_flush_asprintf
|
# pragma weak __printf_buffer_flush_asprintf
|
||||||
|
# pragma weak __printf_buffer_flush_dprintf
|
||||||
# pragma weak __printf_buffer_flush_fp
|
# pragma weak __printf_buffer_flush_fp
|
||||||
# pragma weak __printf_buffer_flush_fp_to_wide
|
# pragma weak __printf_buffer_flush_fp_to_wide
|
||||||
# pragma weak __printf_buffer_flush_fphex_to_wide
|
# pragma weak __printf_buffer_flush_fphex_to_wide
|
||||||
@@ -53,6 +54,9 @@ __printf_buffer_do_flush (struct __printf_buffer *buf)
|
|||||||
case __printf_buffer_mode_asprintf:
|
case __printf_buffer_mode_asprintf:
|
||||||
__printf_buffer_flush_asprintf ((struct __printf_buffer_asprintf *) buf);
|
__printf_buffer_flush_asprintf ((struct __printf_buffer_asprintf *) buf);
|
||||||
return;
|
return;
|
||||||
|
case __printf_buffer_mode_dprintf:
|
||||||
|
__printf_buffer_flush_dprintf ((struct __printf_buffer_dprintf *) buf);
|
||||||
|
return;
|
||||||
case __printf_buffer_mode_strfmon:
|
case __printf_buffer_mode_strfmon:
|
||||||
__set_errno (E2BIG);
|
__set_errno (E2BIG);
|
||||||
__printf_buffer_mark_failed (buf);
|
__printf_buffer_mark_failed (buf);
|
||||||
|
Reference in New Issue
Block a user