Skip to content

Commit 934f613

Browse files
Abseil Teamcopybara-github
Abseil Team
authored andcommitted
Fix "unsafe narrowing" warnings in absl, 4/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 strings/, except /internal/.) Bug: chromium:1292951 PiperOrigin-RevId: 468205572 Change-Id: Ifce3f1a7a4b2b2c359bf7700a11279bebfef8a15
1 parent 5478021 commit 934f613

File tree

9 files changed

+156
-123
lines changed

9 files changed

+156
-123
lines changed

Diff for: absl/strings/ascii.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ ABSL_DLL const char kToUpper[256] = {
157157

158158
void AsciiStrToLower(std::string* s) {
159159
for (auto& ch : *s) {
160-
ch = absl::ascii_tolower(ch);
160+
ch = absl::ascii_tolower(static_cast<unsigned char>(ch));
161161
}
162162
}
163163

164164
void AsciiStrToUpper(std::string* s) {
165165
for (auto& ch : *s) {
166-
ch = absl::ascii_toupper(ch);
166+
ch = absl::ascii_toupper(static_cast<unsigned char>(ch));
167167
}
168168
}
169169

@@ -183,17 +183,17 @@ void RemoveExtraAsciiWhitespace(std::string* str) {
183183
for (; input_it < input_end; ++input_it) {
184184
if (is_ws) {
185185
// Consecutive whitespace? Keep only the last.
186-
is_ws = absl::ascii_isspace(*input_it);
186+
is_ws = absl::ascii_isspace(static_cast<unsigned char>(*input_it));
187187
if (is_ws) --output_it;
188188
} else {
189-
is_ws = absl::ascii_isspace(*input_it);
189+
is_ws = absl::ascii_isspace(static_cast<unsigned char>(*input_it));
190190
}
191191

192192
*output_it = *input_it;
193193
++output_it;
194194
}
195195

196-
str->erase(output_it - &(*str)[0]);
196+
str->erase(static_cast<size_t>(output_it - &(*str)[0]));
197197
}
198198

199199
ABSL_NAMESPACE_END

Diff for: absl/strings/charconv.cc

+21-10
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ struct FloatTraits;
6565

6666
template <>
6767
struct FloatTraits<double> {
68+
using mantissa_t = uint64_t;
69+
6870
// The number of mantissa bits in the given float type. This includes the
6971
// implied high bit.
7072
static constexpr int kTargetMantissaBits = 53;
@@ -103,7 +105,7 @@ struct FloatTraits<double> {
103105
// a normal value is made, or it must be less narrow than that, in which case
104106
// `exponent` must be exactly kMinNormalExponent, and a subnormal value is
105107
// made.
106-
static double Make(uint64_t mantissa, int exponent, bool sign) {
108+
static double Make(mantissa_t mantissa, int exponent, bool sign) {
107109
#ifndef ABSL_BIT_PACK_FLOATS
108110
// Support ldexp no matter which namespace it's in. Some platforms
109111
// incorrectly don't put it in namespace std.
@@ -116,8 +118,10 @@ struct FloatTraits<double> {
116118
if (mantissa > kMantissaMask) {
117119
// Normal value.
118120
// Adjust by 1023 for the exponent representation bias, and an additional
119-
// 52 due to the implied decimal point in the IEEE mantissa represenation.
120-
dbl += uint64_t{exponent + 1023u + kTargetMantissaBits - 1} << 52;
121+
// 52 due to the implied decimal point in the IEEE mantissa
122+
// representation.
123+
dbl += static_cast<uint64_t>(exponent + 1023 + kTargetMantissaBits - 1)
124+
<< 52;
121125
mantissa &= kMantissaMask;
122126
} else {
123127
// subnormal value
@@ -134,16 +138,20 @@ struct FloatTraits<double> {
134138
// members and methods.
135139
template <>
136140
struct FloatTraits<float> {
141+
using mantissa_t = uint32_t;
142+
137143
static constexpr int kTargetMantissaBits = 24;
138144
static constexpr int kMaxExponent = 104;
139145
static constexpr int kMinNormalExponent = -149;
146+
140147
static float MakeNan(const char* tagp) {
141148
// Support nanf no matter which namespace it's in. Some platforms
142149
// incorrectly don't put it in namespace std.
143150
using namespace std; // NOLINT
144151
return nanf(tagp);
145152
}
146-
static float Make(uint32_t mantissa, int exponent, bool sign) {
153+
154+
static float Make(mantissa_t mantissa, int exponent, bool sign) {
147155
#ifndef ABSL_BIT_PACK_FLOATS
148156
// Support ldexpf no matter which namespace it's in. Some platforms
149157
// incorrectly don't put it in namespace std.
@@ -157,7 +165,8 @@ struct FloatTraits<float> {
157165
// Normal value.
158166
// Adjust by 127 for the exponent representation bias, and an additional
159167
// 23 due to the implied decimal point in the IEEE mantissa represenation.
160-
flt += uint32_t{exponent + 127u + kTargetMantissaBits - 1} << 23;
168+
flt += static_cast<uint32_t>(exponent + 127 + kTargetMantissaBits - 1)
169+
<< 23;
161170
mantissa &= kMantissaMask;
162171
} else {
163172
// subnormal value
@@ -242,9 +251,9 @@ struct CalculatedFloat {
242251

243252
// Returns the bit width of the given uint128. (Equivalently, returns 128
244253
// minus the number of leading zero bits.)
245-
unsigned BitWidth(uint128 value) {
254+
int BitWidth(uint128 value) {
246255
if (Uint128High64(value) == 0) {
247-
return static_cast<unsigned>(bit_width(Uint128Low64(value)));
256+
return bit_width(Uint128Low64(value));
248257
}
249258
return 128 - countl_zero(Uint128High64(value));
250259
}
@@ -337,8 +346,10 @@ void EncodeResult(const CalculatedFloat& calculated, bool negative,
337346
*value = negative ? -0.0 : 0.0;
338347
return;
339348
}
340-
*value = FloatTraits<FloatType>::Make(calculated.mantissa,
341-
calculated.exponent, negative);
349+
*value = FloatTraits<FloatType>::Make(
350+
static_cast<typename FloatTraits<FloatType>::mantissa_t>(
351+
calculated.mantissa),
352+
calculated.exponent, negative);
342353
}
343354

344355
// Returns the given uint128 shifted to the right by `shift` bits, and rounds
@@ -519,7 +530,7 @@ CalculatedFloat CalculateFromParsedHexadecimal(
519530
const strings_internal::ParsedFloat& parsed_hex) {
520531
uint64_t mantissa = parsed_hex.mantissa;
521532
int exponent = parsed_hex.exponent;
522-
auto mantissa_width = static_cast<unsigned>(bit_width(mantissa));
533+
int mantissa_width = bit_width(mantissa);
523534
const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent);
524535
bool result_exact;
525536
exponent += shift;

Diff for: absl/strings/cord.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstdio>
2121
#include <cstdlib>
2222
#include <iomanip>
23+
#include <ios>
2324
#include <iostream>
2425
#include <limits>
2526
#include <ostream>
@@ -184,7 +185,7 @@ inline void Cord::InlineRep::reduce_size(size_t n) {
184185
assert(tag >= n);
185186
tag -= n;
186187
memset(data_.as_chars() + tag, 0, n);
187-
set_inline_size(static_cast<char>(tag));
188+
set_inline_size(tag);
188189
}
189190

190191
inline void Cord::InlineRep::remove_prefix(size_t n) {
@@ -1098,7 +1099,7 @@ Cord Cord::ChunkIterator::AdvanceAndReadBytes(size_t n) {
10981099
: current_leaf_;
10991100
const char* data = payload->IsExternal() ? payload->external()->base
11001101
: payload->flat()->Data();
1101-
const size_t offset = current_chunk_.data() - data;
1102+
const size_t offset = static_cast<size_t>(current_chunk_.data() - data);
11021103

11031104
auto* tree = CordRepSubstring::Substring(payload, offset, n);
11041105
subcord.contents_.EmplaceTree(VerifyTree(tree), method);
@@ -1308,7 +1309,7 @@ static bool VerifyNode(CordRep* root, CordRep* start_node,
13081309

13091310
std::ostream& operator<<(std::ostream& out, const Cord& cord) {
13101311
for (absl::string_view chunk : cord.Chunks()) {
1311-
out.write(chunk.data(), chunk.size());
1312+
out.write(chunk.data(), static_cast<std::streamsize>(chunk.size()));
13121313
}
13131314
return out;
13141315
}

Diff for: absl/strings/cord_buffer.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -411,8 +411,12 @@ class CordBuffer {
411411

412412
// Power2 functions
413413
static bool IsPow2(size_t size) { return absl::has_single_bit(size); }
414-
static size_t Log2Floor(size_t size) { return absl::bit_width(size) - 1; }
415-
static size_t Log2Ceil(size_t size) { return absl::bit_width(size - 1); }
414+
static size_t Log2Floor(size_t size) {
415+
return static_cast<size_t>(absl::bit_width(size) - 1);
416+
}
417+
static size_t Log2Ceil(size_t size) {
418+
return static_cast<size_t>(absl::bit_width(size - 1));
419+
}
416420

417421
// Implementation of `CreateWithCustomLimit()`.
418422
// This implementation allows for future memory allocation hints to

0 commit comments

Comments
 (0)