From 751ade00ee347abef5dac7248db851e3f2012e14 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 4 Aug 2022 05:19:56 -0700 Subject: Fix "unsafe narrowing" warnings in absl, 3/n. Addresses failures with the following, in some files: -Wshorten-64-to-32 -Wimplicit-int-conversion -Wsign-compare -Wsign-conversion -Wtautological-unsigned-zero-compare (This specific CL focuses on .cc files in dirs n-t, except string.) Bug: chromium:1292951 PiperOrigin-RevId: 465287204 Change-Id: I0fe98ff78bf3c08d86992019eb626755f8b6803e --- absl/numeric/int128.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'absl/numeric') diff --git a/absl/numeric/int128.cc b/absl/numeric/int128.cc index 8cdcbf05..e5526c6f 100644 --- a/absl/numeric/int128.cc +++ b/absl/numeric/int128.cc @@ -209,15 +209,16 @@ std::ostream& operator<<(std::ostream& os, uint128 v) { // Add the requisite padding. std::streamsize width = os.width(0); if (static_cast(width) > rep.size()) { + const size_t count = static_cast(width) - rep.size(); std::ios::fmtflags adjustfield = flags & std::ios::adjustfield; if (adjustfield == std::ios::left) { - rep.append(width - rep.size(), os.fill()); + rep.append(count, os.fill()); } else if (adjustfield == std::ios::internal && (flags & std::ios::showbase) && (flags & std::ios::basefield) == std::ios::hex && v != 0) { - rep.insert(2, width - rep.size(), os.fill()); + rep.insert(2, count, os.fill()); } else { - rep.insert(0, width - rep.size(), os.fill()); + rep.insert(0, count, os.fill()); } } @@ -306,22 +307,23 @@ std::ostream& operator<<(std::ostream& os, int128 v) { // Add the requisite padding. std::streamsize width = os.width(0); if (static_cast(width) > rep.size()) { + const size_t count = static_cast(width) - rep.size(); switch (flags & std::ios::adjustfield) { case std::ios::left: - rep.append(width - rep.size(), os.fill()); + rep.append(count, os.fill()); break; case std::ios::internal: if (print_as_decimal && (rep[0] == '+' || rep[0] == '-')) { - rep.insert(1, width - rep.size(), os.fill()); + rep.insert(1, count, os.fill()); } else if ((flags & std::ios::basefield) == std::ios::hex && (flags & std::ios::showbase) && v != 0) { - rep.insert(2, width - rep.size(), os.fill()); + rep.insert(2, count, os.fill()); } else { - rep.insert(0, width - rep.size(), os.fill()); + rep.insert(0, count, os.fill()); } break; default: // std::ios::right - rep.insert(0, width - rep.size(), os.fill()); + rep.insert(0, count, os.fill()); break; } } -- cgit v1.2.3