Browse Source

Prevent crash on division by zero

Currently, attempting to divide by zero in an integer expression in a dts
file will cause dtc to crash with a division by zero (SIGFPE).

This patch corrects this to properly detect this case and raise an error.

Reported-by: Anton Blanchard <anton@samba.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
David Gibson 9 years ago
parent
commit
1937095588
  1. 10
      dtc-parser.y
  2. 5
      tests/division-by-zero.dts
  3. 2
      tests/run_tests.sh

10
dtc-parser.y

@ -410,7 +410,15 @@ integer_add: @@ -410,7 +410,15 @@ integer_add:

integer_mul:
integer_mul '*' integer_unary { $$ = $1 * $3; }
| integer_mul '/' integer_unary { $$ = $1 / $3; }
| integer_mul '/' integer_unary
{
if ($3 != 0) {
$$ = $1 / $3;
} else {
ERROR(&@$, "Division by zero");
$$ = 0;
}
}
| integer_mul '%' integer_unary { $$ = $1 % $3; }
| integer_unary
;

5
tests/division-by-zero.dts

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
/dts-v1/;

/ {
prop = < (1/0) >;
};

2
tests/run_tests.sh

@ -289,6 +289,8 @@ libfdt_tests () { @@ -289,6 +289,8 @@ libfdt_tests () {
run_test dtbs_equal_ordered embedded_nul.test.dtb embedded_nul_equiv.test.dtb

run_dtc_test -I dts -O dtb bad-size-cells.dts

run_wrap_error_test $DTC division-by-zero.dts
}

dtc_tests () {

Loading…
Cancel
Save