From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by bl2.djc.id.au (Postfix, from userid 1000) id 51F352CA78B; Sat, 07 Nov 2026 19:53:27 +1100 (AEDT) Date: Sat, 07 Nov 2026 19:53:28 +1100 To: sup-commits@supmua.dev Subject: [ncursesw-ruby.git] 01/06: fix use-after-free with ITEM name and description MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Reply-To: Dan Callaghan In-Reply-To: <179404160709.118121.923149093806097164@bl2.djc.id.au> References: <179404160709.118121.923149093806097164@bl2.djc.id.au> Thread-Index: AQAAEjRWwScN9KkGSxule9y9SuyvqA== X-Git-Host: bl2.djc.id.au X-Git-Repo: ncursesw-ruby.git X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Rev: b240bb5ad794a86776d1803d66b49a03328ade67 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.7.dev Auto-Submitted: auto-generated Message-Id: <20261107085327.51F352CA78B@bl2.djc.id.au> From: Dan Callaghan List-Id: New commit on branch master: https://supmua.dev/git/ncursesw-ruby/commit/b240bb5ad794a86776d1803d66b49a03328ade67.html commit b240bb5ad794a86776d1803d66b49a03328ade67 Author: Dan Callaghan AuthorDate: Sat Nov 7 19:13:18 2026 +1100 fix use-after-free with ITEM name and description --- menu_wrap.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/menu_wrap.c b/menu_wrap.c index 7c6a8ea..1f934db 100644 --- a/menu_wrap.c +++ b/menu_wrap.c @@ -28,6 +28,7 @@ #if defined(HAVE_MENU_H) || defined(HAVE_NCURSESW_MENU_H) +#include #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); }