Skip to content

[libc] Fix printf handling of INT_MIN width #101729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions libc/docs/dev/printf_behavior.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ If a conversion specification is provided an invalid type modifier, that type
modifier will be ignored, and the default type for that conversion will be used.
In the case of the length modifier "L" and integer conversions, it will be
treated as if it was "ll" (lowercase LL). For this purpose the list of integer
conversions is d, i, u, o, x, X, n.
conversions is d, i, u, o, x, X, b, B, n.

If a conversion specification ending in % has any options that consume arguments
(e.g. "%*.*%") those arguments will be consumed as normal, but their values will
Expand All @@ -169,9 +169,13 @@ be ignored.
If a conversion specification ends in a null byte ('\0') then it shall be
treated as an invalid conversion followed by a null byte.

If a number passed as a min width or precision value is out of range for an int,
then it will be treated as the largest or smallest value in the int range
(e.g. "%-999999999999.999999999999s" is the same as "%-2147483648.2147483647s").
If a number passed as a field width or precision value is out of range for an
int, then it will be treated as the largest value in the int range
(e.g. "%-999999999999.999999999999s" is the same as "%-2147483647.2147483647s").

If the field width is set to INT_MIN by using the '*' form,
e.g. printf("%*d", INT_MIN, 1), it will be treated as INT_MAX, since -INT_MIN is
not representable as an int.

If a number passed as a bit width is less than or equal to zero, the conversion
is considered invalid. If the provided bit width is larger than the width of
Expand Down
3 changes: 2 additions & 1 deletion libc/src/stdio/printf_core/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ template <typename ArgProvider> class Parser {
cur_pos = cur_pos + result.parsed_len;
}
if (section.min_width < 0) {
section.min_width = -section.min_width;
section.min_width =
(section.min_width == INT_MIN) ? INT_MAX : -section.min_width;
section.flags = static_cast<FormatFlags>(section.flags |
FormatFlags::LEFT_JUSTIFIED);
}
Expand Down
41 changes: 41 additions & 0 deletions libc/test/src/stdio/printf_core/parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,47 @@ TEST(LlvmLibcPrintfParserTest, EvalThreeArgs) {
ASSERT_PFORMAT_EQ(expected2, format_arr[2]);
}

TEST(LlvmLibcPrintfParserTest, EvalOneArgWithOverflowingWidthAndPrecision) {
LIBC_NAMESPACE::printf_core::FormatSection format_arr[10];
const char *str = "%-999999999999.999999999999d";
int arg1 = 12345;
evaluate(format_arr, str, arg1);

LIBC_NAMESPACE::printf_core::FormatSection expected;
expected.has_conv = true;

expected.raw_string = {str, 28};
expected.flags = LIBC_NAMESPACE::printf_core::FormatFlags::LEFT_JUSTIFIED;
expected.min_width = INT_MAX;
expected.precision = INT_MAX;
expected.conv_val_raw = arg1;
expected.conv_name = 'd';

ASSERT_PFORMAT_EQ(expected, format_arr[0]);
}

TEST(LlvmLibcPrintfParserTest,
EvalOneArgWithOverflowingWidthAndPrecisionAsArgs) {
LIBC_NAMESPACE::printf_core::FormatSection format_arr[10];
const char *str = "%*.*d";
int arg1 = INT_MIN; // INT_MIN = -2147483648 if int is 32 bits.
int arg2 = INT_MIN;
int arg3 = 12345;
evaluate(format_arr, str, arg1, arg2, arg3);

LIBC_NAMESPACE::printf_core::FormatSection expected;
expected.has_conv = true;

expected.raw_string = {str, 5};
expected.flags = LIBC_NAMESPACE::printf_core::FormatFlags::LEFT_JUSTIFIED;
expected.min_width = INT_MAX;
expected.precision = arg2;
expected.conv_val_raw = arg3;
expected.conv_name = 'd';

ASSERT_PFORMAT_EQ(expected, format_arr[0]);
}

#ifndef LIBC_COPT_PRINTF_DISABLE_INDEX_MODE

TEST(LlvmLibcPrintfParserTest, IndexModeOneArg) {
Expand Down
3 changes: 3 additions & 0 deletions libc/test/src/stdio/snprintf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ TEST(LlvmLibcSNPrintfTest, CutOff) {
// passing null as the output pointer is allowed as long as buffsz is 0.
written = LIBC_NAMESPACE::snprintf(nullptr, 0, "%s and more", "1234567890");
EXPECT_EQ(written, 19);

written = LIBC_NAMESPACE::snprintf(nullptr, 0, "%*s", INT_MIN, "nothing");
EXPECT_EQ(written, INT_MAX);
}

TEST(LlvmLibcSNPrintfTest, NoCutOff) {
Expand Down
Loading