mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user