summaryrefslogtreecommitdiff
path: root/debian/patches/str-format-convert-test-printf.diff
blob: 1808f2e1a226993bf876a231add369ff2d195cc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
From: Benjamin Barenblat <bbaren@google.com>
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<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)));
   }
 }