Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions newlib/libc/tinystdio/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ typedef __uint16_t __ungetc_t;
struct __file {
__ungetc_t unget; /* ungetc() buffer */
__uint8_t flags; /* flags, see below */
#ifdef __IO_PERCENT_N
size_t buflimit; /* buffer limit size */
#endif
#define __SRD 0x0001 /* OK to read */
#define __SWR 0x0002 /* OK to write */
#define __SERR 0x0004 /* found error */
Expand Down
8 changes: 8 additions & 0 deletions newlib/libc/tinystdio/stdio_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
#include <stdio-bufio.h>
#include <sys/lock.h>

#ifdef __IO_PERCENT_N
#define PRINTF_BUF_LIMIT(_s, _end) \
.buflimit = (size_t)((_end) ? ((char *)(_end) - (char *)(_s)) : (INT_MAX)),
#else
#define PRINTF_BUF_LIMIT(_s, _end)
#endif

struct __file_str {
struct __file file; /* main file struct */
char *pos; /* current buffer position */
Expand Down Expand Up @@ -111,6 +118,7 @@ bool __matchcaseprefix(const char *input, const char *pattern);
#define FDEV_SETUP_STRING_WRITE(_s, _end) { \
.file = { \
.flags = __SWR, \
PRINTF_BUF_LIMIT(_s, _end) \
.put = __file_str_put, \
__LOCK_INIT_NONE \
}, \
Expand Down
39 changes: 29 additions & 10 deletions newlib/libc/tinystdio/vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ typedef long ultoa_signed_t;
#define arg_to_unsigned(ap, flags, result_var) arg_to_t(ap, flags, unsigned, result_var)
#define arg_to_signed(ap, flags, result_var) arg_to_t(ap, flags, signed, result_var)

#define ASSIGN_STREAM_LEN(stream_len, buflimit, flags, type) \
if (flags & __SBUF) { \
*va_arg(ap, type *) = stream_len; \
} else { \
*va_arg(ap, type *) = ((size_t)stream_len >= buflimit) ? \
(type)buflimit : \
(type)stream_len; \
}

#include "ultoa_invert.c"

/* Order is relevant here and matches order in format string */
Expand Down Expand Up @@ -1192,17 +1201,25 @@ int vfprintf (FILE * stream, const CHAR *fmt, va_list ap_orig)
goto handle_error;
#else
if (flags & FL_LONG) {
if (flags & FL_REPD_TYPE)
*va_arg(ap, long long *) = stream_len;
else
*va_arg(ap, long *) = stream_len;
if ((flags)&FL_REPD_TYPE) {
ASSIGN_STREAM_LEN(stream_len, stream->buflimit,
stream->flags, long long);
} else {
ASSIGN_STREAM_LEN(stream_len, stream->buflimit,
stream->flags, long);
}
} else if (flags & FL_SHORT) {
if (flags & FL_REPD_TYPE)
*va_arg(ap, char *) = stream_len;
else
*va_arg(ap, short *) = stream_len;
if ((flags)&FL_REPD_TYPE) {
ASSIGN_STREAM_LEN(stream_len, stream->buflimit,
stream->flags, char);
} else {
ASSIGN_STREAM_LEN(stream_len, stream->buflimit,
stream->flags, short);
}
} else {
*va_arg(ap, int *) = stream_len;
ASSIGN_STREAM_LEN(stream_len, stream->buflimit,
stream->flags, int);

}
#endif
#endif
Expand Down Expand Up @@ -1249,7 +1266,9 @@ int vfprintf (FILE * stream, const CHAR *fmt, va_list ap_orig)
base = 2;
#endif
} else {
my_putc('%', stream);
while (--width > 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems unrelated to the rest of the patch?

my_putc(' ', stream);
}
my_putc(c, stream);
continue;
}
Expand Down