Skip to content
This repository was archived by the owner on Apr 3, 2020. It is now read-only.

Commit 0755163

Browse files
pkastingCommit bot
authored and
Commit bot
committed
Type conversion fixes, crypto/ edition.
This is mostly to fix MSVC warnings about possible value truncation. BUG=81439 TEST=none Review URL: https://codereview.chromium.org/659943004 Cr-Commit-Position: refs/heads/master@{#300432}
1 parent 084a522 commit 0755163

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

crypto/ec_private_key_nss.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ ECPrivateKey* ECPrivateKey::CreateWithParams(PK11SlotInfo* slot,
340340
};
341341

342342
ec_parameters.data[0] = SEC_ASN1_OBJECT_ID;
343-
ec_parameters.data[1] = oid_data->oid.len;
343+
ec_parameters.data[1] = static_cast<unsigned char>(oid_data->oid.len);
344344
memcpy(ec_parameters.data + 2, oid_data->oid.data, oid_data->oid.len);
345345

346346
result->key_ = PK11_GenerateKeyPair(slot,

crypto/nss_util_unittest.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ TEST(NSSUtilTest, PRTimeConversion) {
1919
prxtime.tm_params.tp_gmt_offset = 0;
2020
prxtime.tm_params.tp_dst_offset = 0;
2121
base::Time::Exploded exploded;
22-
prxtime.tm_year = exploded.year = 2011;
22+
exploded.year = prxtime.tm_year = 2011;
2323
exploded.month = 12;
2424
prxtime.tm_month = 11;
25-
prxtime.tm_wday = exploded.day_of_week = 0; // Should be unusued.
26-
prxtime.tm_mday = exploded.day_of_month = 10;
27-
prxtime.tm_hour = exploded.hour = 2;
28-
prxtime.tm_min = exploded.minute = 52;
29-
prxtime.tm_sec = exploded.second = 19;
25+
// PRExplodedTime::tm_wday is a smaller type than Exploded::day_of_week, so
26+
// assigning the two in this order instead of the reverse avoids potential
27+
// warnings about type downcasting.
28+
exploded.day_of_week = prxtime.tm_wday = 0; // Should be unused.
29+
exploded.day_of_month = prxtime.tm_mday = 10;
30+
exploded.hour = prxtime.tm_hour = 2;
31+
exploded.minute = prxtime.tm_min = 52;
32+
exploded.second = prxtime.tm_sec = 19;
3033
exploded.millisecond = 342;
3134
prxtime.tm_usec = 342000;
3235

crypto/signature_verifier_unittest.cc

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// found in the LICENSE file.
44

55
#include "crypto/signature_verifier.h"
6+
7+
#include "base/numerics/safe_conversions.h"
68
#include "testing/gtest/include/gtest/gtest.h"
79

810
TEST(SignatureVerifierTest, BasicTest) {
@@ -1005,36 +1007,36 @@ static bool EncodeRSAPublicKey(const std::vector<uint8>& modulus_n,
10051007
public_key_info->insert(public_key_info->begin(),
10061008
public_exponent_e.begin(),
10071009
public_exponent_e.end());
1008-
uint8 length = public_exponent_e.size();
1009-
public_key_info->insert(public_key_info->begin(), length);
1010+
uint8 exponent_size = base::checked_cast<uint8>(public_exponent_e.size());
1011+
public_key_info->insert(public_key_info->begin(), exponent_size);
10101012
public_key_info->insert(public_key_info->begin(), kIntegerTag);
10111013

10121014
// Encode the modulus n as an INTEGER.
10131015
public_key_info->insert(public_key_info->begin(),
10141016
modulus_n.begin(), modulus_n.end());
1015-
uint16 length16 = modulus_n.size();
1017+
uint16 modulus_size = base::checked_cast<uint16>(modulus_n.size());
10161018
if (modulus_n[0] & 0x80) {
10171019
public_key_info->insert(public_key_info->begin(), 0x00);
1018-
length16++;
1020+
modulus_size++;
10191021
}
1020-
public_key_info->insert(public_key_info->begin(), length16 & 0xff);
1021-
public_key_info->insert(public_key_info->begin(), (length16 >> 8) & 0xff);
1022+
public_key_info->insert(public_key_info->begin(), modulus_size & 0xff);
1023+
public_key_info->insert(public_key_info->begin(), (modulus_size >> 8) & 0xff);
10221024
public_key_info->insert(public_key_info->begin(), 0x82);
10231025
public_key_info->insert(public_key_info->begin(), kIntegerTag);
10241026

10251027
// Encode the RSAPublicKey SEQUENCE.
1026-
length16 = public_key_info->size();
1027-
public_key_info->insert(public_key_info->begin(), length16 & 0xff);
1028-
public_key_info->insert(public_key_info->begin(), (length16 >> 8) & 0xff);
1028+
uint16 info_size = base::checked_cast<uint16>(public_key_info->size());
1029+
public_key_info->insert(public_key_info->begin(), info_size & 0xff);
1030+
public_key_info->insert(public_key_info->begin(), (info_size >> 8) & 0xff);
10291031
public_key_info->insert(public_key_info->begin(), 0x82);
10301032
public_key_info->insert(public_key_info->begin(), kSequenceTag);
10311033

10321034
// Encode the BIT STRING.
10331035
// Number of unused bits.
10341036
public_key_info->insert(public_key_info->begin(), 0x00);
1035-
length16 = public_key_info->size();
1036-
public_key_info->insert(public_key_info->begin(), length16 & 0xff);
1037-
public_key_info->insert(public_key_info->begin(), (length16 >> 8) & 0xff);
1037+
info_size = base::checked_cast<uint16>(public_key_info->size());
1038+
public_key_info->insert(public_key_info->begin(), info_size & 0xff);
1039+
public_key_info->insert(public_key_info->begin(), (info_size >> 8) & 0xff);
10381040
public_key_info->insert(public_key_info->begin(), 0x82);
10391041
public_key_info->insert(public_key_info->begin(), kBitStringTag);
10401042

@@ -1049,9 +1051,9 @@ static bool EncodeRSAPublicKey(const std::vector<uint8>& modulus_n,
10491051
algorithm, algorithm + sizeof(algorithm));
10501052

10511053
// Encode the outermost SEQUENCE.
1052-
length16 = public_key_info->size();
1053-
public_key_info->insert(public_key_info->begin(), length16 & 0xff);
1054-
public_key_info->insert(public_key_info->begin(), (length16 >> 8) & 0xff);
1054+
info_size = base::checked_cast<uint16>(public_key_info->size());
1055+
public_key_info->insert(public_key_info->begin(), info_size & 0xff);
1056+
public_key_info->insert(public_key_info->begin(), (info_size >> 8) & 0xff);
10551057
public_key_info->insert(public_key_info->begin(), 0x82);
10561058
public_key_info->insert(public_key_info->begin(), kSequenceTag);
10571059

0 commit comments

Comments
 (0)