From 3b33f7fd5c9b751c8463247c992b9f1faa7760a7 Mon Sep 17 00:00:00 2001
From: Mike Kruskal <mkruskal@google.com>
Date: Wed, 24 Jun 2026 15:49:43 -0700
Subject: [PATCH] Fix handling of pointer printing tests in 32-bit builds

Fixes #2079

diff --git a/absl/log/check_test_impl.inc b/absl/log/check_test_impl.inc
index 47af1dd020d..c78bbd412a7 100644
--- absl/log/check_test_impl.inc
+++ absl/log/check_test_impl.inc
@@ -727,11 +727,9 @@ void AbslStringify(Sink& sink, const PointerIsStringifiable* var) {
 TEST(CHECKDeathTest, TestPointerPrintedAsNumberDespiteAbslStringify) {
   const auto* p = reinterpret_cast<const PointerIsStringifiable*>(0x1234);
 
-  EXPECT_DEATH(
-      ABSL_TEST_CHECK_EQ(p, nullptr),
-      AnyOf(
-          HasSubstr("Check failed: p == nullptr (0000000000001234 vs. (null))"),
-          HasSubstr("Check failed: p == nullptr (0x1234 vs. (null))")));
+  EXPECT_DEATH(ABSL_TEST_CHECK_EQ(p, nullptr),
+               ContainsRegex(
+                   "Check failed: p == nullptr \\(0+x?1234 vs. \\(null\\)\\)"));
 }
 
 // An uncopyable object with operator<<.
diff --git a/absl/strings/CMakeLists.txt b/absl/strings/CMakeLists.txt
index ca07c3e4470..b6f2aa715b1 100644
--- absl/strings/CMakeLists.txt
+++ absl/strings/CMakeLists.txt
@@ -1319,9 +1319,9 @@ absl_cc_test(
     absl::config
     absl::flat_hash_map
     absl::generic_printer_internal
-    absl::log
     absl::status
     absl::statusor
+    absl::str_format
     absl::strings
     GTest::gmock_main
 )
diff --git a/absl/strings/internal/generic_printer_test.cc b/absl/strings/internal/generic_printer_test.cc
index 071bf760514..be6e003b6fd 100644
--- absl/strings/internal/generic_printer_test.cc
+++ absl/strings/internal/generic_printer_test.cc
@@ -15,6 +15,8 @@
 #include "absl/strings/internal/generic_printer.h"
 
 #include <array>
+#include <cinttypes>
+#include <clocale>
 #include <cstdint>
 #include <limits>
 #include <map>
@@ -36,6 +38,7 @@
 #include "absl/status/status.h"
 #include "absl/status/statusor.h"
 #include "absl/strings/str_cat.h"
+#include "absl/strings/str_format.h"
 #include "absl/strings/substitute.h"
 
 namespace generic_logging_test {
@@ -494,23 +497,26 @@
   auto cp = std::make_unique<char*>(memory);
 
   EXPECT_THAT(GenericPrintToString(i),
-              AnyOf(Eq(absl::StrFormat("<%016X pointing to 5>",
-                                       reinterpret_cast<intptr_t>(&*i))),
+              AnyOf(Eq(absl::StrFormat("<%0*" PRIXPTR " pointing to 5>",
+                                       sizeof(void*) * 2,
+                                       reinterpret_cast<uintptr_t>(&*i))),
                     Eq(absl::StrFormat("<%#x pointing to 5>",
-                                       reinterpret_cast<intptr_t>(&*i)))));
+                                       reinterpret_cast<uintptr_t>(&*i)))));
 
   EXPECT_THAT(
       GenericPrintToString(c),
-      AnyOf(HasSubstr(absl::StrFormat("<%016X pointing to 'z'",
-                                      reinterpret_cast<intptr_t>(&*c))),
+      AnyOf(HasSubstr(absl::StrFormat("<%0*" PRIXPTR " pointing to 'z'",
+                                      sizeof(void*) * 2,
+                                      reinterpret_cast<uintptr_t>(&*c))),
             HasSubstr(absl::StrFormat("<%#x pointing to 'z'",
-                                      reinterpret_cast<intptr_t>(&*c)))));
+                                      reinterpret_cast<uintptr_t>(&*c)))));
 
   EXPECT_THAT(GenericPrintToString(cp),
-              AnyOf(Eq(absl::StrFormat("<%016X pointing to abcdefg>",
-                                       reinterpret_cast<intptr_t>(&*cp))),
+              AnyOf(Eq(absl::StrFormat("<%0*" PRIXPTR " pointing to abcdefg>",
+                                       sizeof(void*) * 2,
+                                       reinterpret_cast<uintptr_t>(&*cp))),
                     Eq(absl::StrFormat("<%#x pointing to abcdefg>",
-                                       reinterpret_cast<intptr_t>(&*cp)))));
+                                       reinterpret_cast<uintptr_t>(&*cp)))));
 }
 
 TEST(GenericPrinterTest, SmartPointerToArrayOnlyPrintsAddressAndHelpText) {
@@ -525,18 +531,20 @@
   EXPECT_THAT(
       GenericPrintToString(nonempty),
       AllOf(AnyOf(HasSubstr(absl::StrFormat(
-                      "%016X", reinterpret_cast<intptr_t>(nonempty.get()))),
+                      "%0*" PRIXPTR, sizeof(void*) * 2,
+                      reinterpret_cast<uintptr_t>(nonempty.get()))),
                   HasSubstr(absl::StrFormat(
-                      "%#x", reinterpret_cast<intptr_t>(nonempty.get())))),
+                      "%#x", reinterpret_cast<uintptr_t>(nonempty.get())))),
             HasSubstr("array"), Not(HasSubstr("to 54321")),
             Not(HasSubstr("to 12345"))));
 
   EXPECT_THAT(
       GenericPrintToString(empty),
       AllOf(AnyOf(HasSubstr(absl::StrFormat(
-                      "%016X", reinterpret_cast<intptr_t>(empty.get()))),
+                      "%0*" PRIXPTR, sizeof(void*) * 2,
+                      reinterpret_cast<uintptr_t>(empty.get()))),
                   HasSubstr(absl::StrFormat(
-                      "%#x", reinterpret_cast<intptr_t>(empty.get())))),
+                      "%#x", reinterpret_cast<uintptr_t>(empty.get())))),
             HasSubstr("array")));
 }
 
