Skip to content
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

Fix memory leak in error::raise #30

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
//! Rust types for working with Ruby Exceptions and other interrupts.

use std::{
any::Any,
borrow::Cow,
ffi::CString,
fmt,
mem::transmute,
ops::Deref,
os::raw::{c_char, c_int},
};
use std::{any::Any, borrow::Cow, ffi::CString, fmt, mem::transmute, ops::Deref, os::raw::c_int};

use rb_sys::{
rb_bug, rb_ensure, rb_errinfo, rb_exc_raise, rb_iter_break, rb_iter_break_value, rb_jump_tag,
rb_protect, rb_raise, rb_set_errinfo, rb_warning, ruby_special_consts, VALUE,
rb_bug, rb_ensure, rb_errinfo, rb_exc_new_str, rb_exc_raise, rb_iter_break,
rb_iter_break_value, rb_jump_tag, rb_protect, rb_set_errinfo, rb_warning, ruby_special_consts,
VALUE,
};

use crate::{
debug_assert_value,
exception::{self, Exception, ExceptionClass},
module::Module,
value::{ReprValue, Value, QNIL},
RString,
};

/// A Rust representation of a Ruby `Exception` or other interrupt.
Expand Down Expand Up @@ -275,15 +269,15 @@ pub(crate) fn raise(e: Error) -> ! {
Error::Jump(tag) => tag.resume(),
Error::Error(class, msg) => {
debug_assert_value!(class);
const FMT_S: &'static str = "%s\0";
let msg = CString::new(msg.into_owned()).unwrap();
unsafe {
rb_raise(
class.as_rb_value(),
FMT_S.as_ptr() as *const c_char,
msg.as_ptr(),
)
}

// We use `rb_exc_new_str` here because `rb_raise` nevers frees the
// string buffer, and causes memory leaks. Using `rb_exc_new_str`
// allows the GC deallocate the message later.
let rmsg = RString::from(msg.as_ref()).as_rb_value();
drop(msg);
let exc = unsafe { rb_exc_new_str(class.as_rb_value(), rmsg) };
unsafe { rb_exc_raise(exc) };

unreachable!()
}
Error::Exception(e) => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@
//! * `rb_eval_string_protect`: [`eval()`] or [`eval!`].
// * `rb_eval_string_wrap`:
// * `rb_exc_fatal`:
// * `rb_exc_new`:
//! * `rb_exc_new`: See [`error::raise`].
// * `rb_exc_new_cstr`:
// * `rb_exc_new_str`:
//! * `rb_exc_raise`: Return [`Error`].
Expand Down Expand Up @@ -1259,7 +1259,7 @@
// * `rb_ractor_stdin_set`:
// * `rb_ractor_stdout`:
// * `rb_ractor_stdout_set`:
//! * `rb_raise`: Return [`Error`].
// * `rb_raise`:
// * `rb_random_base_init`:
// * `rb_random_bytes`:
// * `RB_RANDOM_DATA_INIT_PARENT`:
Expand Down