Commit 01f36bc8

01f36bc8ec7aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 9 files · +4 added · ~5 modified

Description

Merge pull request #353 from ryanseys/rs/zero-division-error

fix(codegen,runtime): raise ZeroDivisionError on integer division by zero

Details

Commit
01f36bc8ec7a
Parent
2ff7ba46856e
Merge parent
64a699fda165

lib/mruby_shim.hmodifiedView file

@@ -41,13 +41,26 @@
4141 #define mrb_free(mrb, p) free(p)
4242 #define mrb_malloc_simple(mrb, sz) malloc(sz)
4343
44-/* Error handling */
45-#define mrb_raise(mrb, cls, msg) do { fprintf(stderr, "%s\n", msg); exit(1); } while(0)
46-#define mrb_int_zerodiv(mrb) do { fprintf(stderr, "divided by 0\n"); exit(1); } while(0)
47-#define E_RANGE_ERROR 0
48-#define E_RUNTIME_ERROR 0
49-#define E_ARGUMENT_ERROR 0
50-#define E_TYPE_ERROR 0
44+/* Error handling — sp_bigint.c is compiled as a separate TU and
45+ would otherwise hard-exit on internal errors (mini-gmp's zero-
46+ divisor check, allocation overflow, etc.). The mrb_raise macro
47+ now dispatches on the symbolic class enum: ZeroDivisionError is
48+ routed through sp_bigint_raise_zerodiv (defined non-static in
49+ sp_runtime.h, linked from gen.c) so it reaches spinel's longjmp
50+ rescue net and is catchable by `rescue ZeroDivisionError`.
51+ Other classes still hard-exit — they're internal invariants
52+ (allocation overflow) where graceful handling buys little. */
53+extern void sp_bigint_raise_zerodiv(const char *msg);
54+#define E_RANGE_ERROR 1
55+#define E_RUNTIME_ERROR 2
56+#define E_ARGUMENT_ERROR 3
57+#define E_TYPE_ERROR 4
58+#define E_ZERODIV_ERROR 5
59+#define mrb_raise(mrb, cls, msg) do { \
60+ if ((cls) == E_ZERODIV_ERROR) { sp_bigint_raise_zerodiv(msg); } \
61+ else { fprintf(stderr, "%s\n", msg); exit(1); } \
62+} while(0)
63+#define mrb_int_zerodiv(mrb) sp_bigint_raise_zerodiv("divided by 0")
5164
5265 /* mrb_value stub - tagged union */
5366 typedef struct mrb_value {