diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-03-23 20:06:44 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-03-23 20:06:44 +0000 |
commit | d702f10fb345637e82f3fb9606f5aba243df5365 (patch) | |
tree | 26867e9f4b59dfb5568d071f09b5b647126c7af8 /crates | |
parent | c6d6a7d41213dc06bb1f36745d5eaf8b91a99b99 (diff) | |
parent | 79f583ed6622be591886f99974766a3aeda39182 (diff) |
Merge #8159
8159: Ignore proc-macro stdout to prevent IPC crash r=edwin0cheng a=edwin0cheng
fixes #7954
r? @flodiebold
Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/proc_macro_api/src/msg.rs | 35 | ||||
-rw-r--r-- | crates/proc_macro_api/src/process.rs | 7 | ||||
-rw-r--r-- | crates/proc_macro_srv/src/cli.rs | 7 | ||||
-rw-r--r-- | crates/rust-analyzer/tests/rust-analyzer/main.rs | 4 |
4 files changed, 38 insertions, 15 deletions
diff --git a/crates/proc_macro_api/src/msg.rs b/crates/proc_macro_api/src/msg.rs index 970f165ed..f525df152 100644 --- a/crates/proc_macro_api/src/msg.rs +++ b/crates/proc_macro_api/src/msg.rs | |||
@@ -55,8 +55,8 @@ pub enum ErrorCode { | |||
55 | } | 55 | } |
56 | 56 | ||
57 | pub trait Message: Serialize + DeserializeOwned { | 57 | pub trait Message: Serialize + DeserializeOwned { |
58 | fn read(inp: &mut impl BufRead) -> io::Result<Option<Self>> { | 58 | fn read(inp: &mut impl BufRead, buf: &mut String) -> io::Result<Option<Self>> { |
59 | Ok(match read_json(inp)? { | 59 | Ok(match read_json(inp, buf)? { |
60 | None => None, | 60 | None => None, |
61 | Some(text) => { | 61 | Some(text) => { |
62 | let mut deserializer = serde_json::Deserializer::from_str(&text); | 62 | let mut deserializer = serde_json::Deserializer::from_str(&text); |
@@ -76,14 +76,29 @@ pub trait Message: Serialize + DeserializeOwned { | |||
76 | impl Message for Request {} | 76 | impl Message for Request {} |
77 | impl Message for Response {} | 77 | impl Message for Response {} |
78 | 78 | ||
79 | fn read_json(inp: &mut impl BufRead) -> io::Result<Option<String>> { | 79 | fn read_json<'a>( |
80 | let mut buf = String::new(); | 80 | inp: &mut impl BufRead, |
81 | inp.read_line(&mut buf)?; | 81 | mut buf: &'a mut String, |
82 | buf.pop(); // Remove trailing '\n' | 82 | ) -> io::Result<Option<&'a String>> { |
83 | Ok(match buf.len() { | 83 | loop { |
84 | 0 => None, | 84 | buf.clear(); |
85 | _ => Some(buf), | 85 | |
86 | }) | 86 | inp.read_line(&mut buf)?; |
87 | buf.pop(); // Remove trailing '\n' | ||
88 | |||
89 | if buf.is_empty() { | ||
90 | return Ok(None); | ||
91 | } | ||
92 | |||
93 | // Some ill behaved macro try to use stdout for debugging | ||
94 | // We ignore it here | ||
95 | if !buf.starts_with("{") { | ||
96 | log::error!("proc-macro tried to print : {}", buf); | ||
97 | continue; | ||
98 | } | ||
99 | |||
100 | return Ok(Some(buf)); | ||
101 | } | ||
87 | } | 102 | } |
88 | 103 | ||
89 | fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> { | 104 | fn write_json(out: &mut impl Write, msg: &str) -> io::Result<()> { |
diff --git a/crates/proc_macro_api/src/process.rs b/crates/proc_macro_api/src/process.rs index 30bb1b687..99d05aef3 100644 --- a/crates/proc_macro_api/src/process.rs +++ b/crates/proc_macro_api/src/process.rs | |||
@@ -90,8 +90,10 @@ impl ProcMacroProcessSrv { | |||
90 | fn client_loop(task_rx: Receiver<Task>, mut process: Process) { | 90 | fn client_loop(task_rx: Receiver<Task>, mut process: Process) { |
91 | let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio"); | 91 | let (mut stdin, mut stdout) = process.stdio().expect("couldn't access child stdio"); |
92 | 92 | ||
93 | let mut buf = String::new(); | ||
94 | |||
93 | for Task { req, result_tx } in task_rx { | 95 | for Task { req, result_tx } in task_rx { |
94 | match send_request(&mut stdin, &mut stdout, req) { | 96 | match send_request(&mut stdin, &mut stdout, req, &mut buf) { |
95 | Ok(res) => result_tx.send(res).unwrap(), | 97 | Ok(res) => result_tx.send(res).unwrap(), |
96 | Err(err) => { | 98 | Err(err) => { |
97 | log::error!( | 99 | log::error!( |
@@ -152,7 +154,8 @@ fn send_request( | |||
152 | mut writer: &mut impl Write, | 154 | mut writer: &mut impl Write, |
153 | mut reader: &mut impl BufRead, | 155 | mut reader: &mut impl BufRead, |
154 | req: Request, | 156 | req: Request, |
157 | buf: &mut String, | ||
155 | ) -> io::Result<Option<Response>> { | 158 | ) -> io::Result<Option<Response>> { |
156 | req.write(&mut writer)?; | 159 | req.write(&mut writer)?; |
157 | Response::read(&mut reader) | 160 | Response::read(&mut reader, buf) |
158 | } | 161 | } |
diff --git a/crates/proc_macro_srv/src/cli.rs b/crates/proc_macro_srv/src/cli.rs index d428b9567..bc48f1c43 100644 --- a/crates/proc_macro_srv/src/cli.rs +++ b/crates/proc_macro_srv/src/cli.rs | |||
@@ -6,8 +6,9 @@ use std::io; | |||
6 | 6 | ||
7 | pub fn run() -> io::Result<()> { | 7 | pub fn run() -> io::Result<()> { |
8 | let mut srv = ProcMacroSrv::default(); | 8 | let mut srv = ProcMacroSrv::default(); |
9 | let mut buf = String::new(); | ||
9 | 10 | ||
10 | while let Some(req) = read_request()? { | 11 | while let Some(req) = read_request(&mut buf)? { |
11 | let res = match req { | 12 | let res = match req { |
12 | msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro), | 13 | msg::Request::ListMacro(task) => srv.list_macros(&task).map(msg::Response::ListMacro), |
13 | msg::Request::ExpansionMacro(task) => { | 14 | msg::Request::ExpansionMacro(task) => { |
@@ -30,8 +31,8 @@ pub fn run() -> io::Result<()> { | |||
30 | Ok(()) | 31 | Ok(()) |
31 | } | 32 | } |
32 | 33 | ||
33 | fn read_request() -> io::Result<Option<msg::Request>> { | 34 | fn read_request(buf: &mut String) -> io::Result<Option<msg::Request>> { |
34 | msg::Request::read(&mut io::stdin().lock()) | 35 | msg::Request::read(&mut io::stdin().lock(), buf) |
35 | } | 36 | } |
36 | 37 | ||
37 | fn write_response(msg: msg::Response) -> io::Result<()> { | 38 | fn write_response(msg: msg::Response) -> io::Result<()> { |
diff --git a/crates/rust-analyzer/tests/rust-analyzer/main.rs b/crates/rust-analyzer/tests/rust-analyzer/main.rs index 19516de7b..4442cbff6 100644 --- a/crates/rust-analyzer/tests/rust-analyzer/main.rs +++ b/crates/rust-analyzer/tests/rust-analyzer/main.rs | |||
@@ -712,6 +712,10 @@ pub fn foo(_input: TokenStream) -> TokenStream { | |||
712 | // We hard code the output here for preventing to use any deps | 712 | // We hard code the output here for preventing to use any deps |
713 | let mut res = TokenStream::new(); | 713 | let mut res = TokenStream::new(); |
714 | 714 | ||
715 | // ill behaved proc-macro will use the stdout | ||
716 | // we should ignore it | ||
717 | println!("I am bad guy"); | ||
718 | |||
715 | // impl Bar for Foo { fn bar() {} } | 719 | // impl Bar for Foo { fn bar() {} } |
716 | let mut tokens = vec![t!("impl"), t!("Bar"), t!("for"), t!("Foo")]; | 720 | let mut tokens = vec![t!("impl"), t!("Bar"), t!("for"), t!("Foo")]; |
717 | let mut fn_stream = TokenStream::new(); | 721 | let mut fn_stream = TokenStream::new(); |