Skip to content

Commit a885570

Browse files
committed
Move ERROR_BUF to RMain
This works only because of the `UnsafeCell` around `RMain`
1 parent 9f9d224 commit a885570

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

crates/ark/src/interface.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ pub struct RMain {
229229

230230
/// Banner output accumulated during startup
231231
r_banner: String,
232+
233+
/// Raw error buffer provided to `Rf_error()` when throwing `r_read_console()` errors.
234+
/// Stored in `RMain` to avoid memory leakage when `Rf_error()` jumps.
235+
r_error_buffer: Option<CString>,
232236
}
233237

234238
/// Represents the currently active execution request from the frontend. It
@@ -564,6 +568,7 @@ impl RMain {
564568
positron_ns: None,
565569
pending_lines: Vec::new(),
566570
r_banner: String::new(),
571+
r_error_buffer: None,
567572
}
568573
}
569574

@@ -1951,17 +1956,12 @@ pub extern "C" fn r_read_console(
19511956
return 0;
19521957
},
19531958
ConsoleResult::Error(err) => {
1954-
// Save error message to a global buffer to avoid leaking memory
1959+
// Save error message to `RMain`'s buffer to avoid leaking memory
19551960
// when `Rf_error()` jumps. Some gymnastics are required to deal
19561961
// with the possibility of `CString` conversion failure since the
19571962
// error message comes from the frontend and might be corrupted.
1958-
static mut ERROR_BUF: Option<CString> = None;
1959-
1960-
unsafe {
1961-
ERROR_BUF = Some(new_cstring(format!("\n{err}")));
1962-
}
1963-
1964-
unsafe { Rf_error(ERROR_BUF.as_ref().unwrap().as_ptr()) };
1963+
main.r_error_buffer = Some(new_cstring(format!("\n{err}")));
1964+
unsafe { Rf_error(main.r_error_buffer.as_ref().unwrap().as_ptr()) };
19651965
},
19661966
};
19671967
}

0 commit comments

Comments
 (0)