Commit notifications for all Sup repositories
 help / color / mirror / Atom feed
From: Dan Callaghan <dan@bl2.djc.id.au>
To: sup-commits@supmua.dev
Subject: [ncursesw-ruby.git] 05/10: migrate to TypedData_Wrap_Struct() and family
Date: Sat, 17 Oct 2026 11:25:03 +1100	[thread overview]
Message-ID: <20261017002459.1F0BF29BBCF@bl2.djc.id.au> (raw)
In-Reply-To: <179219669847.381480.6906705364727468632@bl2.djc.id.au>

New commit on branch master:
https://supmua.dev/git/ncursesw-ruby/commit/ecd4c8300b813f6dc26c7ffe2ba4b0bbad124415.html

commit ecd4c8300b813f6dc26c7ffe2ba4b0bbad124415
Author: Dan Callaghan <djc@djc.id.au>
AuthorDate: Sat Oct 10 15:26:03 2026 +1100

    migrate to TypedData_Wrap_Struct() and family
    
    This provides type checking before dereferencing the wrapped pointers
    passed back from Ruby, avoiding potential segfaults due to memory
    reinterpretation.
---
 form_wrap.c    | 21 +++++++++++++++------
 menu_wrap.c    | 14 ++++++++++----
 ncurses_wrap.c | 14 ++++++++++----
 panel_wrap.c   |  7 +++++--
 4 files changed, 40 insertions(+), 16 deletions(-)

diff --git a/form_wrap.c b/form_wrap.c
index 8a3a837..2399e37 100644
--- a/form_wrap.c
+++ b/form_wrap.c
@@ -253,6 +253,9 @@ void init_form_opts_constants() {
 /*
  * FIELD wrapper
  */
+static const struct rb_data_type_struct field_rbtype = {
+    .wrap_struct_name = "Ncurses::Form::FIELDTYPE",
+};
 static VALUE wrap_field(FIELD* field)
 {
   if (field == 0) return Qnil;
@@ -261,7 +264,7 @@ static VALUE wrap_field(FIELD* field)
 	 VALUE field_adress  = INT2NUM((long)(field));
 	 VALUE rb_field      = rb_hash_aref(fields_hash, field_adress);
 	 if (rb_field == Qnil) {
-		rb_field = Data_Wrap_Struct(cFIELD, 0, 0, field);
+		rb_field = TypedData_Wrap_Struct(cFIELD, &field_rbtype, field);
 		rb_iv_set(rb_field, "@destroyed", Qfalse);
 		rb_hash_aset(fields_hash, field_adress, rb_field);
 	 }
@@ -276,13 +279,16 @@ static FIELD* get_field(VALUE rb_field)
 	 rb_raise(rb_eRuntimeError, "Attempt to access a destroyed field");
 	 return 0;
   }
-  Data_Get_Struct(rb_field, FIELD, field);
+  TypedData_Get_Struct(rb_field, FIELD, &field_rbtype, field);
   return field;
 }
 
 /*
  * FIELDTYPE wrapper
  */
+static const struct rb_data_type_struct fieldtype_rbtype = {
+    .wrap_struct_name = "Ncurses::Form::FIELD",
+};
 static VALUE wrap_fieldtype(FIELDTYPE* fieldtype)
 {
   if (fieldtype == NULL) return Qnil;
@@ -291,7 +297,7 @@ static VALUE wrap_fieldtype(FIELDTYPE* fieldtype)
 	 VALUE fieldtype_adress  = INT2NUM((long)(fieldtype));
 	 VALUE rb_fieldtype      = rb_hash_aref(fieldtypes_hash, fieldtype_adress);
 	 if (rb_fieldtype == Qnil) {
-		rb_fieldtype = Data_Wrap_Struct(cFIELDTYPE, 0, 0, fieldtype);
+		rb_fieldtype = TypedData_Wrap_Struct(cFIELDTYPE, &fieldtype_rbtype, fieldtype);
 		rb_iv_set(rb_fieldtype, "@destroyed", Qfalse);
 		rb_hash_aset(fieldtypes_hash, fieldtype_adress, rb_fieldtype);
 	 }
@@ -306,13 +312,16 @@ static FIELDTYPE* get_fieldtype(VALUE rb_fieldtype)
 	 rb_raise(rb_eRuntimeError, "Attempt to access a destroyed fieldtype");
 	 return 0;
   }
-  Data_Get_Struct(rb_fieldtype, FIELDTYPE, fieldtype);
+  TypedData_Get_Struct(rb_fieldtype, FIELDTYPE, &fieldtype_rbtype, fieldtype);
   return fieldtype;
 }
 
 /*
  * FORM wrapper
  */
