[rtems commit] libfdt: Allow #size-cells of 0

Sebastian Huber sebh at rtems.org
Mon Mar 2 06:52:33 UTC 2020


Module:    rtems
Branch:    master
Commit:    39f64e089c9bbc8e2f6140f8486b740d561c69c4
Changeset: http://git.rtems.org/rtems/commit/?id=39f64e089c9bbc8e2f6140f8486b740d561c69c4

Author:    David Gibson <david at gibson.dropbear.id.au>
Date:      Sun Jul 28 16:25:51 2019 +1000

libfdt: Allow #size-cells of 0

c12b2b0c20eb "libfdt: fdt_address_cells() and fdt_size_cells()" introduced
a bug as it consolidated code between the helpers for getting
#address-cells and #size-cells.  Specifically #size-cells is allowed to
be 0, and is frequently found so in practice for /cpus.  IEEE1275 only
requires implementations to handle 1..4 for #address-cells, although one
could make a case for #address-cells == #size-cells == 0 being used to
represent a bridge with a single port.

While we're there, it's not totally obvious that the existing implicit
cast of a u32 to int will give the correct results according to strict C,
although it does work in practice.  Straighten that up to cast only after
we've made our range checks.

Reported-by: yonghuhaige via https://github.com/dgibson/dtc/issues/28
Signed-off-by: David Gibson <david at gibson.dropbear.id.au>

---

 cpukit/dtc/libfdt/fdt_addresses.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/cpukit/dtc/libfdt/fdt_addresses.c b/cpukit/dtc/libfdt/fdt_addresses.c
index d8ba8ec..9a82cd0 100644
--- a/cpukit/dtc/libfdt/fdt_addresses.c
+++ b/cpukit/dtc/libfdt/fdt_addresses.c
@@ -14,7 +14,7 @@
 static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
 {
 	const fdt32_t *c;
-	int val;
+	uint32_t val;
 	int len;
 
 	c = fdt_getprop(fdt, nodeoffset, name, &len);
@@ -25,10 +25,10 @@ static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
 		return -FDT_ERR_BADNCELLS;
 
 	val = fdt32_to_cpu(*c);
-	if ((val <= 0) || (val > FDT_MAX_NCELLS))
+	if (val > FDT_MAX_NCELLS)
 		return -FDT_ERR_BADNCELLS;
 
-	return val;
+	return (int)val;
 }
 
 int fdt_address_cells(const void *fdt, int nodeoffset)
@@ -36,6 +36,8 @@ int fdt_address_cells(const void *fdt, int nodeoffset)
 	int val;
 
 	val = fdt_cells(fdt, nodeoffset, "#address-cells");
+	if (val == 0)
+		return -FDT_ERR_BADNCELLS;
 	if (val == -FDT_ERR_NOTFOUND)
 		return 2;
 	return val;



More information about the vc mailing list