Miscellaneous size_t truncation fixes
Bug: 516
Change-Id: I3cc7e85687a29201a325b498eecf3694e0429ebc
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60067
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/kyber/kyber.c b/crypto/kyber/kyber.c
index 776c085..98c70e6 100644
--- a/crypto/kyber/kyber.c
+++ b/crypto/kyber/kyber.c
@@ -132,7 +132,7 @@
static uint16_t reduce(uint32_t x) {
assert(x < kPrime + 2u * kPrime * kPrime);
uint64_t product = (uint64_t)x * kBarrettMultiplier;
- uint32_t quotient = product >> kBarrettShift;
+ uint32_t quotient = (uint32_t)(product >> kBarrettShift);
uint32_t remainder = x - quotient * kPrime;
return reduce_once(remainder);
}
@@ -491,9 +491,10 @@
// remainder (for rounding) and the quotient (as the result), we cannot use
// |reduce| here, but need to do the Barrett reduction directly.
static uint16_t compress(uint16_t x, int bits) {
- uint32_t product = (uint32_t)x << bits;
- uint32_t quotient = ((uint64_t)product * kBarrettMultiplier) >> kBarrettShift;
- uint32_t remainder = product - quotient * kPrime;
+ uint32_t shifted = (uint32_t)x << bits;
+ uint64_t product = (uint64_t)shifted * kBarrettMultiplier;
+ uint32_t quotient = (uint32_t)(product >> kBarrettShift);
+ uint32_t remainder = shifted - quotient * kPrime;
// Adjust the quotient to round correctly:
// 0 <= remainder <= kHalfPrime round to 0
diff --git a/crypto/x509/x509_time_test.cc b/crypto/x509/x509_time_test.cc
index c0327d2..7abb10d 100644
--- a/crypto/x509/x509_time_test.cc
+++ b/crypto/x509/x509_time_test.cc
@@ -296,14 +296,11 @@
for (auto &test : kX509CmpTests) {
SCOPED_TRACE(test.data);
- ASN1_TIME t;
+ bssl::UniquePtr<ASN1_STRING> t(ASN1_STRING_type_new(test.type));
+ ASSERT_TRUE(t);
+ ASSERT_TRUE(ASN1_STRING_set(t.get(), test.data, strlen(test.data)));
- memset(&t, 0, sizeof(t));
- t.type = test.type;
- t.data = (unsigned char*) test.data;
- t.length = strlen(test.data);
-
- EXPECT_EQ(test.expected, X509_cmp_time_posix(&t, test.cmp_time));
+ EXPECT_EQ(test.expected, X509_cmp_time_posix(t.get(), test.cmp_time));
}
}
diff --git a/ssl/test/async_bio.cc b/ssl/test/async_bio.cc
index 89d66d7..9eae290 100644
--- a/ssl/test/async_bio.cc
+++ b/ssl/test/async_bio.cc
@@ -59,8 +59,8 @@
return -1;
}
- if (!a->datagram && (size_t)inl > a->write_quota) {
- inl = a->write_quota;
+ if (!a->datagram && static_cast<size_t>(inl) > a->write_quota) {
+ inl = static_cast<int>(a->write_quota);
}
int ret = BIO_write(bio->next_bio, in, inl);
if (ret <= 0) {
@@ -85,8 +85,8 @@
return -1;
}
- if (!a->datagram && (size_t)outl > a->read_quota) {
- outl = a->read_quota;
+ if (!a->datagram && static_cast<size_t>(outl) > a->read_quota) {
+ outl = static_cast<int>(a->read_quota);
}
int ret = BIO_read(bio->next_bio, out, outl);
if (ret <= 0) {