+static const struct rb_data_type_struct form_rbtype = {
+    .wrap_struct_name = "Ncurses::Form::FORM",
+};
 static VALUE wrap_form(FORM* form)
 {
   if (form == 0) return Qnil;
@@ -321,7 +330,7 @@ static VALUE wrap_form(FORM* form)
 	 VALUE form_adress  = INT2NUM((long)(form));
 	 VALUE rb_form      = rb_hash_aref(forms_hash, form_adress);
 	 if (rb_form == Qnil) {
-		rb_form = Data_Wrap_Struct(cFORM, 0, 0, form);
+		rb_form = TypedData_Wrap_Struct(cFORM, &form_rbtype, form);
 		rb_iv_set(rb_form, "@destroyed", Qfalse);
 		rb_hash_aset(forms_hash, form_adress, rb_form);
 	 }
@@ -336,7 +345,7 @@ static FORM* get_form(VALUE rb_form)
 	 rb_raise(rb_eRuntimeError, "Attempt to access a destroyed form");
 	 return 0;
   }
-  Data_Get_Struct(rb_form, FORM, form);
+  TypedData_Get_Struct(rb_form, FORM, &form_rbtype, form);
   return form;
 }
 
diff --git a/menu_wrap.c b/menu_wrap.c
index 0578d77..7c6a8ea 100644
--- a/menu_wrap.c
+++ b/menu_wrap.c
@@ -140,6 +140,9 @@ void init_menu_opts_constants(void)
 /*
  * ITEM wrapper
  */
+static const struct rb_data_type_struct item_rbtype = {
+    .wrap_struct_name = "Ncurses::Menu::ITEM",
+};
 static VALUE wrap_item(ITEM *item)
 {
   if (item == 0) return Qnil;
@@ -150,7 +153,7 @@ static VALUE wrap_item(ITEM *item)
 
 	 if (rb_item == Qnil)
 	 {
-		rb_item = Data_Wrap_Struct(cITEM, 0, 0, item);
+		rb_item = TypedData_Wrap_Struct(cITEM, &item_rbtype, item);
 		rb_iv_set(rb_item, "@destroyed", Qfalse);
 		rb_hash_aset(items_hash, item_address, rb_item);
 	 }
@@ -167,7 +170,7 @@ static ITEM *get_item(VALUE rb_item)
 	 rb_raise(rb_eRuntimeError, "Attempt to access a destroyed item");
 	 return 0;
   }
-  Data_Get_Struct(rb_item, ITEM, item);
+  TypedData_Get_Struct(rb_item, ITEM, &item_rbtype, item);
   return item;
 }
 
@@ -175,6 +178,9 @@ static ITEM *get_item(VALUE rb_item)
 /*
  * MENU wrapper
  */
+static const struct rb_data_type_struct menu_rbtype = {
+    .wrap_struct_name = "Ncurses::Menu::MENU",
+};
 static VALUE wrap_menu(MENU *menu)
 {
   if (menu == 0) return Qnil;
@@ -185,7 +191,7 @@ static VALUE wrap_menu(MENU *menu)
 
 	 if (rb_menu == Qnil)
 	 {
-		rb_menu = Data_Wrap_Struct(cMENU, 0, 0, menu);
+		rb_menu = TypedData_Wrap_Struct(cMENU, &menu_rbtype, menu);
 		rb_iv_set(rb_menu, "@destroyed", Qfalse);
 		rb_hash_aset(menus_hash, menu_address, rb_menu);
 	 }
@@ -202,7 +208,7 @@ static MENU *get_menu(VALUE rb_menu)
 	 rb_raise(rb_eRuntimeError, "Attempt to access a destroyed menu");
 	 return 0;
   }
-  Data_Get_Struct(rb_menu, MENU, menu);
+  TypedData_Get_Struct(rb_menu, MENU, &menu_rbtype, menu);
   return menu;
 }
 
diff --git a/ncurses_wrap.c b/ncurses_wrap.c
index 6adbc3b..eb0a9b7 100644
--- a/ncurses_wrap.c
+++ b/ncurses_wrap.c
@@ -223,6 +223,9 @@ long rbncurs_array_length(VALUE array)
   return NUM2LONG(rb_funcall(array, rb_intern("length"), 0));
 }
 
