[Bug gdb/34278] New: [gdb] DW_OP_mod should error on zero divisor, but GDB returns the dividend
wujielun402 at gmail dot com via Gdb-prs <[email protected]>
| Newsgroups | gmane.comp.gdb.bugs.discuss |
|---|---|
| Message-ID | <[email protected]/bugzilla/> |
https://sourceware.org/bugzilla/show_bug.cgi?id=34278
Bug ID: 34278
Summary: [gdb] DW_OP_mod should error on zero divisor, but GDB
returns the dividend
Product: gdb
Version: HEAD
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: gdb
Assignee: unassigned at sourceware dot org
Reporter: wujielun402 at gmail dot com
Target Milestone: ---
GDB currently appears to evaluate `DW_OP_mod` using the historical Knuth-style
definition in which `x mod 0` is defined as `x`.
For example, the following DWARF expression:
```text
DW_OP_const1u 42
DW_OP_lit0
DW_OP_mod
DW_OP_stack_value
```
evaluates successfully to 42.
This is visible directly in the implementation. `DW_OP_mod` is evaluated in
`gdb/dwarf2/expr.c` by dispatching to the generic `BINOP_MOD` operation. The
generic BINOP_MOD implementation in gdb/valarith.c then explicitly defines the
zero-divisor case as returning the dividend:
```c
case BINOP_MOD:
/* Knuth 1.2.4, integer only. Note that unlike the C '%' op,
v1 mod 0 has a defined value, v1. */
if (v2.sgn () == 0)
{
v = v1;
}
else
{
v = v1 / v2;
/* Note floor(v1/v2) == v1/v2 for unsigned. */
v = v1 - (v2 * v);
}
break;
```
So any DWARF expression evaluated through DW_OP_mod currently succeeds for a
zero divisor and returns the dividend.
This seems to preserve an older interpretation of modulo. DWARF v5 only says
that DW_OP_mod computes “former second stack entry modulo the former top of
the stack”, without defining the zero-divisor case.
The accepted [DWARF issue 250924.2](https://dwarfstd.org/issues/250924.2.html)
clarifies the intended semantics and explicitly says that the zero-divisor case
should error, like division.
Would it make sense for GDB's DWARF evaluator to report an error for
DW_OP_mod with a zero divisor?
If the legacy BINOP_MOD behavior is still needed elsewhere, the check could
be placed specifically in the DW_OP_mod evaluator. Otherwise, BINOP_MOD
could reject zero divisors similarly to the neighboring division/remainder
paths.
--
You are receiving this mail because:
You are on the CC list for the bug.