* [PATCH] Handle fixed-point division by zero
@ 2020-12-11 15:15 Tom Tromey
2020-12-13 14:01 ` Joel Brobecker
0 siblings, 1 reply; 2+ messages in thread
From: Tom Tromey @ 2020-12-11 15:15 UTC (permalink / raw)
To: gdb-patches; +Cc: Tom Tromey
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-11 Tom Tromey <tromey@adacore.com>
* valarith.c (fixed_point_binop): Call error on division by zero.
gdb/testsuite/ChangeLog
2020-12-11 Tom Tromey <tromey@adacore.com>
* gdb.dwarf2/dw2-fixed-point.exp: Add test for division by zero.
---
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/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;
--
2.26.2
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] Handle fixed-point division by zero
2020-12-11 15:15 [PATCH] Handle fixed-point division by zero Tom Tromey
@ 2020-12-13 14:01 ` Joel Brobecker
0 siblings, 0 replies; 2+ messages in thread
From: Joel Brobecker @ 2020-12-13 14:01 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
Hi Tom,
On Fri, Dec 11, 2020 at 08:15:48AM -0700, Tom Tromey wrote:
> 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-11 Tom Tromey <tromey@adacore.com>
>
> * valarith.c (fixed_point_binop): Call error on division by zero.
>
> gdb/testsuite/ChangeLog
> 2020-12-11 Tom Tromey <tromey@adacore.com>
>
> * gdb.dwarf2/dw2-fixed-point.exp: Add test for division by zero.
Thanks for the patch. It looks good to me.
(sooo obvious in hindsight...)
> ---
> 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/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;
> --
> 2.26.2
--
Joel
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2020-12-13 14:01 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-11 15:15 [PATCH] Handle fixed-point division by zero Tom Tromey
2020-12-13 14:01 ` Joel Brobecker
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).