More printf de-duplication.

Fix the 'j' (intmax_t/uintmax_t) length qualifier in the wide
variant. (With new tests that fail without this fix.)

Fix a typo in the wide support for intmax_t*, which isn't testable because
%n is disabled on Android (and will be removed in a later cleanup pass).

Also move the public vfprintf/vfwprint functions into stdio.cpp.

Bug: http://b/67371539
Test: ran tests
Change-Id: Ib003599b1e9cb789044a068940b59e447f2cb7cb
This commit is contained in:
Elliott Hughes 2017-11-02 16:58:44 -07:00
parent 46621f43b0
commit 618303ca4a
4 changed files with 78 additions and 25 deletions

View file

@ -935,6 +935,16 @@ int swscanf(const wchar_t* s, const wchar_t* fmt, ...) {
PRINTF_IMPL(vswscanf(s, fmt, ap));
}
int vfprintf(FILE* fp, const char* fmt, va_list ap) {
ScopedFileLock sfl(fp);
return __vfprintf(fp, fmt, ap);
}
int vfwprintf(FILE* fp, const wchar_t* fmt, va_list ap) {
ScopedFileLock sfl(fp);
return __vfwprintf(fp, fmt, ap);
}
int vprintf(const char* fmt, va_list ap) {
return vfprintf(stdout, fmt, ap);
}

View file

@ -83,7 +83,7 @@ union arg {
wchar_t* pwchararg;
};
static int __find_arguments(const char* fmt0, va_list ap, union arg** argtable, size_t* argtablesiz);
static int __find_arguments(const CHAR_TYPE* fmt0, va_list ap, union arg** argtable, size_t* argtablesiz);
static int __grow_type_table(unsigned char** typetable, int* tablesize);
/*
@ -250,16 +250,6 @@ static int exponent(CharT* p0, int exp, int fmtch) {
#define CHARINT 0x0800 /* 8 bit integer */
#define MAXINT 0x1000 /* largest integer size (intmax_t) */
int vfprintf(FILE* fp, const char* fmt0, __va_list ap) {
int ret;
FLOCKFILE(fp);
ret = __vfprintf(fp, fmt0, ap);
FUNLOCKFILE(fp);
return (ret);
}
DEF_STRONG(vfprintf);
int __vfprintf(FILE* fp, const char* fmt0, __va_list ap) {
char* fmt; /* format string */
int ch; /* character from fmt */

View file

@ -83,8 +83,7 @@ union arg {
wchar_t* pwchararg;
};
static int __find_arguments(const wchar_t* fmt0, va_list ap, union arg** argtable,
size_t* argtablesiz);
static int __find_arguments(const CHAR_TYPE* fmt0, va_list ap, union arg** argtable, size_t* argtablesiz);
static int __grow_type_table(unsigned char** typetable, int* tablesize);
/*
@ -997,17 +996,6 @@ finish:
return (ret);
}
int vfwprintf(FILE* __restrict fp, const wchar_t* __restrict fmt0, __va_list ap) {
int r;
FLOCKFILE(fp);
r = __vfwprintf(fp, fmt0, ap);
FUNLOCKFILE(fp);
return (r);
}
DEF_STRONG(vfwprintf);
/*
* Type ids for argument type table.
*/
@ -1192,6 +1180,9 @@ static int __find_arguments(const wchar_t* fmt0, va_list ap, union arg** argtabl
flags |= SHORTINT;
}
goto rflag;
case 'j':
flags |= MAXINT;
goto rflag;
case 'l':
if (*fmt == 'l') {
fmt++;
@ -1367,9 +1358,15 @@ done:
case TP_SSIZEINT:
(*argtable)[n].pssizearg = va_arg(ap, ssize_t*);
break;
case TP_MAXINT:
case T_MAXINT:
(*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
break;
case T_MAXUINT:
(*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
break;
case TP_MAXINT:
(*argtable)[n].pintmaxarg = va_arg(ap, intmax_t*);
break;
case T_WINT:
(*argtable)[n].wintarg = va_arg(ap, wint_t);
break;

View file

@ -621,6 +621,38 @@ TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
EXPECT_EQ(std::wstring(L"<a>"), buf);
}
TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
constexpr size_t nchars = 32;
wchar_t buf[nchars];
swprintf(buf, nchars, L"%jd", INTMAX_MAX);
EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
}
TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
constexpr size_t nchars = 32;
wchar_t buf[nchars];
swprintf(buf, nchars, L"%jd", INTMAX_MIN);
EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
}
TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
constexpr size_t nchars = 32;
wchar_t buf[nchars];
swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
}
TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
constexpr size_t nchars = 32;
wchar_t buf[nchars];
swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
}
TEST(STDIO_TEST, swprintf_ls) {
constexpr size_t nchars = 32;
wchar_t buf[nchars];
@ -655,6 +687,30 @@ TEST(STDIO_TEST, snprintf_d_INT_MIN) {
EXPECT_STREQ("-2147483648", buf);
}
TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
EXPECT_STREQ("9223372036854775807", buf);
}
TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
EXPECT_STREQ("-9223372036854775808", buf);
}
TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
EXPECT_STREQ("18446744073709551615", buf);
}
TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
EXPECT_STREQ("18446744073709551615", buf);
}
TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%ld", LONG_MAX);