+static const struct rb_data_type_struct window_rbtype = {
+    .wrap_struct_name = "Ncurses::WINDOW",
+};
 VALUE   wrap_window(WINDOW* window)
 {
     if (window == 0) return Qnil;
@@ -231,7 +234,7 @@ VALUE   wrap_window(WINDOW* window)
         VALUE window_adress  = INT2NUM((long)(window));
         VALUE rb_window      = rb_hash_aref(windows_hash, window_adress);
         if (rb_window == Qnil) {
-            rb_window = Data_Wrap_Struct(cWINDOW, 0, 0, window);
+            rb_window = TypedData_Wrap_Struct(cWINDOW, &window_rbtype, window);
             rb_iv_set(rb_window, "@destroyed", Qfalse);
             rb_iv_set(rb_window, "@timeout", INT2FIX(-1));
             rb_hash_aset(windows_hash, window_adress, rb_window);
@@ -247,7 +250,7 @@ WINDOW* get_window(VALUE rb_window)
         rb_raise(eNcurses, "Attempt to access a destroyed window");
         return 0;
     }
-    Data_Get_Struct(rb_window, WINDOW, window);
+    TypedData_Get_Struct(rb_window, WINDOW, &window_rbtype, window);
     return window;
 }
 static VALUE rbncurs_delwin(VALUE dummy, VALUE arg1) {
@@ -259,6 +262,9 @@ static VALUE rbncurs_delwin(VALUE dummy, VALUE arg1) {
     return INT2NUM(delwin(window));
 }
 
+static const struct rb_data_type_struct screen_rbtype = {
+    .wrap_struct_name = "Ncurses::SCREEN",
+};
 static VALUE   wrap_screen(SCREEN* screen)
 {
     if (screen == 0) return Qnil;
@@ -267,7 +273,7 @@ static VALUE   wrap_screen(SCREEN* screen)
         VALUE screen_adress  = INT2NUM((long)(screen));
         VALUE rb_screen      = rb_hash_aref(screens_hash, screen_adress);
         if (rb_screen == Qnil) {
-            rb_screen = Data_Wrap_Struct(cSCREEN, 0, 0, screen);
+            rb_screen = TypedData_Wrap_Struct(cSCREEN, &screen_rbtype, screen);
             rb_iv_set(rb_screen, "@destroyed", Qfalse);
             rb_hash_aset(screens_hash, screen_adress, rb_screen);
         }
@@ -282,7 +288,7 @@ static SCREEN* get_screen(VALUE rb_screen)
         rb_raise(eNcurses, "Attempt to access a destroyed screen");
         return 0;
     }
-    Data_Get_Struct(rb_screen, SCREEN, screen);
+    TypedData_Get_Struct(rb_screen, SCREEN, &screen_rbtype, screen);
     return screen;
 }
 #ifdef HAVE_DELSCREEN
diff --git a/panel_wrap.c b/panel_wrap.c
index b9d67a7..6cf350b 100644
--- a/panel_wrap.c
+++ b/panel_wrap.c
@@ -29,6 +29,9 @@
 VALUE mPanel;
 VALUE cPANEL;
 
+static const struct rb_data_type_struct panel_rbtype = {
+    .wrap_struct_name = "Ncurses::Panel::PANEL",
+};
 static VALUE wrap_panel(PANEL* panel)
 {
     if (panel == 0) return Qnil;
@@ -37,7 +40,7 @@ static VALUE wrap_panel(PANEL* panel)
         VALUE panel_adress  = INT2NUM((long)(panel));
         VALUE rb_panel      = rb_hash_aref(panels_hash, panel_adress);
         if (rb_panel == Qnil) {
-            rb_panel = Data_Wrap_Struct(cPANEL, 0, 0, panel);
+            rb_panel = TypedData_Wrap_Struct(cPANEL, &panel_rbtype, panel);
             rb_iv_set(rb_panel, "@destroyed", Qfalse);
             rb_hash_aset(panels_hash, panel_adress, rb_panel);
         }
@@ -52,7 +55,7 @@ static PANEL* get_panel(VALUE rb_panel)
         rb_raise(rb_eRuntimeError, "Attempt to access a destroyed panel");
         return 0;
     }
-    Data_Get_Struct(rb_panel, PANEL, panel);
+    TypedData_Get_Struct(rb_panel, PANEL, &panel_rbtype, panel);
     return panel;
 }
 static VALUE rbncurs_c_del_panel(VALUE rb_panel) {

  parent reply	other threads:[~2026-10-17  0:24 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-17  0:24 [ncursesw-ruby.git] branch master updated (de96552 -> 4bf538c) Dan Callaghan
2026-10-17  0:24 ` [ncursesw-ruby.git] 01/10: fix segfault when tigetstr() returns -1 Dan Callaghan
2026-10-17  0:25 ` [ncursesw-ruby.git] 02/10: fix wrong argument passed to wenclose() Dan Callaghan
2026-10-17  0:25 ` [ncursesw-ruby.git] 03/10: fix wrong argument handling in mvwgetch() Dan Callaghan
2026-10-17  0:25 ` [ncursesw-ruby.git] 04/10: fix missing NULL checks for return values Dan Callaghan
2026-10-17  0:25 ` Dan Callaghan [this message]
2026-10-17  0:25 ` [ncursesw-ruby.git] 06/10: add GC mark function for PANEL to mark userptr Dan Callaghan
2026-10-17  0:25 ` [ncursesw-ruby.git] 07/10: fix add_wch() Dan Callaghan
2026-10-17  0:25 ` [ncursesw-ruby.git] 08/10: fix duplicate definition of pair_content method Dan Callaghan
2026-10-17  0:25 ` [ncursesw-ruby.git] 09/10: ensure definitions are only done once Dan Callaghan
2026-10-17  0:25 ` [ncursesw-ruby.git] 10/10: fix copy-paste mistake in name of FIELDTYPE wrapper Dan Callaghan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261017002459.1F0BF29BBCF@bl2.djc.id.au \
    --to=dan@bl2.djc.id.au \
    --cc=djc@djc.id.au \
    --cc=sup-commits@supmua.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox