ruby : fix segmentation fault (#3591)

* Mark long live variable

* Fix test for Whisper::Token#deconstruct_keys(nil)

* Don't use long live variable

* Fix indentation
This commit is contained in:
KITAITI Makoto 2026-01-05 17:41:22 +09:00 committed by GitHub
parent e9898ddfb9
commit 679bdb53db
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 34 deletions

View File

@ -11,7 +11,6 @@ static VALUE sym_text;
static VALUE sym_no_speech_prob;
static VALUE sym_speaker_turn_next;
static VALUE sym_n_tokens;
static VALUE key_names;
extern const rb_data_type_t ruby_whisper_type;
@ -221,7 +220,14 @@ ruby_whisper_segment_deconstruct_keys(VALUE self, VALUE keys)
VALUE hash = rb_hash_new();
long n_keys;
if (NIL_P(keys)) {
keys = key_names;
keys = rb_ary_new3(
N_KEY_NAMES,
sym_start_time,
sym_end_time,
sym_text,
sym_no_speech_prob,
sym_speaker_turn_next
);
n_keys = N_KEY_NAMES;
} else {
n_keys = RARRAY_LEN(keys);
@ -265,14 +271,6 @@ init_ruby_whisper_segment(VALUE *mWhisper)
sym_no_speech_prob = ID2SYM(rb_intern("no_speech_prob"));
sym_speaker_turn_next = ID2SYM(rb_intern("speaker_turn_next"));
sym_n_tokens = ID2SYM(rb_intern("n_tokens"));
key_names = rb_ary_new3(
N_KEY_NAMES,
sym_start_time,
sym_end_time,
sym_text,
sym_no_speech_prob,
sym_speaker_turn_next
);
rb_define_alloc_func(cSegment, ruby_whisper_segment_allocate);
rb_define_method(cSegment, "start_time", ruby_whisper_segment_get_start_time, 0);

View File

@ -6,7 +6,6 @@
extern VALUE cToken;
extern const rb_data_type_t ruby_whisper_type;
static VALUE key_names;
static VALUE sym_id;
static VALUE sym_tid;
static VALUE sym_probability;
@ -241,7 +240,20 @@ static VALUE ruby_whisper_token_deconstruct_keys(VALUE self, VALUE keys)
long n_keys = 0;
if (NIL_P(keys)) {
keys = key_names;
keys = rb_ary_new3(
N_KEY_NAMES,
sym_id,
sym_tid,
sym_probability,
sym_log_probability,
sym_pt,
sym_ptsum,
sym_t_dtw,
sym_voice_length,
sym_start_time,
sym_end_time,
sym_text
);
n_keys = N_KEY_NAMES;
} else {
n_keys = RARRAY_LEN(keys);
@ -320,20 +332,6 @@ init_ruby_whisper_token(VALUE *mWhisper)
sym_start_time = ID2SYM(rb_intern("start_time"));
sym_end_time = ID2SYM(rb_intern("end_time"));
sym_text = ID2SYM(rb_intern("text"));
key_names = rb_ary_new3(
N_KEY_NAMES,
sym_id,
sym_tid,
sym_probability,
sym_log_probability,
sym_pt,
sym_ptsum,
sym_t_dtw,
sym_voice_length,
sym_start_time,
sym_end_time,
sym_text
);
rb_define_method(cToken, "id", ruby_whisper_token_get_id, 0);
rb_define_method(cToken, "tid", ruby_whisper_token_get_tid, 0);

View File

@ -9,7 +9,6 @@ extern const rb_data_type_t ruby_whisper_vad_segments_type;
static VALUE sym_start_time;
static VALUE sym_end_time;
static VALUE key_names;
static void
rb_whisper_vad_segment_mark(void *p)
@ -100,7 +99,11 @@ ruby_whisper_vad_segment_deconstruct_keys(VALUE self, VALUE keys)
hash = rb_hash_new();
if (NIL_P(keys)) {
keys = key_names;
keys = rb_ary_new3(
N_KEY_NAMES,
sym_start_time,
sym_end_time
);
n_keys = N_KEY_NAMES;
} else {
n_keys = RARRAY_LEN(keys);
@ -128,11 +131,6 @@ init_ruby_whisper_vad_segment(VALUE *mVAD)
sym_start_time = ID2SYM(rb_intern("start_time"));
sym_end_time = ID2SYM(rb_intern("end_time"));
key_names = rb_ary_new3(
N_KEY_NAMES,
sym_start_time,
sym_end_time
);
rb_define_alloc_func(cVADSegment, ruby_whisper_vad_segment_s_allocate);
rb_define_method(cVADSegment, "start_time", ruby_whisper_vad_segment_get_start_time, 0);

View File

@ -57,7 +57,9 @@ class TestToken < TestBase
end
def test_deconstruct_keys_with_nil
assert_equal({}, @token.deconstruct_keys(nil))
keys = %i[id tid probability log_probability pt ptsum t_dtw voice_length start_time end_time text]
expected = keys.collect {|key| [key, @token.send(key)] }.to_h
assert_equal(expected, @token.deconstruct_keys(nil))
end
def test_deconstruct_keys_with_keys