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>
diff --git a/dtc-parser.y b/dtc-parser.y
index 5a897e3..00d4dbb 100644
--- a/dtc-parser.y
+++ b/dtc-parser.y
@@ -410,7 +410,15 @@
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
;
diff --git a/tests/division-by-zero.dts b/tests/division-by-zero.dts
new file mode 100644
index 0000000..d26fc27
--- /dev/null
+++ b/tests/division-by-zero.dts
@@ -0,0 +1,5 @@
+/dts-v1/;
+
+/ {
+ prop = < (1/0) >;
+};
diff --git a/tests/run_tests.sh b/tests/run_tests.sh
index 1063d1e..3d6a230 100755
--- a/tests/run_tests.sh
+++ b/tests/run_tests.sh
@@ -289,6 +289,8 @@
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 () {