Check printf format strings in str_format_convert_test
authorBenjamin Barenblat <bbaren@google.com>
Fri, 27 May 2022 20:58:38 +0000 (21:58 +0100)
committerBenjamin Barenblat <bbaren@debian.org>
Fri, 27 May 2022 20:58:38 +0000 (21:58 +0100)
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.

Gbp-Pq: Name str-format-convert-test-printf.diff

absl/strings/BUILD.bazel
absl/strings/CMakeLists.txt
absl/strings/internal/str_format/convert_test.cc

index 123b5efbd86543880da9ce2e6f6e620aeab34765..10652ff386914021ad1b83d85d13f3e574d6b0f7 100644 (file)
@@ -787,6 +787,7 @@ cc_test(
     deps = [
         ":str_format_internal",
         ":strings",
+        "//absl/base:core_headers",
         "//absl/base:raw_logging_internal",
         "//absl/types:optional",
         "@com_google_googletest//:gtest_main",
index 3b7ae639f533d9f66c1cb0f29d2752b4e57c88bb..1bc0f94921efe13d98b1f06afb94b805ea38d01b 100644 (file)
@@ -492,6 +492,7 @@ absl_cc_test(
   DEPS
     absl::strings
     absl::str_format_internal
+    absl::core_headers
     absl::raw_logging_internal
     absl::int128
     gmock_main
index 926283cfac1ded44cf160b5d7528ab40f0b7c31e..bd419adb4572937d557e1ff4003b711cb56769f8 100644 (file)
@@ -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 @@ void StrAppendV(std::string *dst, const char *format, va_list ap) {
   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 @@ void StrAppend(std::string *out, const char *format, ...) {
   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, AllIntsWithFlags) {
 }
 
 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<T>::type;
-  static const T kMin = std::numeric_limits<remove_volatile_t>::min();
-  static const T kMax = std::numeric_limits<remove_volatile_t>::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<remove_volatile_t> 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<remove_volatile_t>(std::numeric_limits<int>::min());
+  static const T kMax =
+      static_cast<remove_volatile_t>(std::numeric_limits<int>::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<int>(c)),
+              FormatPack(format, absl::MakeSpan(args)));
   }
 }