From 19370955884ff0c49328956227c302225f4a014b Mon Sep 17 00:00:00 2001 From: David Gibson Date: Sun, 3 Jan 2016 22:27:32 +1100 Subject: [PATCH] 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 Signed-off-by: David Gibson --- dtc-parser.y | 10 +++++++++- tests/division-by-zero.dts | 5 +++++ tests/run_tests.sh | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/division-by-zero.dts 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_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 ; 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 @@ 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 () {