prevent panics on core load failure

This commit is contained in:
Corwin 2021-07-03 16:33:26 +00:00 committed by GitHub
parent 7ab17d7a4e
commit eb538b4536
3 changed files with 8 additions and 4 deletions

View file

@ -31,6 +31,7 @@ struct MGBA* new_runner(char* filename) {
struct mCore* core = mCoreFind(mgba->filename);
if (!core) {
printf("failed to find core\n");
free(mgba);
return NULL;
}

View file

@ -20,7 +20,7 @@ fn test_file(file_to_run: &str) -> Status {
let mut finished = Status::Running;
let debug_reader_mutex = Regex::new(r"(?s)^\[(.*)\] GBA Debug: (.*)$").unwrap();
let mut mgba = runner::MGBA::new(file_to_run);
let mut mgba = runner::MGBA::new(file_to_run).unwrap();
let video_buffer = mgba.get_video_buffer();
mgba.set_logger(|message| {

View file

@ -35,10 +35,13 @@ impl VideoBuffer {
}
impl MGBA {
pub fn new(filename: &str) -> Self {
pub fn new(filename: &str) -> Result<Self, anyhow::Error> {
let c_str = CString::new(filename).expect("should be able to make cstring from filename");
MGBA {
mgba: unsafe { bindings::new_runner(c_str.as_ptr() as *mut i8) },
let mgba = unsafe { bindings::new_runner(c_str.as_ptr() as *mut i8) };
if mgba.is_null() {
Err(anyhow::anyhow!("could not create core"))
} else {
Ok(MGBA { mgba })
}
}