Skip to content
This repository was archived by the owner on Mar 9, 2023. It is now read-only.

Commit 4a5036c

Browse files
authored
fix: Limit only player logs not simulator logs (#28)
1 parent aa78f37 commit 4a5036c

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

src/cpp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Runner {
4040
))
4141
})?;
4242

43-
let _ = handle_process(compile, |x| SimulatorError::CompilationError(x))?;
43+
let _ = handle_process(compile, true, |x| SimulatorError::CompilationError(x))?;
4444

4545
Command::new("timeout".to_owned())
4646
.args([

src/java.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Runner {
4444
))
4545
})?;
4646

47-
let _ = handle_process(compile, |x| SimulatorError::CompilationError(x))?;
47+
let _ = handle_process(compile, true, |x| SimulatorError::CompilationError(x))?;
4848

4949
Command::new("timeout".to_owned())
5050
.args([

src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,33 @@ pub mod response;
1515
pub mod simulator;
1616
pub mod utils;
1717

18-
// maximum size for log will be around 2 MBs, everything after that is ignored
19-
const MAXLOGSIZE: usize = 2000000;
18+
// maximum size for log will be around 200KBs, everything after that is ignored
19+
const MAXLOGSIZE: usize = 200000;
2020
const SIGKILL: i32 = 9;
2121

2222
pub fn handle_process(
2323
proc: Child,
24+
is_player_process: bool,
2425
make_err: fn(String) -> SimulatorError,
2526
) -> Result<String, SimulatorError> {
2627
match proc.wait_with_output() {
2728
Ok(out) => {
28-
let mut truncated_logs = out.stderr.take(MAXLOGSIZE as u64);
29-
let mut logs = String::new();
30-
let logs_extraction_result = truncated_logs.read_to_string(&mut logs);
29+
let logs_extraction_result: Result<String, std::io::Error> = if is_player_process {
30+
let mut logs = String::new();
31+
out.stderr
32+
.take(MAXLOGSIZE as u64)
33+
.read_to_string(&mut logs)
34+
.map(|_| logs)
35+
} else {
36+
String::from_utf8(out.stderr)
37+
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)))
38+
};
3139
if out.status.success() {
3240
match logs_extraction_result {
3341
Err(e) => Err(SimulatorError::UnidentifiedError(
3442
format!("Error during log extraction: {}", e).to_owned(),
3543
)),
36-
Ok(_) => Ok(logs),
44+
Ok(logs) => Ok(logs),
3745
}
3846
} else {
3947
if let Some(sig) = out.status.signal() {
@@ -50,7 +58,7 @@ pub fn handle_process(
5058
)
5159
.to_owned(),
5260
)),
53-
Ok(_) => Err(make_err(format!(
61+
Ok(logs) => Err(make_err(format!(
5462
"Program exited with non zero exit code: {} ",
5563
logs
5664
))),

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ fn handler(game_request: GameRequest) -> GameStatus {
111111
};
112112

113113
let player_process_out =
114-
cc_driver::handle_process(player_pid, |x| SimulatorError::RuntimeError(x));
114+
cc_driver::handle_process(player_pid, true, |x| SimulatorError::RuntimeError(x));
115115
if let Err(err) = player_process_out {
116116
error!("Error from player.");
117117
return create_error_response(&game_request, err);
118118
}
119119
let player_process_out = player_process_out.unwrap();
120120

121121
let sim_process_out =
122-
cc_driver::handle_process(sim_pid, |x| SimulatorError::RuntimeError(x));
122+
cc_driver::handle_process(sim_pid, false, |x| SimulatorError::RuntimeError(x));
123123
if let Err(err) = sim_process_out {
124124
error!("Error from simulator.");
125125
return create_error_response(&game_request, err);

0 commit comments

Comments
 (0)