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>
main-with-bazel
David Benjamin 2 years ago committed by Boringssl LUCI CQ
parent 89a6253757
commit 7e56051791
  1. 9
      crypto/kyber/kyber.c
  2. 11
      crypto/x509/x509_time_test.cc
  3. 8
      ssl/test/async_bio.cc

@ -132,7 +132,7 @@ static uint16_t reduce_once(uint16_t x) {
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 @@ static int vector_decode(vector *out, const uint8_t *in, int bits) {
// 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

@ -296,14 +296,11 @@ TEST(X509TimeTest, TestCmpTime) {
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));
}
}

@ -59,8 +59,8 @@ static int AsyncWrite(BIO *bio, const char *in, int inl) {
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 @@ static int AsyncRead(BIO *bio, char *out, int outl) {
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) {

Loading…
Cancel
Save