mrb_print_error: handle NULL gracefully to simplify error checking

made mrb_print_error() handle NULL by printing "Failed to allocate
mrb_state" when mrb is NULL. since mrb_close() already handles NULL,
this allows simplified error checking pattern:

  if (!MRB_OPEN_SUCCESS(mrb)) {
    mrb_print_error(mrb);  // handles NULL
    mrb_close(mrb);        // handles NULL
    return EXIT_FAILURE;
  }

updated all binary tools (mruby, mirb, mrdb, mrbtest) to use this
simplified pattern, removing nested if checks.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-11-14 00:49:55 +09:00
parent 05ffe0c441
commit 8e50a45f3e
5 changed files with 13 additions and 36 deletions
+2 -9
View File
@@ -674,15 +674,8 @@ main(int argc, char **argv)
l_restart:
if (!MRB_OPEN_SUCCESS(mrb)) {
if (mrb) {
/* Initialization failed - print exception details */
mrb_print_error(mrb);
mrb_close(mrb);
}
else {
/* Allocation failed */
fputs("Failed to allocate mrb_state, exiting mrdb\n", stderr);
}
mrb_print_error(mrb); /* handles NULL */
mrb_close(mrb); /* handles NULL */
return EXIT_FAILURE;
}
+2 -9
View File
@@ -476,15 +476,8 @@ main(int argc, char **argv)
/* new interpreter instance */
mrb = mrb_open();
if (!MRB_OPEN_SUCCESS(mrb)) {
if (mrb) {
/* Initialization failed - print exception details */
mrb_print_error(mrb);
mrb_close(mrb);
}
else {
/* Allocation failed */
fputs("Failed to allocate mrb_state, exiting mirb\n", stderr);
}
mrb_print_error(mrb); /* handles NULL */
mrb_close(mrb); /* handles NULL */
return EXIT_FAILURE;
}
+2 -9
View File
@@ -283,15 +283,8 @@ main(int argc, char **argv)
mrb_value v;
if (!MRB_OPEN_SUCCESS(mrb)) {
if (mrb) {
/* Initialization failed - print exception details */
mrb_print_error(mrb);
mrb_close(mrb);
}
else {
/* Allocation failed */
fprintf(stderr, "%s: Failed to allocate mrb_state, exiting mruby\n", *argv);
}
mrb_print_error(mrb); /* handles NULL */
mrb_close(mrb); /* handles NULL */
return EXIT_FAILURE;
}
+2 -9
View File
@@ -291,15 +291,8 @@ main(int argc, char **argv)
/* new interpreter instance */
mrb = mrb_open();
if (!MRB_OPEN_SUCCESS(mrb)) {
if (mrb) {
/* Initialization failed - print exception details */
mrb_print_error(mrb);
mrb_close(mrb);
}
else {
/* Allocation failed */
fputs("Failed to allocate mrb_state, exiting test driver", stderr);
}
mrb_print_error(mrb); /* handles NULL */
mrb_close(mrb); /* handles NULL */
return EXIT_FAILURE;
}
+5
View File
@@ -845,6 +845,11 @@ MRB_API void
mrb_print_error(mrb_state *mrb)
{
#ifndef MRB_NO_STDIO
if (!mrb) {
/* mrb_open() returned NULL - allocation failed */
fputs("Failed to allocate mrb_state\n", stderr);
return;
}
if (mrb->jmp == NULL) {
struct mrb_jmpbuf c_jmp;
MRB_TRY(&c_jmp) {