Skip to content

Commit

Permalink
libc/baselibc: Add va_list pointer compiling error workaround
Browse files Browse the repository at this point in the history
On some architectures va_list is defined as an array, which leads
to compilation errors if va_list type is caller argument and
callee expects a pointer to it as it's argument. Thanks to this
the compilation errors in this situation no longer occur.
  • Loading branch information
m-gorecki committed Nov 7, 2023
1 parent 2dc8c99 commit badc6f6
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions libc/baselibc/src/tinyprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)
char ch;
char lng;
void *v;
va_list va_to_pass;
#if MYNEWT_VAL(FLOAT_USER)
double d;
int n;
Expand All @@ -257,6 +258,12 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)

p.bf = bf;

/* On some architectures va_list type is an array type, which will cause
* compilation errors if we simply pass &va to functions. Creating the copy
* of va and passing it to functions is a workaround of this issue.
*/
va_copy(va_to_pass, va);

while ((ch = *(fmt++))) {
if (ch != '%') {
written += putf(putp, ch);
Expand Down Expand Up @@ -294,7 +301,7 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)

/* Width */
if (ch == '*') {
i = intarg(0, 1, &va);
i = intarg(0, 1, &va_to_pass);
if (i > UCHAR_MAX) {
p.width = UCHAR_MAX;
} else if (i > 0) {
Expand Down Expand Up @@ -331,29 +338,29 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)
goto abort;
case 'u':
p.base = 10;
ui2a(intarg(lng, 0, &va), &p);
ui2a(intarg(lng, 0, &va_to_pass), &p);
written += putchw(putp, &p);
break;
case 'd':
case 'i':
p.base = 10;
i2a(intarg(lng, 1, &va), &p);
i2a(intarg(lng, 1, &va_to_pass), &p);
written += putchw(putp, &p);
break;
case 'x':
case 'X':
p.base = 16;
p.uc = (ch == 'X');
ui2a(intarg(lng, 0, &va), &p);
ui2a(intarg(lng, 0, &va_to_pass), &p);
written += putchw(putp, &p);
break;
case 'o':
p.base = 8;
ui2a(intarg(lng, 0, &va), &p);
ui2a(intarg(lng, 0, &va_to_pass), &p);
written += putchw(putp, &p);
break;
case 'p':
v = va_arg(va, void *);
v = va_arg(va_to_pass, void *);
p.base = 16;
ui2a((uintptr_t)v, &p);
p.width = 2 * sizeof(void*);
Expand All @@ -363,10 +370,10 @@ size_t tfp_format(FILE *putp, const char *fmt, va_list va)
written += putchw(putp, &p);
break;
case 'c':
written += putf(putp, (char)(va_arg(va, int)));
written += putf(putp, (char)(va_arg(va_to_pass, int)));
break;
case 's':
p.bf = va_arg(va, char *);
p.bf = va_arg(va_to_pass, char *);
written += putchw(putp, &p);
p.bf = bf;
break;
Expand Down

0 comments on commit badc6f6

Please sign in to comment.