1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

id: Handle NULL pointer gracefully within cstr2cow macro (#7810)

* id: Handle NULL pointer gracefully within `cstr2cow` macro

> getlogin() returns a pointer to a string containing the name of the user logged in on the controlling terminal of the process, or a NULL pointer if this information cannot be determined.

Ref: https://linux.die.net/man/3/getlogin

* id: Remove redundant std::ffi:: prefix from CStr

* id: Add comment for the null check

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>

* id: Remove skip for test that should not segfault anymore

Segfault fixed by 292fb9242342d47412a789f4d0002b811568bd41

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
Teemu Pätsi 2025-04-23 09:26:00 +03:00 committed by GitHub
parent a4230410c8
commit 6ac444a9c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 15 deletions

View file

@ -47,7 +47,15 @@ use uucore::{format_usage, help_about, help_section, help_usage, show_error};
macro_rules! cstr2cow {
($v:expr) => {
unsafe { CStr::from_ptr($v).to_string_lossy() }
unsafe {
let ptr = $v;
// Must be not null to call cstr2cow
if ptr.is_null() {
None
} else {
Some({ CStr::from_ptr(ptr) }.to_string_lossy())
}
}
};
}
@ -451,8 +459,8 @@ fn pretty(possible_pw: Option<Passwd>) {
let login = cstr2cow!(getlogin().cast_const());
let rid = getuid();
if let Ok(p) = Passwd::locate(rid) {
if login == p.name {
println!("login\t{login}");
if let Some(user_name) = login {
println!("login\t{user_name}");
}
println!("uid\t{}", p.name);
} else {

View file

@ -152,19 +152,8 @@ fn test_id_real() {
fn test_id_pretty_print() {
// `-p` is BSD only and not supported on GNU's `id`
let username = whoami();
let result = new_ucmd!().arg("-p").run();
if result.stdout_str().trim().is_empty() {
// this fails only on: "MinRustV (ubuntu-latest, feat_os_unix)"
// `rustc 1.40.0 (73528e339 2019-12-16)`
// run: /home/runner/work/coreutils/coreutils/target/debug/coreutils id -p
// thread 'test_id::test_id_pretty_print' panicked at 'Command was expected to succeed.
// stdout =
// stderr = ', tests/common/util.rs:157:13
println!("test skipped:");
} else {
result.success().stdout_contains(username);
}
result.success().stdout_contains(username);
}
#[test]