LibCore: Make get_salt() fallible so we get an error instead of crashing

This commit is contained in:
Ninad Sachania
2024-11-28 12:34:37 +00:00
committed by Nico Weber
parent 61ebfdab56
commit 2f3ad5ca28
3 changed files with 7 additions and 7 deletions

View File

@@ -27,7 +27,7 @@
namespace Core {
static ByteString get_salt()
static ErrorOr<ByteString> get_salt()
{
char random_data[12];
fill_with_random({ random_data, sizeof(random_data) });
@@ -35,8 +35,7 @@ static ByteString get_salt()
StringBuilder builder;
builder.append("$5$"sv);
// FIXME: change to TRY() and make method fallible
auto salt_string = MUST(encode_base64({ random_data, sizeof(random_data) }));
auto salt_string = TRY(encode_base64({ random_data, sizeof(random_data) }));
builder.append(salt_string);
return builder.to_byte_string();
@@ -184,9 +183,10 @@ ErrorOr<void> Account::login() const
return {};
}
void Account::set_password(SecretString const& password)
ErrorOr<void> Account::set_password(SecretString const& password)
{
m_password_hash = crypt(password.characters(), get_salt().characters());
m_password_hash = crypt(password.characters(), TRY(get_salt()).characters());
return {};
}
void Account::set_password_enabled(bool enabled)

View File

@@ -45,7 +45,7 @@ public:
// Setters only affect in-memory copy of password.
// You must call sync to apply changes.
void set_password(SecretString const& password);
ErrorOr<void> set_password(SecretString const& password);
void set_password_enabled(bool enabled);
void set_home_directory(StringView home_directory) { m_home_directory = home_directory; }
void set_uid(uid_t uid) { m_uid = uid; }

View File

@@ -87,7 +87,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return 1;
}
target_account.set_password(new_password);
TRY(target_account.set_password(new_password));
}
TRY(Core::System::pledge("stdio wpath rpath cpath fattr"));