Skip to content

Ignore malformed environment strings like glibc does #29297

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

Merged
merged 1 commit into from
Nov 17, 2015

Conversation

tbu-
Copy link
Contributor

@tbu- tbu- commented Oct 25, 2015

Otherwise, the iterator and the functions for getting specific
environment variables might disagree, for environments like

FOOBAR

@rust-highfive
Copy link
Contributor

r? @brson

(rust_highfive has picked a reviewer for you, use r? to override)

@tbu-
Copy link
Contributor Author

tbu- commented Oct 25, 2015

Here's a test program for this behavior:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

extern char **environ;

int main(int argc, char **argv) {
    printf("ARGV:\n");
    for(; *argv; argv++) {
        printf("%s\n", *argv);
    }
    printf("\n");
    printf("ENV:\n");
    char **env = environ;
    for(; *env; env++) {
        printf("%s\n", *env);
    }
    printf("\n");

    char *new_argv[] = {
        NULL
    };

    char *new_env[] = {
        "FOOBAR",
        NULL
    };

    if(argc != 0) {
        execve("./a.out", new_argv, new_env);
        perror("execve ./a.out");
    } else {
        char *interesting = getenv("FOOBAR");
        if(interesting) {
            printf("FOOBAR=%s\n", interesting);
        } else {
            printf("(nil)\n");
        }
    }

    return 0;
}

@tbu- tbu- force-pushed the pr_env_ignore_malformed branch from 9e8bf6e to 556a121 Compare October 25, 2015 21:58
// by an ASCII equals sign '='. Since a variable name must not be
// empty, allow variable names starting with an equals sign. Skip all
// malformed lines.
if input.len() < 1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this could just be if input.len() == 0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if input.is_empty() also works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explicitly chose to use < 1 as I use the index 1 later on. But if you want me to change that, please say so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let's just stick to is_empty here

@alexcrichton
Copy link
Member

Could a linux-specific run-pass test be added for this perhaps?

@tbu-
Copy link
Contributor Author

tbu- commented Oct 26, 2015

Sure, will look into that.

@tbu-
Copy link
Contributor Author

tbu- commented Oct 27, 2015

What kind of test do you think of? I can't really set a malformed environment variable without resorting to the execve syscall.

