Project

General

Profile

Backport #6736

Avoid misoptimization due to division by zero

Added by xi (Xi Wang) about 7 years ago. Updated about 7 years ago.

Status:
Closed
Priority:
Normal
[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

Associated revisions

Revision 0501be6f
Added by kosaki (Motohiro KOSAKI) about 7 years ago

  • include/ruby/intern.h (rb_num_zerodiv): Added NORETURN. Patched by Xi Wang. [Bug #6736]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36454 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 36454
Added by kosaki (Motohiro KOSAKI) about 7 years ago

  • include/ruby/intern.h (rb_num_zerodiv): Added NORETURN. Patched by Xi Wang. [Bug #6736]

Revision 36454
Added by kosaki (Motohiro KOSAKI) about 7 years ago

  • include/ruby/intern.h (rb_num_zerodiv): Added NORETURN. Patched by Xi Wang. [Bug #6736]

Revision 36454
Added by kosaki (Motohiro KOSAKI) about 7 years ago

  • include/ruby/intern.h (rb_num_zerodiv): Added NORETURN. Patched by Xi Wang. [Bug #6736]

Revision 36454
Added by kosaki (Motohiro KOSAKI) about 7 years ago

  • include/ruby/intern.h (rb_num_zerodiv): Added NORETURN. Patched by Xi Wang. [Bug #6736]

Revision 36454
Added by kosaki (Motohiro KOSAKI) about 7 years ago

  • include/ruby/intern.h (rb_num_zerodiv): Added NORETURN. Patched by Xi Wang. [Bug #6736]

Revision 36454
Added by kosaki (Motohiro KOSAKI) about 7 years ago

  • include/ruby/intern.h (rb_num_zerodiv): Added NORETURN. Patched by Xi Wang. [Bug #6736]

Revision 2dc1a733
Added by naruse (Yui NARUSE) about 7 years ago

merge revision(s) 36454:

    * include/ruby/intern.h (rb_num_zerodiv): Added NORETURN.
      Patched by Xi Wang. [Bug #6736]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_3@36552 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 36552
Added by naruse (Yui NARUSE) about 7 years ago

merge revision(s) 36454:

* include/ruby/intern.h (rb_num_zerodiv): Added NORETURN.
  Patched by Xi Wang. [Bug #6736]

History

Updated by shyouhei (Shyouhei Urabe) about 7 years ago

The patch seems OK to me.

#2

Updated by kosaki (Motohiro KOSAKI) about 7 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r36454.
Xi, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


  • include/ruby/intern.h (rb_num_zerodiv): Added NORETURN. Patched by Xi Wang. [Bug #6736]
#3

Updated by kosaki (Motohiro KOSAKI) about 7 years ago

  • Tracker changed from Bug to Backport
  • Project changed from Ruby master to Backport193
  • Category deleted (core)
  • Target version deleted (2.0.0)

Committed. And I believe this should be backported into 1.9.x.

Updated by ktsj (Kazuki Tsujimoto) about 7 years ago

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

Updated by naruse (Yui NARUSE) about 7 years ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r36552.
Xi, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


merge revision(s) 36454:

* include/ruby/intern.h (rb_num_zerodiv): Added NORETURN.
  Patched by Xi Wang. [Bug #6736]

Also available in: Atom PDF