pylibfdt: Update the bytearray size with pack()

At present pack() calls fdt_pack() which may well reduce the size of the
device-tree data. However this does not currently update the size of the
bytearray to take account of any reduction. This means that there may be
unused data at the end of the bytearray and any users of as_bytearray()
will see this extra data.

Fix this by resizing the bytearray after packing.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
Simon Glass 2018-06-13 05:38:23 -06:00 committed by David Gibson
parent 3c374d46ac
commit 354d3dc559
2 changed files with 9 additions and 1 deletions

View File

@ -474,10 +474,17 @@ class Fdt:
Args:
quiet: Errors to ignore (empty to raise on all errors)

Returns:
Error code, or 0 if OK

Raises:
FdtException if any error occurs
"""
return check_err(fdt_pack(self._fdt), quiet)
err = check_err(fdt_pack(self._fdt), quiet)
if err:
return err
del self._fdt[self.totalsize():]
return err

def getprop(self, nodeoffset, prop_name, quiet=()):
"""Get a property from a node

View File

@ -308,6 +308,7 @@ class PyLibfdtTests(unittest.TestCase):
self.assertEquals(orig_size, self.fdt.totalsize())
self.assertEquals(self.fdt.pack(), 0)
self.assertTrue(self.fdt.totalsize() < orig_size)
self.assertEquals(self.fdt.totalsize(), len(self.fdt.as_bytearray()))

def testBadPropertyOffset(self):
"""Test that bad property offsets are detected"""