From 13d909bd3c22fb79fb7adf5825c49407bb810ae1 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 25 Jul 2022 08:11:25 +0900 Subject: [PATCH] array.c: new configuration MRB_ARY_LENGTH_MAX. The default value is 2**17 entries. If you want to avoid the limitation, set this value to 0. --- include/mrbconf.h | 5 +++++ src/array.c | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/include/mrbconf.h b/include/mrbconf.h index 77618ce25..0f8aba9f7 100644 --- a/include/mrbconf.h +++ b/include/mrbconf.h @@ -112,6 +112,11 @@ /* set this value to zero to skip the check */ //#define MRB_STR_LENGTH_MAX 1048576 +/* maximum length of arrays */ +/* the default value is 2**17 entries */ +/* set this value to zero to skip the check */ +//#define MRB_ARY_LENGTH_MAX 131072 + /* argv max size in mrb_funcall */ //#define MRB_FUNCALL_ARGC_MAX 16 diff --git a/src/array.c b/src/array.c index 227d8a734..8f2b04d20 100644 --- a/src/array.c +++ b/src/array.c @@ -17,6 +17,9 @@ #define ARY_DEFAULT_LEN 4 #define ARY_SHRINK_RATIO 5 /* must be larger than 2 */ #define ARY_C_MAX_SIZE (SIZE_MAX / sizeof(mrb_value)) +#ifndef MRB_ARY_LENGTH_MAX +#define MRB_ARY_LENGTH_MAX 131072 +#endif #define ARY_MAX_SIZE ((mrb_int)((ARY_C_MAX_SIZE < (size_t)MRB_INT_MAX) ? ARY_C_MAX_SIZE : MRB_INT_MAX-1)) static void @@ -30,6 +33,10 @@ ary_check_too_big(mrb_state *mrb, mrb_int a, mrb_int b) { if (a > ARY_MAX_SIZE - b || a < 0) ary_too_big(mrb); +#if MRB_ARY_LENGTH_MAX != 0 + if (a > MRB_ARY_LENGTH_MAX - b || a < 0) + ary_too_big(mrb); +#endif } static struct RArray*