Skip to content

Commit

Permalink
Fixes bug so that %v with modifiers doesn't compile. %v is not in…
Browse files Browse the repository at this point in the history
…tended to work with modifiers because the meaning of modifiers is type-dependent and `%v` is intended to be used in situations where the type is not important. Please continue using if `%s` if you require format modifiers.

PiperOrigin-RevId: 472534916
Change-Id: I5838761b2b40cbc4344077f23d44b1e634e5bae3
  • Loading branch information
Abseil Team authored and copybara-github committed Sep 6, 2022
1 parent 60499cf commit d3c00b0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 4 additions & 0 deletions absl/strings/internal/str_format/checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ class ConvParser {
constexpr ConvParser ParseConversion() const {
char first_char = GetChar(format_, 0);

if (first_char == 'v' && *(format_.data() - 1) != '%') {
return SetError(true);
}

if (is_positional_) {
return VerifyPositional({ConsumeFront(format_), arg_position_},
first_char);
Expand Down
2 changes: 0 additions & 2 deletions absl/strings/str_format_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,6 @@ TEST_F(FormatEntryPointTest, FormatStreamed) {

TEST_F(FormatEntryPointTest, FormatStreamedWithV) {
EXPECT_EQ("123", StrFormat("%v", FormatStreamed(123)));
EXPECT_EQ(" 123", StrFormat("%5v", FormatStreamed(123)));
EXPECT_EQ("123 ", StrFormat("%-5v", FormatStreamed(123)));
EXPECT_EQ("X", StrFormat("%v", FormatStreamed(streamed_test::X())));
EXPECT_EQ("123", StrFormat("%v", FormatStreamed(StreamFormat("%d", 123))));
}
Expand Down

0 comments on commit d3c00b0

Please sign in to comment.