Skip to content

mruby-task: compare names in C when Task.get walks the queues - #7834

Merged
matz merged 1 commit into
mruby:masterfrom
saddamr3e:task-get-name-compare
Nov 16, 2026
Merged

matz merged 1 commit into
mruby:masterfrom
saddamr3e:task-get-name-compare

Conversation

@saddamr3e

@saddamr3e saddamr3e commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor
  1. Task.get (mrb_task_s_get) compares each queued task's name with mrb_equal(), which calls the name's Ruby == for anything but a plain String. Task#name= stores any object, so that == runs mid-walk and can close the task being looked at; mrb_close_task() frees the mrb_task and the loop then reads t->next from freed memory.
  2. Repro: t = Task.new {}; o = Object.new; def o.==(x) = ($t.close; false); $t = t; t.name = o; Task.get("x"). ASan: heap-use-after-free READ in mrb_task_s_get (task.c:1275), freed by mrb_task_free via Task#close from mrb_equal. A plain full-core build segfaults once Strings allocated in the callback reuse the freed chunk.
  3. Names are now compared in C with mrb_str_equal(), skipping a task whose name is not a String, so no Ruby runs during the walk. Task.get only takes a String, so lookups of String-named tasks are unchanged. The other queue walks in task.c / task_queue.c call no Ruby.

Added a regression test to mrbgems/mruby-task/test/task.rb: it aborts the clang ASan mrbtest on master and passes with the fix. All seven ci/gcc-clang mrbtest targets and full-debug with -s are KO 0; the one crash in each (test/t/class.rb:536) is pre-existing on master.

Summary by CodeRabbit

  • Bug Fixes
    • Task lookups now match names by their string contents and do not invoke custom equality behavior on non-string names.
    • Looking up a different name no longer changes the state of a task with a custom name object; exact string-name lookups continue to work.

Task.get compared each task's name with mrb_equal(), which calls the
name's Ruby == for anything but a plain String. Task#name= takes any
object, so that == ran in the middle of the queue walk and could close
the task under it; the loop then read t->next from the freed mrb_task.

Compare with mrb_str_equal() and skip a task whose name is not a
String, so no Ruby runs during the walk.
@saddamr3e
saddamr3e requested a review from matz as a code owner November 16, 2026 16:20
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Task.get now skips task names that are not strings and compares string names with mrb_str_equal. A regression test checks that a lookup does not call == on a task name.

Changes: Task lookup

Layer / File(s) Summary
String name lookup and regression test
mrbgems/mruby-task/src/task.c, mrbgems/mruby-task/test/task.rb
mrb_task_s_get skips non-string task names and compares string names with mrb_str_equal. The test verifies that a nonmatching lookup leaves the task :READY and that an exact-name lookup returns it.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: Minimal · up to 2cba1

Task.get no longer runs Ruby equality code while walking task queues, which removes a potential use-after-free. Lookups by String name behave as before. Merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: Task.get in mruby-task now compares task names in C instead of dispatching Ruby ==. The raw summary confirms mrb_task_s_get matches only String names by byte equality,…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Task.get in mruby-task now compares task names in C instead of dispatching Ruby ==. The raw summary confirms mrb_task_s_get matches only String names by byte equality, and the test covers the queue-walk regression. The title is concise and specific, and it matches the PR objectives.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ce4811f5-72c8-4eb6-9f7f-73bf449d5199
Commits

Reviewing files that changed from the base of the PR and between 3a118cb and 2cba17a.

Files selected for processing: 2

  • mrbgems/mruby-task/src/task.c
  • mrbgems/mruby-task/test/task.rb

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@matz
matz merged commit 81fd905 into mruby:master Nov 16, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants