Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear env in user function #394

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions plrust-tests/src/trusted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,31 +190,32 @@ mod tests {

#[pg_test]
#[search_path(@extschema@)]
#[should_panic = "error: the `env` and `option_env` macros are forbidden"]
#[should_panic = "environment variable `FOOBAR` not defined at compile time"]
#[cfg(feature = "trusted")]
fn plrust_block_env() -> spi::Result<()> {
let definition = r#"
CREATE FUNCTION get_path() RETURNS text AS $$
let path = env!("PATH");
Ok(Some(path.to_string()))
CREATE FUNCTION get_foobar() RETURNS text AS $$
let foo = env!("FOOBAR");
Ok(Some(foo.to_string()))
$$ LANGUAGE plrust;
"#;
std::env::set_var("FOOBAR", "1");
Spi::run(definition)
}

#[pg_test]
#[search_path(@extschema@)]
#[should_panic = "error: the `env` and `option_env` macros are forbidden"]
#[should_panic = "the `option_env` macro always returns `None` in the PL/Rust user function"]
#[cfg(feature = "trusted")]
fn plrust_block_option_env() -> spi::Result<()> {
let definition = r#"
CREATE FUNCTION try_get_path() RETURNS text AS $$
match option_env!("PATH") {
None => Ok(None),
Some(s) => Ok(Some(s.to_string()))
}
CREATE FUNCTION try_get_foobar2() RETURNS text AS $$
let v = option_env!("FOOBAR2")
.expect("the `option_env` macro always returns `None` in the PL/Rust user function");
Ok(Some(v.to_string()))
$$ LANGUAGE plrust;
"#;
std::env::set_var("FOOBAR2", "1");
Spi::run(definition)
}

Expand Down
13 changes: 13 additions & 0 deletions plrustc/plrustc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ impl Callbacks for PlrustcCallbacks {
}
}

fn clear_env() {
for (name, _) in std::env::vars_os() {
// Can't remove `PATH`, need it to locate linker and such.
if name == "PATH" {
continue;
}
std::env::remove_var(name);
}
}

fn main() {
rustc_driver::install_ice_hook("https://github.com/tcdi/plrust/issues/new", |_| ());
let handler = &EarlyErrorHandler::new(ErrorOutputType::default());
Expand All @@ -58,6 +68,9 @@ fn main() {
let args =
rustc_driver::args::arg_expand_all(handler, &std::env::args().collect::<Vec<_>>());
let config = PlrustcConfig::from_env_and_args(&args);
if config.compiling_user_crate() {
clear_env();
}
run_compiler(
args,
&mut PlrustcCallbacks {
Expand Down