Skip to content

[libc] Fix incorrect printing for alt mode ints #70252

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
Oct 27, 2023
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
16 changes: 14 additions & 2 deletions libc/src/stdio/printf_core/int_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ LIBC_INLINE int convert_int(Writer *writer, const FormatSection &to_conv) {
size_t prefix_len;
char prefix[2];
if ((to_lower(to_conv.conv_name) == 'x') &&
((flags & FormatFlags::ALTERNATE_FORM) != 0)) {
((flags & FormatFlags::ALTERNATE_FORM) != 0) && num != 0) {
prefix_len = 2;
prefix[0] = '0';
prefix[1] = a + ('x' - 'a');
Expand Down Expand Up @@ -158,8 +158,20 @@ LIBC_INLINE int convert_int(Writer *writer, const FormatSection &to_conv) {
prefix_len);
}

// The standard says that alternate form for the o conversion "increases
// the precision, if and only if necessary, to force the first digit of the
// result to be a zero (if the value and precision are both 0, a single 0 is
// printed)"
// This if checks the following conditions:
// 1) is this an o conversion in alternate form?
// 2) does this number has a leading zero?
// 2a) ... because there are additional leading zeroes?
// 2b) ... because it is just "0", unless it will not write any digits.
const bool has_leading_zero =
(zeroes > 0) || ((num == 0) && (digits_written != 0));
if ((to_conv.conv_name == 'o') &&
((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) && zeroes < 1) {
((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) &&
!has_leading_zero) {
zeroes = 1;
--spaces;
}
Expand Down
16 changes: 16 additions & 0 deletions libc/test/src/stdio/sprintf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ TEST(LlvmLibcSPrintfTest, HexConv) {
EXPECT_EQ(written, 5);
ASSERT_STREQ(buff, "0xd3f");

written = LIBC_NAMESPACE::sprintf(buff, "%#x", 0);
EXPECT_EQ(written, 1);
ASSERT_STREQ(buff, "0");

written = LIBC_NAMESPACE::sprintf(buff, "%#X", 0xE40);
EXPECT_EQ(written, 5);
ASSERT_STREQ(buff, "0XE40");
Expand Down Expand Up @@ -359,6 +363,10 @@ TEST(LlvmLibcSPrintfTest, HexConv) {
EXPECT_EQ(written, 9);
ASSERT_STREQ(buff, " 0X009D4");

written = LIBC_NAMESPACE::sprintf(buff, "%#.x", 0);
EXPECT_EQ(written, 0);
ASSERT_STREQ(buff, "");

written = LIBC_NAMESPACE::sprintf(buff, "%-7.5x", 0x260);
EXPECT_EQ(written, 7);
ASSERT_STREQ(buff, "00260 ");
Expand Down Expand Up @@ -516,6 +524,10 @@ TEST(LlvmLibcSPrintfTest, OctConv) {
EXPECT_EQ(written, 4);
ASSERT_STREQ(buff, "0234");

written = LIBC_NAMESPACE::sprintf(buff, "%#o", 0);
EXPECT_EQ(written, 1);
ASSERT_STREQ(buff, "0");

written = LIBC_NAMESPACE::sprintf(buff, "%05o", 0470);
EXPECT_EQ(written, 5);
ASSERT_STREQ(buff, "00470");
Expand All @@ -534,6 +546,10 @@ TEST(LlvmLibcSPrintfTest, OctConv) {
EXPECT_EQ(written, 7);
ASSERT_STREQ(buff, "0703 ");

written = LIBC_NAMESPACE::sprintf(buff, "%#.o", 0);
EXPECT_EQ(written, 1);
ASSERT_STREQ(buff, "0");

written = LIBC_NAMESPACE::sprintf(buff, "%7.5o", 0314);
EXPECT_EQ(written, 7);
ASSERT_STREQ(buff, " 00314");
Expand Down