Skip to content

Commit fcd61c8

Browse files
committed
fixed boot_snprintf, and nanoprintf linking bugs
1 parent 12e83a1 commit fcd61c8

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

Diff for: docs/headers/ti/sprintf.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The OS comes with an implementation of ANSI C89 `sprintf`, which can reduce the
1212
boot_sprintf
1313
------------
1414

15-
The following type specifiers are supported :code:`%s %c %d %i %u %o %x %X %p %n`. The minimum field width :code:`*`, precision :code:`.*`, alongside :code:`-+#0` and the space flag are also supported.
15+
The following type specifiers are supported :code:`%s %c %d %i %u %o %x %X %p %n`. The minimum field width :code:`*`, precision :code:`.*`, alongside :code:`-+#0` and the space flag are also supported. However, the precision :code:`.*` field is ignored for integers.
1616

1717
All length modifiers :code:`hh h l ll j z t L` and floating point specifiers :code:`%f %g %e %a` are **not** supported.
1818

Diff for: src/libc/boot_vsprintf.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int boot_vsnprintf(char *__restrict buffer, size_t count, const char *__restrict
2424
int boot_snprintf(char *__restrict buffer, size_t count, const char *__restrict format, ...) {
2525
va_list args;
2626
va_start(args, format);
27-
const int ret = vsnprintf(buffer, count, format, args);
27+
const int ret = boot_vsnprintf(buffer, count, format, args);
2828
va_end(args);
2929
return ret;
3030
}

Diff for: src/libc/nanoprintf.c

+26-24
Original file line numberDiff line numberDiff line change
@@ -982,15 +982,7 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
982982
#undef NPF_EXTRACT
983983
#undef NPF_WRITEBACK
984984

985-
int _printf_c(char const *format, ...) {
986-
va_list va;
987-
va_start(va, format);
988-
int const rv = vprintf(format, va);
989-
va_end(va);
990-
return rv;
991-
}
992-
993-
int _vsnprintf_c(char *buffer, size_t bufsz, char const *format, va_list vlist) {
985+
int _vsnprintf_c(char *__restrict buffer, size_t bufsz, char const *__restrict format, va_list vlist) {
994986
npf_bufputc_ctx_t bufputc_ctx;
995987
bufputc_ctx.dst = buffer;
996988
bufputc_ctx.len = bufsz;
@@ -1009,36 +1001,31 @@ int _vsnprintf_c(char *buffer, size_t bufsz, char const *format, va_list vlist)
10091001
return n;
10101002
}
10111003

1012-
int _snprintf_c(char *buffer, size_t bufsz, const char *format, ...) {
1004+
int _snprintf_c(char *__restrict buffer, size_t bufsz, const char *__restrict format, ...) {
10131005
va_list va;
10141006
va_start(va, format);
1015-
int const rv = vsnprintf(buffer, bufsz, format, va);
1007+
int const rv = _vsnprintf_c(buffer, bufsz, format, va);
10161008
va_end(va);
10171009
return rv;
10181010
}
10191011

1020-
int _vsprintf_c(char *buffer, const char *format, va_list vlist)
1012+
int _vsprintf_c(char *__restrict buffer, const char *__restrict format, va_list vlist)
10211013
{
1022-
return vsnprintf(buffer, (size_t)INT_MAX, format, vlist);
1023-
}
1024-
1025-
int _vprintf_c(const char *format, va_list vlist)
1026-
{
1027-
return npf_vpprintf(npf_putc_std, NULL, format, vlist);
1014+
return _vsnprintf_c(buffer, (size_t)INT_MAX, format, vlist);
10281015
}
10291016

1030-
int _sprintf_c(char *buffer, const char *format, ...)
1017+
int _sprintf_c(char *__restrict buffer, const char *__restrict format, ...)
10311018
{
10321019
va_list va;
10331020
va_start(va, format);
1034-
const int ret = vsnprintf(buffer, (size_t)INT_MAX, format, va);
1021+
const int ret = _vsnprintf_c(buffer, (size_t)INT_MAX, format, va);
10351022
va_end(va);
10361023
return ret;
10371024
}
10381025

10391026
int _vasprintf_c(char **__restrict p_str, const char *__restrict format, va_list vlist) {
10401027
*p_str = NULL;
1041-
int str_len = vsnprintf(NULL, 0, format, vlist);
1028+
int str_len = _vsnprintf_c(NULL, 0, format, vlist);
10421029
if (str_len <= 0) {
10431030
return str_len;
10441031
}
@@ -1048,7 +1035,7 @@ int _vasprintf_c(char **__restrict p_str, const char *__restrict format, va_list
10481035
// malloc failure
10491036
return -1;
10501037
}
1051-
int ret = vsnprintf(buf, buf_len, format, vlist);
1038+
int ret = _vsnprintf_c(buf, buf_len, format, vlist);
10521039
if (ret <= 0) {
10531040
free(buf);
10541041
return ret;
@@ -1060,11 +1047,12 @@ int _vasprintf_c(char **__restrict p_str, const char *__restrict format, va_list
10601047
int _asprintf_c(char **__restrict p_str, const char *__restrict format, ...) {
10611048
va_list va;
10621049
va_start(va, format);
1063-
const int ret = vasprintf(p_str, format, va);
1050+
const int ret = _vasprintf_c(p_str, format, va);
10641051
va_end(va);
10651052
return ret;
10661053
}
10671054

1055+
__attribute__((__always_inline__))
10681056
int _vfprintf_c(FILE* __restrict stream, const char* __restrict format, va_list vlist)
10691057
{
10701058
return npf_vpprintf(npf_fputc_std, (void*)stream, format, vlist);
@@ -1074,11 +1062,25 @@ int _fprintf_c(FILE* __restrict stream, const char* __restrict format, ...)
10741062
{
10751063
va_list va;
10761064
va_start(va, format);
1077-
const int ret = vfprintf(stream, format, va);
1065+
const int ret = _vfprintf_c(stream, format, va);
10781066
va_end(va);
10791067
return ret;
10801068
}
10811069

1070+
__attribute__((__always_inline__))
1071+
int _vprintf_c(const char *__restrict format, va_list vlist)
1072+
{
1073+
return npf_vpprintf(npf_putc_std, NULL, format, vlist);
1074+
}
1075+
1076+
int _printf_c(char const *__restrict format, ...) {
1077+
va_list va;
1078+
va_start(va, format);
1079+
int const rv = _vprintf_c(format, va);
1080+
va_end(va);
1081+
return rv;
1082+
}
1083+
10821084
#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS
10831085
#pragma GCC diagnostic pop
10841086
#endif

Diff for: test/standalone/asprintf_fprintf/src/main.c

+24-3
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static char const * const test_3 =
7373
"sprintf%%%% %%is unsafe";
7474
static const int pos_3 = 20;
7575

76-
static const int pos_4 = 209;
76+
static const int pos_4 = 212;
7777

7878
static char* buf = NULL;
7979
static FILE* file = NULL;
@@ -153,7 +153,7 @@ int boot_sprintf_tests(void) {
153153
"%%%.*s\n"
154154
"Characters:\t%c %%\n"
155155
"Integers:\n"
156-
"%%*Decimal:\t%i %d %.6i %i %.0i %+i %i\n"
156+
"%%*Decimal:\t%i %d %.6i %i %3u %+i %i\n"
157157
"*%%Hexadecimal:\t%x %x %X %#x\n"
158158
"%%*.*%%Octal:\t%o %#o %#o\n"
159159
"Width trick: %*d \n",
@@ -256,7 +256,7 @@ int nano_tests(void) {
256256
"%%%.*s\n"
257257
"Characters:\t%c %%\n"
258258
"Integers:\n"
259-
"%%*Decimal:\t%i %d %.6i %i %.0i %+i %i\n"
259+
"%%*Decimal:\t%i %d %.6i %i %3u %+i %i\n"
260260
"*%%Hexadecimal:\t%x %x %X %#x\n"
261261
"%%*.*%%Octal:\t%o %#o %#o\n"
262262
"Width trick: %*d \n",
@@ -467,6 +467,27 @@ int run_tests(void) {
467467
return 0;
468468
}
469469

470+
#if 0
471+
static void write_letter(char c) {
472+
if (isgraph(c)) {
473+
fputc(c, stdout);
474+
return;
475+
}
476+
fputc('\\', stdout);
477+
switch (c) {
478+
case '\0': fputc('0', stdout); return;
479+
case ' ': fputc('s', stdout); return;
480+
case '\n': fputc('n', stdout); return;
481+
case '\t': fputc('t', stdout); return;
482+
case '\v': fputc('v', stdout); return;
483+
case '\r': fputc('r', stdout); return;
484+
case '\f': fputc('f', stdout); return;
485+
case '\b': fputc('b', stdout); return;
486+
default: printf("x%02X", (unsigned int)c); return;
487+
}
488+
}
489+
#endif
490+
470491
int main(void)
471492
{
472493
os_ClrHome();

0 commit comments

Comments
 (0)