summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
author卜部昌平 <shyouhei@ruby-lang.org>2019-10-07 16:56:08 +0900
committer卜部昌平 <shyouhei@ruby-lang.org>2019-10-09 12:12:28 +0900
commit7e0ae1698d4db0baec858a46de8d1ae875360cf5 (patch)
tree646fbe720b13469679973060b8ab5299cf076236 /dir.c
parenta220410be70264a0e4089c4d63a9c22dd688ca7c (diff)
avoid overflow in integer multiplication
This changeset basically replaces `ruby_xmalloc(x * y)` into `ruby_xmalloc2(x, y)`. Some convenient functions are also provided for instance `rb_xmalloc_mul_add(x, y, z)` which allocates x * y + z byes.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/2540
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/dir.c b/dir.c
index d20cf60..e98fa25 100644
--- a/dir.c
+++ b/dir.c
@@ -1343,8 +1343,20 @@ sys_enc_warning_in(const char *func, const char *mesg, rb_encoding *enc)
#define sys_warning(val, enc) \
((flags & GLOB_VERBOSE) ? sys_enc_warning_in(RUBY_FUNCTION_NAME_STRING, (val), (enc)) :(void)0)
+static inline void *
+glob_alloc_n(size_t x, size_t y)
+{
+ size_t z;
+ if (rb_mul_size_overflow(x, y, SSIZE_MAX, &z)) {
+ rb_memerror(); /* or...? */
+ }
+ else {
+ return malloc(z);
+ }
+}
+
#define GLOB_ALLOC(type) ((type *)malloc(sizeof(type)))
-#define GLOB_ALLOC_N(type, n) ((type *)malloc(sizeof(type) * (n)))
+#define GLOB_ALLOC_N(type, n) ((type *)glob_alloc_n(sizeof(type), n))
#define GLOB_REALLOC(ptr, size) realloc((ptr), (size))
#define GLOB_FREE(ptr) free(ptr)
#define GLOB_JUMP_TAG(status) (((status) == -1) ? rb_memerror() : rb_jump_tag(status))