Skip to content

std::os has a possible infinite loop/OOM crash when running commands that return a lot of text #12376

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

Closed
huonw opened this issue Feb 18, 2014 · 0 comments · Fixed by #12396
Closed
Labels
O-windows Operating system: Windows

Comments

@huonw
Copy link
Member

huonw commented Feb 18, 2014

I don't have a windows set-up, but I believe the following will crash with OOM (it would be nice for someone to verify that it actually does fail):

use std::os;

fn main() {
    os::setenv("test_env", "x".repeat(10000));
    println!("Set ok!");

    let env = os::getenv("test_env"); // will probably crash here
    println!("Got Some? {}", env.is_some());
}

The following patch theoretically fixes it, but causes the try bot to fail:

diff --git a/src/libstd/os.rs b/src/libstd/os.rs
index 719ed62..7f0e585 100644
--- a/src/libstd/os.rs
+++ b/src/libstd/os.rs
@@ -112,7 +112,7 @@ pub mod win32 {
             let mut done = false;
             while !done {
                 let mut buf = vec::from_elem(n as uint, 0u16);
-                let k = f(buf.as_mut_ptr(), TMPBUF_SZ as DWORD);
+                let k = f(buf.as_mut_ptr(), n);
                 if k == (0 as DWORD) {
                     done = true;
                 } else if k == n &&
@@ -1498,6 +1498,14 @@ mod tests {
     }

     #[test]
+    fn test_env_set_get_huge() {
+        let n = make_rand_name();
+        let s = "x".repeat(10000);
+        setenv(n, s);
+        assert_eq!(getenv(n), Some(s));
+    }
+
+    #[test]
     fn test() {
         assert!((!Path::new("test-path").is_absolute()));

Failure:

---- os::tests::test_env_set_get_huge stdout ----
    task 'os::tests::test_env_set_get_huge' failed at 'assertion failed: end <= self.len()', C:\bot\slave\try-win\build\src\libstd\vec.rs:992

---- run::tests::test_inherit_env stdout ----
    task 'run::tests::test_inherit_env' failed at 'assertion failed: k.is_empty() || output.contains(format!("{}={}" , * k , * v))', C:\bot\slave\try-win\build\src\libstd\run.rs:602

(transmute::<pull, issue>(#12365))

bors added a commit that referenced this issue Feb 20, 2014
On windows, the GetEnvironmentVariable function will return the necessary buffer
size if the buffer provided was too small. This case previously fell through the
checks inside of fill_utf16_buf_and_decode, tripping an assertion in the `slice`
method.

This adds an extra case for when the return value is >= the buffer size, in
which case we assume the return value as the new buffer size and try again.

Closes #12376
bors added a commit to rust-lang-ci/rust that referenced this issue Jul 25, 2022
fix: Insert whitespace into trait-impl completions when coming from macros

Fixes rust-lang/rust-analyzer#12278
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows Operating system: Windows
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant