@@ -408,17 +408,18 @@ typedef uint128_t DSIZE_T;
408408/* *
409409 * @private
410410 *
411- * This is an implementation detail of rbimpl_size_mul_overflow().
411+ * This is an implementation detail of rbimpl_size_mul_overflow() and
412+ * rbimpl_size_add_overflow().
412413 *
413414 * @internal
414415 *
415416 * Expecting this struct to be eliminated by function inlinings. This is
416417 * nothing more than std::variant<std::size_t> if we could use recent C++, but
417418 * reality is we cannot.
418419 */
419- struct rbimpl_size_mul_overflow_tag {
420- bool left; /* *< Whether overflow happened or not. */
421- size_t right ; /* *< Multiplication result. */
420+ struct rbimpl_size_overflow_tag {
421+ bool overflowed; /* *< Whether overflow happened or not. */
422+ size_t result ; /* *< Calculation result. */
422423};
423424
424425RBIMPL_SYMBOL_EXPORT_BEGIN ()
@@ -572,46 +573,46 @@ RBIMPL_ATTR_CONST()
572573 *
573574 * @param[in] x Arbitrary value.
574575 * @param[in] y Arbitrary value.
575- * @return `{ left, right }`, where `left ` is whether there is an integer
576- * overflow or not, and `right` is a (possibly overflowed) result
577- * of `x` * `y`.
576+ * @return `{ overflowed, result }`, where `overflowed ` is whether there is
577+ * an integer overflow or not, and `result` is a (possibly
578+ * overflowed) result of `x` * `y`.
578579 *
579580 * @internal
580581 *
581582 * This is in fact also an implementation detail of ruby_xmalloc2() etc.
582583 */
583- static inline struct rbimpl_size_mul_overflow_tag
584+ static inline struct rbimpl_size_overflow_tag
584585rbimpl_size_mul_overflow(size_t x, size_t y)
585586{
586- struct rbimpl_size_mul_overflow_tag ret = { false , 0 , };
587+ struct rbimpl_size_overflow_tag ret = { false , 0 , };
587588
588589#if defined(ckd_mul)
589- ret.left = ckd_mul (&ret.right , x, y);
590+ ret.overflowed = ckd_mul (&ret.result , x, y);
590591
591592#elif RBIMPL_HAS_BUILTIN(__builtin_mul_overflow)
592- ret.left = __builtin_mul_overflow (x, y, &ret.right );
593+ ret.overflowed = __builtin_mul_overflow (x, y, &ret.result );
593594
594595#elif defined(DSIZE_T)
595596 RB_GNUC_EXTENSION DSIZE_T dx = x;
596597 RB_GNUC_EXTENSION DSIZE_T dy = y;
597598 RB_GNUC_EXTENSION DSIZE_T dz = dx * dy;
598- ret.left = dz > SIZE_MAX;
599- ret.right = RBIMPL_CAST ((size_t )dz);
599+ ret.overflowed = dz > SIZE_MAX;
600+ ret.result = RBIMPL_CAST ((size_t )dz);
600601
601602#elif defined(_MSC_VER) && defined(_M_AMD64)
602603 unsigned __int64 dp = 0 ;
603604 unsigned __int64 dz = _umul128 (x, y, &dp);
604- ret.left = RBIMPL_CAST ((bool )dp);
605- ret.right = RBIMPL_CAST ((size_t )dz);
605+ ret.overflowed = RBIMPL_CAST ((bool )dp);
606+ ret.result = RBIMPL_CAST ((size_t )dz);
606607
607608#elif defined(_MSC_VER) && defined(_M_ARM64)
608- ret.left = __umulh (x, y) != 0 ;
609- ret.right = x * y;
609+ ret.overflowed = __umulh (x, y) != 0 ;
610+ ret.result = x * y;
610611
611612#else
612613 /* https://wiki.sei.cmu.edu/confluence/display/c/INT30-C.+Ensure+that+unsigned+integer+operations+do+not+wrap */
613- ret.left = (y != 0 ) && (x > SIZE_MAX / y);
614- ret.right = x * y;
614+ ret.overflowed = (y != 0 ) && (x > SIZE_MAX / y);
615+ ret.result = x * y;
615616#endif
616617
617618 return ret;
@@ -635,11 +636,11 @@ rbimpl_size_mul_overflow(size_t x, size_t y)
635636static inline size_t
636637rbimpl_size_mul_or_raise (size_t x, size_t y)
637638{
638- struct rbimpl_size_mul_overflow_tag size =
639+ struct rbimpl_size_overflow_tag size =
639640 rbimpl_size_mul_overflow (x, y);
640641
641- if (RB_LIKELY (! size.left )) {
642- return size.right ;
642+ if (RB_LIKELY (! size.overflowed )) {
643+ return size.result ;
643644 }
644645 else {
645646 ruby_malloc_size_overflow (x, y);
@@ -662,33 +663,33 @@ RBIMPL_ATTR_CONST()
662663 *
663664 * @param[in] x Arbitrary value.
664665 * @param[in] y Arbitrary value.
665- * @return `{ left, right }`, where `left ` is whether there is an integer
666- * overflow or not, and `right` is a (possibly overflowed) result
667- * of `x` + `y`.
666+ * @return `{ overflowed, result }`, where `overflowed ` is whether there is
667+ * an integer overflow or not, and `result` is a (possibly
668+ * overflowed) result of `x` + `y`.
668669 *
669670 * @internal
670671 */
671- static inline struct rbimpl_size_mul_overflow_tag
672+ static inline struct rbimpl_size_overflow_tag
672673rbimpl_size_add_overflow(size_t x, size_t y)
673674{
674- struct rbimpl_size_mul_overflow_tag ret = { false , 0 , };
675+ struct rbimpl_size_overflow_tag ret = { false , 0 , };
675676
676677#if defined(ckd_add)
677- ret.left = ckd_add (&ret.right , x, y);
678+ ret.overflowed = ckd_add (&ret.result , x, y);
678679
679680#elif RBIMPL_HAS_BUILTIN(__builtin_add_overflow)
680- ret.left = __builtin_add_overflow (x, y, &ret.right );
681+ ret.overflowed = __builtin_add_overflow (x, y, &ret.result );
681682
682683#elif defined(DSIZE_T)
683684 RB_GNUC_EXTENSION DSIZE_T dx = x;
684685 RB_GNUC_EXTENSION DSIZE_T dy = y;
685686 RB_GNUC_EXTENSION DSIZE_T dz = dx + dy;
686- ret.left = dz > SIZE_MAX;
687- ret.right = (size_t )dz;
687+ ret.overflowed = dz > SIZE_MAX;
688+ ret.result = (size_t )dz;
688689
689690#else
690- ret.right = x + y;
691- ret.left = ret.right < y;
691+ ret.result = x + y;
692+ ret.overflowed = ret.result < y;
692693
693694#endif
694695
@@ -710,11 +711,11 @@ rbimpl_size_add_overflow(size_t x, size_t y)
710711static inline size_t
711712rbimpl_size_add_or_raise (size_t x, size_t y)
712713{
713- struct rbimpl_size_mul_overflow_tag size =
714+ struct rbimpl_size_overflow_tag size =
714715 rbimpl_size_add_overflow (x, y);
715716
716- if (RB_LIKELY (!size.left )) {
717- return size.right ;
717+ if (RB_LIKELY (!size.overflowed )) {
718+ return size.result ;
718719 }
719720 else {
720721 ruby_malloc_add_size_overflow (x, y);
0 commit comments