Issue #6736 has been updated by ktsj (Kazuki Tsujimoto).

Status changed from Closed to Assigned
Assignee set to naruse (Yui NARUSE)


----------------------------------------
Backport #6736: Avoid misoptimization due to division by zero
https://bugs.ruby-lang.org/issues/6736#change-28273

Author: xi (Xi Wang)
Status: Assigned
Priority: Normal
Assignee: naruse (Yui NARUSE)
Category: 
Target version: 


In time.c (quo) we have:

        if (b == 0) rb_num_zerodiv();
        c = a / b;

This code pattern is dangerous.  Some C compilers may move the division _before_ the check because:

1) It appears to the compiler that the division a / b is always reachable even with b == 0 (though rb_num_zerodiv invokes rb_raise internally).

2) Division by zero is undefined behavior.

This happened to PostgreSQL before.

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=616180

Attached patch marks rb_num_zerodiv as NORETURN to avoid this issue.


-- 
http://bugs.ruby-lang.org/