Skip to content

Commit 9f1ac22

Browse files
committed
chown: fails when XXXX. or XXXX: is provided (when XXXX is numeric values)
If the arg starts with an id numeric value, the group isn't set but the separator is provided, we should fail with an error Should fix tests/chown/separator.sh
1 parent 418518a commit 9f1ac22

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

src/uu/chown/src/chown.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,19 @@ fn parse_spec(spec: &str, sep: char) -> UResult<(Option<u32>, Option<u32>)> {
246246
} else {
247247
None
248248
};
249+
250+
if user.chars().next().map(char::is_numeric).unwrap_or(false)
251+
&& group.is_empty()
252+
&& spec != user
253+
{
254+
// if the arg starts with an id numeric value, the group isn't set but the separator is provided,
255+
// we should fail with an error
256+
return Err(USimpleError::new(
257+
1,
258+
format!("invalid spec: {}", spec.quote()),
259+
));
260+
}
261+
249262
Ok((uid, gid))
250263
}
251264

tests/by-util/test_chown.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,38 @@ fn test_chown_only_user_id() {
423423
.stderr_contains("failed to change");
424424
}
425425

426+
#[test]
427+
fn test_chown_fail_id() {
428+
// test chown 1111. file.txt
429+
430+
let scene = TestScenario::new(util_name!());
431+
let at = &scene.fixtures;
432+
433+
let result = scene.cmd_keepenv("id").arg("-u").run();
434+
if skipping_test_is_okay(&result, "id: cannot find name for group ID") {
435+
return;
436+
}
437+
let user_id = String::from(result.stdout_str().trim());
438+
assert!(!user_id.is_empty());
439+
440+
let file1 = "test_chown_file1";
441+
at.touch(file1);
442+
443+
scene
444+
.ucmd()
445+
.arg(format!("{}:", user_id))
446+
.arg(file1)
447+
.fails()
448+
.stderr_contains("invalid spec");
449+
450+
scene
451+
.ucmd()
452+
.arg(format!("{}.", user_id))
453+
.arg(file1)
454+
.fails()
455+
.stderr_contains("invalid spec");
456+
}
457+
426458
/// Test for setting the owner to a user ID for a user that does not exist.
427459
///
428460
/// For example:

0 commit comments

Comments
 (0)