@@ -399,24 +399,33 @@ pub unsafe fn environ() -> *mut *const *const c_char {
pub fn env() -> Env {
return unsafe {
let mut environ = *environ();
if environ as usize == 0 {
if environ == ptr::null() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use .is_null().

@eddyb
Copy link
Member

eddyb commented Oct 27, 2015

@tbu- You'd have to maybe write a run-make test with a C program? Not sure if we support that easily.

@alexcrichton
Copy link
Member

Ah yeah I was thinking of a run-pass test that just called execve directly (e.g. translate the C into Rust). I personally prefer to avoid run-make wherever possible.

@tbu-
Copy link
Contributor Author

tbu- commented Nov 5, 2015

@alexcrichton Added a test.

@alexcrichton
Copy link
Member

@bors: r+ 2373415a17e4d9d237b364d787efe4844c8ab98c

@bors
Copy link
Collaborator

bors commented Nov 5, 2015

⌛ Testing commit 2373415 with merge 9c2df72...

@bors
Copy link
Collaborator

bors commented Nov 6, 2015

💔 Test failed - auto-linux-64-opt

@tbu-
Copy link
Contributor Author

tbu- commented Nov 6, 2015

<program name unknown>: error while loading shared libraries: libstd-71b07a99.so: cannot open shared object file: No such file or directory

Clearly caused by my changes, but I'm not sure how to fix that.

@eddyb
Copy link
Member

eddyb commented Nov 6, 2015

@tbu- If your changes remove LD_LIBRARY_PATH or something similar, try appending something malformed to the environment instead of replacing it.

@alexcrichton
Copy link
Member

Yeah, @eddyb's diagnosis is right. You may be able to get around this with // no-prefer-dynamic which should build a mostly-static binary

@tbu-
Copy link
Contributor Author

tbu- commented Nov 6, 2015

@alexcrichton Fixed.

@alexcrichton
Copy link
Member

@bors: r+ 9dc752fa3be645c74eed41c331a7c5351345ea05

@bors
Copy link
Collaborator

bors commented Nov 6, 2015

⌛ Testing commit 9dc752f with merge d383c36...

@bors
Copy link
Collaborator

bors commented Nov 6, 2015

💔 Test failed - auto-win-gnu-64-opt

@tbu-
Copy link
Contributor Author

tbu- commented Nov 6, 2015

I have no experience with Windows. Does this fail because execve always fails on Windows? Does Windows need an nonempty argv?

@alexcrichton
Copy link
Member

Oh execve isn't actually a thing on Windows, I believe mingw must just have a wrapper around it? It's fine to just completely ignore this tests on Windows.

@tbu-
Copy link
Contributor Author

tbu- commented Nov 14, 2015

@alexcrichton I believe this one is ready to be merged.

@alexcrichton
Copy link
Member

Thanks! Could you also squash all these commits down?

@tbu- tbu- force-pushed the pr_env_ignore_malformed branch from 96a9627 to c910006 Compare November 16, 2015 01:01
@tbu-
Copy link
Contributor Author

tbu- commented Nov 16, 2015

@alexcrichton Done.

@alexcrichton
Copy link
Member

Looks like travis is reporting some errors with make tidy

@tbu- tbu- force-pushed the pr_env_ignore_malformed branch from c910006 to 2917cd4 Compare November 16, 2015 01:10
@tbu-
Copy link
Contributor Author

tbu- commented Nov 16, 2015

Fixed.

@alexcrichton
Copy link
Member

@bors: r+

On Sunday, November 15, 2015, tbu- notifications@github.com wrote:

Fixed.


Reply to this email directly or view it on GitHub
#29297 (comment).

@bors
Copy link
Collaborator

bors commented Nov 16, 2015

📌 Commit 2917cd4 has been approved by alexcrichton

@bors
Copy link
Collaborator

bors commented Nov 16, 2015

⌛ Testing commit 2917cd4 with merge fa2f0fd...

@bors
Copy link
Collaborator

bors commented Nov 16, 2015

💔 Test failed - auto-linux-64-x-android-t

@tbu-
Copy link
Contributor Author

tbu- commented Nov 16, 2015

.. segmentation fault? That's not supposed to happen. No idea where this comes from.

@alexcrichton
Copy link
Member

It may be the case that the libc on android just doesn't handle calls like this and has bug itself. Either way it's fine to ignore android as well.

@alexcrichton
Copy link
Member

(so long as a comment is added as to why it's ignored on android)

Otherwise, the iterator and the functions for getting specific
environment variables might disagree, for environments like

    FOOBAR

Variable names starting with equals sign are OK:

glibc only interprets equals signs not in the first position as
separators between variable name and variable value. Instead of skipping
them entirely, a leading equals sign is interpreted to be part of the
variable name.
@tbu- tbu- force-pushed the pr_env_ignore_malformed branch from 2917cd4 to 87243bc Compare November 16, 2015 23:32
@tbu-
Copy link
Contributor Author

tbu- commented Nov 16, 2015

@alexcrichton Ignored the test, added a comment.

@alexcrichton
Copy link
Member

@bors: r+ 87243bc

bors added a commit that referenced this pull request Nov 17, 2015
Otherwise, the iterator and the functions for getting specific
environment variables might disagree, for environments like

    FOOBAR
@bors
Copy link
Collaborator

bors commented Nov 17, 2015

⌛ Testing commit 87243bc with merge a7644b3...

@bors bors merged commit 87243bc into rust-lang:master Nov 17, 2015
@petrochenkov
Copy link
Contributor

@tbu-
test/run-pass/env-vars.rs fails on my Windows 8.1 64-bit / MSYS2 / MinGW-w64

---- [run-pass] run-pass/env-vars.rs stdout ----

error: test run failed!
status: exit code: 101
command: PATH="x86_64-pc-windows-gnu/stage2/lib/rustlib/x86_64-pc-windows-gnu/lib;C:\msys64\home\we\rust\x86_64-pc-windows-gnu\stage2\bin;C:\msys64\mingw64\bin;C:\msys64\usr\local\bin;C:\msys64\usr\bin;C:\msys64\usr\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\ProgramData\chocolatey\bin;C:\Program Files (x86)\Skype\Phone;C:\msys64\usr\bin\site_perl;C:\msys64\usr\bin\vendor_perl;C:\msys64\usr\bin\core_perl" x86_64-pc-windows-gnu/test/run-pass\env-vars.stage2-x86_64-pc-windows-gnu.exe
stdout:
------------------------------------------

------------------------------------------
stderr:
------------------------------------------
thread '<main>' panicked at 'bad vars->var transition: "" "C:=C:\\Windows\\System32" None', C:/msys64/home/we/rust/src/test/run-pass/env-vars.rs:17

------------------------------------------

thread '[run-pass] run-pass/env-vars.rs' panicked at 'explicit panic', C:/msys64/home/we/rust/src/compiletest\runtest.rs:1501

@tbu-
Copy link
Contributor Author

tbu- commented Nov 17, 2015

@petrochenkov Can you check whether #29901 fixes that?

@petrochenkov
Copy link
Contributor

@tbu-
No.
It doesn't compile, I modified it to compile, but it still doesn't fix the test.

@tbu-
Copy link
Contributor Author

tbu- commented Nov 18, 2015

@petrochenkov Made a pull request to ignore it again: #29910. See also #29911.

bors added a commit that referenced this pull request Nov 19, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants