Skip to content

Commit deb0247

Browse files
committed
printenv skips environment variables with invalid UTF-8
closes: uutils#9701
1 parent 45f81bb commit deb0247

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

src/uu/printenv/src/printenv.rs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66
use clap::{Arg, ArgAction, Command};
77
use std::env;
8+
use std::io::{self, Write};
89
use uucore::translate;
910
use uucore::{error::UResult, format_usage};
1011

12+
#[cfg(unix)]
13+
use std::os::unix::ffi::OsStrExt;
14+
1115
static OPT_NULL: &str = "null";
1216

1317
static ARG_VARIABLES: &str = "variables";
@@ -21,15 +25,33 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
2125
.map(|v| v.map(ToString::to_string).collect())
2226
.unwrap_or_default();
2327

24-
let separator = if matches.get_flag(OPT_NULL) {
25-
"\x00"
28+
let separator: &[u8] = if matches.get_flag(OPT_NULL) {
29+
b"\x00"
2630
} else {
27-
"\n"
31+
b"\n"
2832
};
2933

34+
let mut stdout = io::stdout().lock();
35+
3036
if variables.is_empty() {
31-
for (env_var, value) in env::vars() {
32-
print!("{env_var}={value}{separator}");
37+
for (env_var, value) in env::vars_os() {
38+
#[cfg(unix)]
39+
{
40+
stdout.write_all(env_var.as_bytes())?;
41+
stdout.write_all(b"=")?;
42+
stdout.write_all(value.as_bytes())?;
43+
stdout.write_all(separator)?;
44+
}
45+
#[cfg(not(unix))]
46+
{
47+
// On non-Unix, use lossy conversion as OsStrExt is not available
48+
print!(
49+
"{}={}{}",
50+
env_var.to_string_lossy(),
51+
value.to_string_lossy(),
52+
String::from_utf8_lossy(separator)
53+
);
54+
}
3355
}
3456
return Ok(());
3557
}
@@ -41,8 +63,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
4163
error_found = true;
4264
continue;
4365
}
44-
if let Ok(var) = env::var(env_var) {
45-
print!("{var}{separator}");
66+
if let Some(var) = env::var_os(&env_var) {
67+
#[cfg(unix)]
68+
{
69+
stdout.write_all(var.as_bytes())?;
70+
stdout.write_all(separator)?;
71+
}
72+
#[cfg(not(unix))]
73+
{
74+
print!(
75+
"{}{}",
76+
var.to_string_lossy(),
77+
String::from_utf8_lossy(separator)
78+
);
79+
}
4680
} else {
4781
error_found = true;
4882
}

tests/by-util/test_printenv.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
//
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
5+
#[cfg(unix)]
6+
use std::ffi::OsString;
7+
#[cfg(unix)]
8+
use std::os::unix::ffi::OsStringExt;
59
use uutests::new_ucmd;
610

711
#[test]
@@ -90,3 +94,18 @@ fn test_null_separator() {
9094
.stdout_is("FOO\x00VALUE\x00");
9195
}
9296
}
97+
98+
#[test]
99+
#[cfg(unix)]
100+
fn test_non_utf8_value() {
101+
// Environment variable values can contain non-UTF-8 bytes on Unix.
102+
// printenv should output them correctly, matching GNU behavior.
103+
// Reproduces: LD_PRELOAD=$'/tmp/lib.so\xff' printenv LD_PRELOAD
104+
let value_with_invalid_utf8 = OsString::from_vec(b"/tmp/lib.so\xff".to_vec());
105+
106+
new_ucmd!()
107+
.env("LD_PRELOAD", &value_with_invalid_utf8)
108+
.arg("LD_PRELOAD")
109+
.succeeds()
110+
.stdout_is_bytes(b"/tmp/lib.so\xff\n");
111+
}

0 commit comments

Comments
 (0)