diff --git a/ast.c b/ast.c index adb7287ed3..d60e5d3fcf 100644 --- a/ast.c +++ b/ast.c @@ -377,7 +377,7 @@ rest_arg(rb_ast_t *ast, const NODE *rest_arg) static VALUE node_children(rb_ast_t *ast, const NODE *node) { - char name[DECIMAL_SIZE_OF_BITS(sizeof(long) * CHAR_BIT) + 2]; /* including '$' */ + char name[sizeof("$") + DECIMAL_SIZE_OF(long)]; enum node_type type = nd_type(node); switch (type) { diff --git a/compile.c b/compile.c index 66dafe0169..41e908ecd0 100644 --- a/compile.c +++ b/compile.c @@ -8275,7 +8275,7 @@ compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NOD } else { # define BUILTIN_INLINE_PREFIX "_bi" - char inline_func[DECIMAL_SIZE_OF_BITS(sizeof(int) * CHAR_BIT) + sizeof(BUILTIN_INLINE_PREFIX)]; + char inline_func[sizeof(BUILTIN_INLINE_PREFIX) + DECIMAL_SIZE_OF(int)]; bool cconst = false; retry:; const struct rb_builtin_function *bf = iseq_builtin_function_lookup(iseq, builtin_func); diff --git a/include/ruby/util.h b/include/ruby/util.h index e8727a3200..ee11bc940a 100644 --- a/include/ruby/util.h +++ b/include/ruby/util.h @@ -36,6 +36,15 @@ RBIMPL_SYMBOL_EXPORT_BEGIN() /** an approximation of ceil(n * log10(2)), up to 65536 at least */ #define DECIMAL_SIZE_OF_BITS(n) (((n) * 3010 + 9998) / 9999) +/** an approximation of decimal representation size for n-bytes */ +#define DECIMAL_SIZE_OF_BYTES(n) DECIMAL_SIZE_OF_BITS((n) * CHAR_BIT) + +/** + * An approximation of decimal representation size. `expr` may be a + * type name + */ +#define DECIMAL_SIZE_OF(expr) DECIMAL_SIZE_OF_BYTES(sizeof(expr)) + /** * Character to number mapping like `'a'` -> `10`, `'b'` -> `11` etc. For * punctuation etc., the value is -1. "36" terminology comes from the fact