Project

General

Profile

Actions

Backport #6736

closed

Avoid misoptimization due to division by zero

Added by xi (Xi Wang) almost 12 years ago. Updated over 11 years ago.

Status:
Closed
[ruby-core:46486]

Description

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.


Files

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0