[PATCH] Handle fixed-point division by zero

Joel Brobecker brobecker@adacore.com
Sun Dec 13 14:01:36 GMT 2020


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


More information about the Gdb-patches mailing list