From 07ce51c5aaf25a5a184a35074a40138256a0c099 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Fri, 17 Jan 2020 09:55:50 +0900 Subject: [PATCH] internal/rational.h: insert assertions in RATIONAL_SET_{NUM,DEN} --- internal/numeric.h | 12 ++++++++++++ internal/rational.h | 5 +++++ rational.c | 1 - 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/internal/numeric.h b/internal/numeric.h index 609b0c5fff..a5875fbbff 100644 --- a/internal/numeric.h +++ b/internal/numeric.h @@ -89,6 +89,7 @@ static inline double rb_float_flonum_value(VALUE v); static inline double rb_float_noflonum_value(VALUE v); static inline double rb_float_value_inline(VALUE v); static inline VALUE rb_float_new_inline(double d); +static inline bool INT_POSITIVE_P(VALUE num); static inline bool INT_NEGATIVE_P(VALUE num); static inline bool FLOAT_ZERO_P(VALUE num); #define rb_float_value rb_float_value_inline @@ -108,6 +109,17 @@ VALUE rb_float_eql(VALUE x, VALUE y); VALUE rb_fix_aref(VALUE fix, VALUE idx); MJIT_SYMBOL_EXPORT_END +static inline bool +INT_POSITIVE_P(VALUE num) +{ + if (FIXNUM_P(num)) { + return FIXNUM_POSITIVE_P(num); + } + else { + return BIGNUM_POSITIVE_P(num); + } +} + static inline bool INT_NEGATIVE_P(VALUE num) { diff --git a/internal/rational.h b/internal/rational.h index 028fc4c03e..d514050641 100644 --- a/internal/rational.h +++ b/internal/rational.h @@ -12,6 +12,8 @@ #include "ruby/config.h" /* for HAVE_LIBGMP */ #include "ruby/ruby.h" /* for struct RBasic */ #include "internal/gc.h" /* for RB_OBJ_WRITE */ +#include "internal/numeric.h" /* for INT_POSITIVE_P */ +#include "ruby_assert.h" /* for assert */ struct RRational { struct RBasic basic; @@ -51,12 +53,15 @@ RUBY_SYMBOL_EXPORT_END static inline void RATIONAL_SET_NUM(VALUE r, VALUE n) { + assert(RB_INTEGER_TYPE_P(n)); RB_OBJ_WRITE(r, &RRATIONAL(r)->num, n); } static inline void RATIONAL_SET_DEN(VALUE r, VALUE d) { + assert(RB_INTEGER_TYPE_P(d)); + assert(INT_POSITIVE_P(d)); RB_OBJ_WRITE(r, &RRATIONAL(r)->den, d); } diff --git a/rational.c b/rational.c index 0f9841667f..f483c6d68b 100644 --- a/rational.c +++ b/rational.c @@ -37,7 +37,6 @@ #define GMP_GCD_DIGITS 1 -#define INT_POSITIVE_P(x) (FIXNUM_P(x) ? FIXNUM_POSITIVE_P(x) : BIGNUM_POSITIVE_P(x)) #define INT_ZERO_P(x) (FIXNUM_P(x) ? FIXNUM_ZERO_P(x) : rb_bigzero_p(x)) VALUE rb_cRational;