mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 00:27:43 +00:00
LibCore: get_password() now removes the trailing '\n' read by getline()
This avoids unintentionally adding a newline character at the end of user passwords when they are set using passwd(1). I also fixed these two issues: - The return value of getline() was being saved in an `int` variable instead of in a `ssize_t` variable; I replaced the `int` keyword with `auto` to fix this issue. - Prior to this patch, get_password() could potentially return tcsetattr()'s errno instead of getline()'s errno in case of an error. We now make sure it always returns the right errno in case of an error.
This commit is contained in:
parent
9e5aa6f794
commit
91222a67c8
1 changed files with 12 additions and 4 deletions
|
@ -1,5 +1,6 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Peter Elliott <pelliott@ualberta.ca>
|
||||
* Copyright (c) 2021, Emanuele Torre <torreemanuele6@gmail.com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
|
@ -48,12 +49,19 @@ Result<String, int> get_password(const StringView& prompt)
|
|||
char* password = nullptr;
|
||||
size_t n = 0;
|
||||
|
||||
int ret = getline(&password, &n, stdin);
|
||||
auto line_length = getline(&password, &n, stdin);
|
||||
int saved_errno = errno;
|
||||
|
||||
tcsetattr(STDIN_FILENO, TCSAFLUSH, &original);
|
||||
putchar('\n');
|
||||
if (ret < 0) {
|
||||
return errno;
|
||||
}
|
||||
|
||||
if (line_length < 0)
|
||||
return saved_errno;
|
||||
|
||||
ASSERT(line_length != 0);
|
||||
|
||||
// Remove trailing '\n' read by getline().
|
||||
password[line_length - 1] = '\0';
|
||||
|
||||
String s(password);
|
||||
free(password);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue