]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
ruby : fix segmentation fault (#3591)
authorKITAITI Makoto <redacted>
Mon, 5 Jan 2026 08:41:22 +0000 (17:41 +0900)
committerGitHub <redacted>
Mon, 5 Jan 2026 08:41:22 +0000 (17:41 +0900)
* Mark long live variable

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

* Don't use long live variable

* Fix indentation

bindings/ruby/ext/ruby_whisper_segment.c
bindings/ruby/ext/ruby_whisper_token.c
bindings/ruby/ext/ruby_whisper_vad_segment.c
bindings/ruby/test/test_token.rb

index 74221790928d5d37b10c6a1291b51c6a0a25e995..5229cb539003c3369a8fe21179c512ef33d3fac1 100644 (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);
index e0dbb79eb836db4546794be3c46814ee705e754f..ea4f4e635d2681270895a43d71a979761bcb096b 100644 (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);
index f444b419495d136e8514469eef869510323f38ba..49ff0aadcce81a46b7570dde37fc8d09a34cfea3 100644 (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);
index 214b355b372d2a19c7851041876f754e65fdab70..e5834b1b4804fd4ef4df7b5390504a6c58e16652 100644 (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