From: Benjamin Barenblat Subject: Check printf format strings in str_format_convert_test Forwarded: yes Applied-Upstream: https://github.com/abseil/abseil-cpp/commit/9fed77a6fea29b8c8468bd41c6259c7f67163a65 Add ABSL_PRINTF_ATTRIBUTE to appropriate functions in strings/internal/str_format/convert_test. Correct TypedFormatConvertTest.Char, which was accidentally passing values of types larger than int to StrPrint. The author works at Google. Upstream applied this patch as Piper revision 439388148 and exported it to GitHub; the Applied-Upstream URL above points to the exported commit. --- a/absl/strings/BUILD.bazel +++ b/absl/strings/BUILD.bazel @@ -787,6 +787,7 @@ deps = [ ":str_format_internal", ":strings", + "//absl/base:core_headers", "//absl/base:raw_logging_internal", "//absl/types:optional", "@com_google_googletest//:gtest_main", --- a/absl/strings/CMakeLists.txt +++ b/absl/strings/CMakeLists.txt @@ -492,6 +492,7 @@ DEPS absl::strings absl::str_format_internal + absl::core_headers absl::raw_logging_internal absl::int128 gmock_main --- a/absl/strings/internal/str_format/convert_test.cc +++ b/absl/strings/internal/str_format/convert_test.cc @@ -24,6 +24,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/base/attributes.h" #include "absl/base/internal/raw_logging.h" #include "absl/strings/internal/str_format/bind.h" #include "absl/strings/match.h" @@ -124,6 +125,7 @@ delete[] buf; } +void StrAppend(std::string *, const char *, ...) ABSL_PRINTF_ATTRIBUTE(2, 3); void StrAppend(std::string *out, const char *format, ...) { va_list ap; va_start(ap, format); @@ -131,6 +133,7 @@ va_end(ap); } +std::string StrPrint(const char *, ...) ABSL_PRINTF_ATTRIBUTE(1, 2); std::string StrPrint(const char *format, ...) { va_list ap; va_start(ap, format); @@ -452,21 +455,32 @@ } TYPED_TEST_P(TypedFormatConvertTest, Char) { + // Pass a bunch of values of type TypeParam to both FormatPack and libc's + // vsnprintf("%c", ...) (wrapped in StrPrint) to make sure we get the same + // value. typedef TypeParam T; using remove_volatile_t = typename std::remove_volatile::type; - static const T kMin = std::numeric_limits::min(); - static const T kMax = std::numeric_limits::max(); - T kVals[] = { - remove_volatile_t(1), remove_volatile_t(2), remove_volatile_t(10), - remove_volatile_t(-1), remove_volatile_t(-2), remove_volatile_t(-10), - remove_volatile_t(0), - kMin + remove_volatile_t(1), kMin, - kMax - remove_volatile_t(1), kMax + std::vector vals = { + remove_volatile_t(1), remove_volatile_t(2), remove_volatile_t(10), // + remove_volatile_t(-1), remove_volatile_t(-2), remove_volatile_t(-10), // + remove_volatile_t(0), }; - for (const T &c : kVals) { + + // We'd like to test values near std::numeric_limits::min() and + // std::numeric_limits::max(), too, but vsnprintf("%c", ...) can't handle + // anything larger than an int. Add in the most extreme values we can without + // exceeding that range. + static const T kMin = + static_cast(std::numeric_limits::min()); + static const T kMax = + static_cast(std::numeric_limits::max()); + vals.insert(vals.end(), {kMin + 1, kMin, kMax - 1, kMax}); + + for (const T c : vals) { const FormatArgImpl args[] = {FormatArgImpl(c)}; UntypedFormatSpecImpl format("%c"); - EXPECT_EQ(StrPrint("%c", c), FormatPack(format, absl::MakeSpan(args))); + EXPECT_EQ(StrPrint("%c", static_cast(c)), + FormatPack(format, absl::MakeSpan(args))); } }