Skip to content

Commit

Permalink
Handle thread-local exceptions
Browse files Browse the repository at this point in the history
Thread-local exceptions caused the previous implementation to abort, which doesn't perform any cleanup, so it was necessary to handle the exceptions thread-locally and to cause the program to exit (i.e., terminate normally, with cleanup), to enhance the security and safety of the program and to protect against memory leaks.
  • Loading branch information
dr8co committed Nov 22, 2023
1 parent 032d221 commit f505443
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/passwordManager/passwords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,16 @@ encryptDecryptRange(privacy::vector<passwordRecords> &passwords, const privacy::
if (start > end || end > passwords.size())
throw std::range_error("Invalid range.");

// Encrypt/decrypt the password field
for (std::size_t i = start; i < end; ++i) {
std::get<2>(passwords[i]) = encrypt ? encryptStringWithMoreRounds(std::get<2>(passwords[i]), key)
: decryptStringWithMoreRounds(std::string{std::get<2>(passwords[i])}, key);
try {
// Encrypt/decrypt the password field
for (std::size_t i = start; i < end; ++i) {
std::get<2>(passwords[i]) = encrypt ? encryptStringWithMoreRounds(std::get<2>(passwords[i]), key)
: decryptStringWithMoreRounds(std::string{std::get<2>(passwords[i])},
key);
}
} catch (const std::exception &ex) {
printColor(std::format("Error: {}", ex.what()), 'r', true, std::cerr);
std::exit(1);
}
}

Expand All @@ -158,16 +164,21 @@ encryptDecryptRangeAllFields(privacy::vector<passwordRecords> &passwords, const
if (start > end || end > passwords.size())
throw std::range_error("Invalid range.");

// Encrypt/decrypt all fields
for (std::size_t i = start; i < end; ++i) {
std::get<0>(passwords[i]) = encrypt ? encryptString(std::get<0>(passwords[i]), key)
: decryptString(std::string{std::get<0>(passwords[i])}, key);
try {
// Encrypt/decrypt all fields
for (std::size_t i = start; i < end; ++i) {
std::get<0>(passwords[i]) = encrypt ? encryptString(std::get<0>(passwords[i]), key)
: decryptString(std::string{std::get<0>(passwords[i])}, key);

std::get<1>(passwords[i]) = encrypt ? encryptString(std::get<1>(passwords[i]), key)
: decryptString(std::string{std::get<1>(passwords[i])}, key);
std::get<1>(passwords[i]) = encrypt ? encryptString(std::get<1>(passwords[i]), key)
: decryptString(std::string{std::get<1>(passwords[i])}, key);

std::get<2>(passwords[i]) = encrypt ? encryptString(std::get<2>(passwords[i]), key)
: decryptString(std::string{std::get<2>(passwords[i])}, key);
std::get<2>(passwords[i]) = encrypt ? encryptString(std::get<2>(passwords[i]), key)
: decryptString(std::string{std::get<2>(passwords[i])}, key);
}
} catch (const std::exception &ex) {
printColor(std::format("Error: {}", ex.what()), 'r', true, std::cerr);
std::exit(1);
}
}

Expand Down Expand Up @@ -238,7 +249,7 @@ bool savePasswords(privacy::vector<passwordRecords> &passwords, const std::strin
} catch (const std::exception &ex) {
std::cerr << ex.what() << std::endl;

} catch (...) {}
} catch (...) {} // we're not 'swallowing' this, we just don't want to have a specific message for it.

std::cerr << std::format("Failed to open the password file ({}) for writing.\n", filePath);
return false;
Expand Down

0 comments on commit f505443

Please sign in to comment.