[binutils-gdb] Handle fixed-point division by zero

Tom Tromey tromey@sourceware.org
Mon Dec 14 14:40:45 GMT 2020


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=a3bdae4ef826f2b59cc475e530b2a4130185cfac

commit a3bdae4ef826f2b59cc475e530b2a4130185cfac
Author: Tom Tromey <tromey@adacore.com>
Date:   Mon Dec 14 07:35:45 2020 -0700

    Handle fixed-point division by zero
    
    fixed_point_binop did not account for division by zero.  This would
    lead to gdb getting SIGFPE and subsequently cause some test cases to
    hang.
    
    gdb/ChangeLog
    2020-12-14  Tom Tromey  <tromey@adacore.com>
    
            * valarith.c (fixed_point_binop): Call error on division by zero.
    
    gdb/testsuite/ChangeLog
    2020-12-14  Tom Tromey  <tromey@adacore.com>
    
            * gdb.dwarf2/dw2-fixed-point.exp: Add test for division by zero.

Diff:
---
 gdb/ChangeLog                                | 4 ++++
 gdb/testsuite/ChangeLog                      | 4 ++++
 gdb/testsuite/gdb.dwarf2/dw2-fixed-point.exp | 3 +++
 gdb/valarith.c                               | 2 ++
 4 files changed, 13 insertions(+)

diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index e6c572dc2f6..4ac0acc362c 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,7 @@
+2020-12-14  Tom Tromey  <tromey@adacore.com>
+
+	* valarith.c (fixed_point_binop): Call error on division by zero.
+
 2020-12-13  Tom Tromey  <tom@tromey.com>
 
 	* gdbtypes.c (safe_parse_type): Make argument const.
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
index c33f0b29251..dba92927a6b 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,7 @@
+2020-12-14  Tom Tromey  <tromey@adacore.com>
+
+	* gdb.dwarf2/dw2-fixed-point.exp: Add test for division by zero.
+
 2020-12-13  Tom de Vries  <tdevries@suse.de>
 
 	PR testsuite/26953
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-fixed-point.exp b/gdb/testsuite/gdb.dwarf2/dw2-fixed-point.exp
index 67d1d34d8f3..2c859d1fd16 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-fixed-point.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-fixed-point.exp
@@ -164,6 +164,9 @@ gdb_test "print pck.fp3_var * 1" \
 gdb_test "print pck.fp3_var / pck.fp3_var" \
          " = 1"
 
+gdb_test "print pck.fp3_var / 0" \
+         "Division by zero"
+
 gdb_test "print pck.fp1_range_var - 0.5" \
          " = 0.5"
 
diff --git a/gdb/valarith.c b/gdb/valarith.c
index 37988f1dfa7..6854d9b80f0 100644
--- a/gdb/valarith.c
+++ b/gdb/valarith.c
@@ -965,6 +965,8 @@ fixed_point_binop (struct value *arg1, struct value *arg2, enum exp_opcode op)
       break;
 
     case BINOP_DIV:
+      if (mpq_sgn (v2.val) == 0)
+	error (_("Division by zero"));
       mpq_div (res.val, v1.val, v2.val);
       val = fixed_point_to_value (res);
       break;


More information about the Gdb-cvs mailing list