sup

A curses threads-with-tags style email client

ncursesw-ruby.git

git clone https://supmua.dev/git/ncursesw-ruby/
commit b240bb5ad794a86776d1803d66b49a03328ade67
parent b5d4f4515e051e0e800574a6f08a54b9108214ec
Author: Dan Callaghan <djc@djc.id.au>
Date:   Sat,  7 Nov 2026 19:13:18 +1100

fix use-after-free with ITEM name and description

Diffstat:
M menu_wrap.c | 21 +++++++++++++++++++--
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/menu_wrap.c b/menu_wrap.c
@@ -28,6 +28,7 @@
 
 #if defined(HAVE_MENU_H) || defined(HAVE_NCURSESW_MENU_H)
 
+#include <ruby/util.h>
 #include "menu_wrap.h"
 #include "ncurses_wrap.h"
 #include "compat.h"
@@ -379,16 +380,32 @@ static VALUE rbncurs_m_item_index(VALUE dummy, VALUE rb_item)
  * Item creation/destruction functions - mitem_new(3X) man page
  */
 static VALUE rbncurs_m_new_item(VALUE dummy, VALUE name, VALUE description)
-{ return wrap_item(new_item(STR2CSTR(name), STR2CSTR(description))); }
+{
+  char *c_name = ruby_strdup(STR2CSTR(name));
+  char *c_desc = ruby_strdup(STR2CSTR(description));
+  ITEM *item = new_item(c_name, c_desc);
+  if (item == NULL) { xfree(c_name); xfree(c_desc); return Qnil; }
+  if (item_description(item) != c_desc) {
+    /* ncurses rejected the description */
+    xfree(c_desc);
+  }
+  return wrap_item(item);
+}
 
 static VALUE rbncurs_c_free_item(VALUE rb_item)
 {
   VALUE items_hash   = rb_iv_get(mMenu, "@items_hash");
   ITEM *item       = get_item(rb_item);
   VALUE item_address  = INT2NUM((long)(item));
+  char *c_name = (char *)item_name(item);
+  char *c_desc = (char *)item_description(item);
+  int result = free_item(item);
+  if (result != E_OK) return INT2NUM(result);
+  xfree(c_name);
+  xfree(c_desc);
   rb_funcall(items_hash, rb_intern("delete"), 1, item_address);
   rb_iv_set(rb_item, "@destroyed", Qtrue);
-  return INT2NUM(free_item(item));
+  return INT2NUM(result);
 }
 static VALUE rbncurs_m_free_item(VALUE dummy, VALUE rb_item)
 { return rbncurs_c_free_item(rb_item); }