Skip to content

Commit

Permalink
dtc: Warning rather than error on possible truncation of cell values
Browse files Browse the repository at this point in the history
We always evaluate integer values in cell arrays as 64-bit quantities, then
truncate to the size of the array cells (32-bit by default).  However to
detect accidental truncation of meaningful values, we give an error if the
truncated portion isn't either all 0 or all 1 bits.  However, this can
still give counterintuitive errors.  For if the user is thinking in 2's
complement 32-bit arithmetic (which would be quite natural), then they'd
expect the expression (-0xffffffff-2) to evaluate to -1 (0xffffffff).
However in 64-bit it evaluates to 0xfffffffeffffffff which does truncate
to the expected value but trips this error message.

Because of this reduce the error to only a warnings, with a somewhat more
helpful message.

Fixes: #74

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
  • Loading branch information
dgibson committed Nov 21, 2022
1 parent 55778a0 commit 2cd89f8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
11 changes: 8 additions & 3 deletions dtc-parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,14 @@ arrayprefix:
* within the mask to one (i.e. | in the
* mask), all bits are one.
*/
if (($2 > mask) && (($2 | mask) != -1ULL))
ERROR(&@2, "Value out of range for"
" %d-bit array element", $1.bits);
if (($2 > mask) && (($2 | mask) != -1ULL)) {
char *loc = srcpos_string(&@2);
fprintf(stderr,
"WARNING: %s: Value 0x%016" PRIx64
" truncated to 0x%0*" PRIx64 "\n",
loc, $2, $1.bits / 4, ($2 & mask));
free(loc);
}
}

$$.data = data_append_integer($1.data, $2, $1.bits);
Expand Down
7 changes: 7 additions & 0 deletions tests/cell-overflow-results.dts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/dts-v1/;

/ {
prop1 = < 0 >;
prop2 = < 0xffffffff >;
prop3 = < 0 >;
};
7 changes: 7 additions & 0 deletions tests/cell-overflow.dts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/dts-v1/;

/ {
prop1 = < (-0xffffffff - 1) >;
prop2 = < (-0xffffffff - 2) >;
prop3 = < ((-0xffffffff - 1) * 2) >;
};
5 changes: 5 additions & 0 deletions tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,11 @@ libfdt_tests () {
## https://github.com/dgibson/dtc/issues/64
check_tests "$SRCDIR/phandle-args-overflow.dts" clocks_property

## https://github.com/dgibson/dtc/issues/74
run_dtc_test -I dts -O dtb -o cell-overflow-results.test.dtb cell-overflow-results.dts
run_dtc_test -I dts -O dtb -o cell-overflow.test.dtb cell-overflow.dts
run_test dtbs_equal_ordered cell-overflow.test.dtb cell-overflow-results.test.dtb

# check full tests
for good in test_tree1.dtb; do
run_test check_full $good
Expand Down

0 comments on commit 2cd89f8

Please sign in to comment.