torture_misc: Do not rely on environment variables

The safest way is to use getpwuid().

Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Reviewed-by: Sahana Prasad <sahana@redhat.com>
(cherry picked from commit e9046fc069)
This commit is contained in:
Andreas Schneider
2025-01-15 13:22:38 +01:00
committed by Jakub Jelen
parent 39aefd479f
commit ec753057a5

View File

@@ -130,27 +130,22 @@ static void torture_path_expand_tilde_win(void **state) {
#else /* _WIN32 */ #else /* _WIN32 */
static void torture_path_expand_tilde_unix(void **state) { static void torture_path_expand_tilde_unix(void **state) {
char h[256]; char h[256] = {0};
char *d; char *d = NULL;
char *user; char *user = NULL;
char *home; char *home = NULL;
struct passwd *pw = NULL;
(void) state; (void) state;
user = getenv("USER"); pw = getpwuid(getuid());
if (user == NULL){ assert_non_null(pw);
user = getenv("LOGNAME");
}
/* in certain CIs there no such variables */
if (!user){
struct passwd *pw = getpwuid(getuid());
if (pw){
user = pw->pw_name;
}
}
home = getenv("HOME"); user = pw->pw_name;
assert_non_null(user);
home = pw->pw_dir;
assert_non_null(home); assert_non_null(home);
snprintf(h, 256 - 1, "%s/.ssh", home); snprintf(h, 256 - 1, "%s/.ssh", home);
d = ssh_path_expand_tilde("~/.ssh"); d = ssh_path_expand_tilde("~/.ssh");