Skip to content

Commit 4ff5ace

Browse files
authored
Add size checks before casting to int
Added checks to ensure sizes fit into int before casting.
1 parent 7fc762d commit 4ff5ace

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

include/jwt-cpp/jwt.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,13 +722,23 @@ namespace jwt {
722722
if (key.substr(0, 27) == "-----BEGIN CERTIFICATE-----") {
723723
auto epkey = helper::extract_pubkey_from_cert<error_category>(key, password, ec);
724724
if (ec) return {};
725-
const int len = static_cast<int>(epkey.size());
725+
// Ensure the size fits into an int before casting
726+
if (epkey.size() > static_cast<std::size_t>(std::numeric_limits<int>::max())) {
727+
ec = error_category::load_key_bio_write; // Add an appropriate error here
728+
return {};
729+
}
730+
int len = static_cast<int>(epkey.size());
726731
if (BIO_write(pubkey_bio.get(), epkey.data(), len) != len) {
727732
ec = error_category::load_key_bio_write;
728733
return {};
729734
}
730735
} else {
731-
const int len = static_cast<int>(key.size());
736+
// Ensure the size fits into an int before casting
737+
if (key.size() > static_cast<std::size_t>(std::numeric_limits<int>::max())) {
738+
ec = error_category::load_key_bio_write; // Add an appropriate error here
739+
return {};
740+
}
741+
int len = static_cast<int>(key.size());
732742
if (BIO_write(pubkey_bio.get(), key.data(), len) != len) {
733743
ec = error_category::load_key_bio_write;
734744
return {};

0 commit comments

Comments
 (0)