Instead of growing the buffer when reading a file by a factor of 2, try to allocate the whole size of the FDT once the size is known after having read the header. While the size is not known, use the existing strategy of doubling the buffer's size. Since the initial buffer size is 1024, which is greater than FDT_V1_SIZE (and any later version header sizes such as FDT_V17_SIZE), this reduces the number of allocations to two (one for the initial buffer and one additional one for the full size of the FDT. The total size of the buffer will be reduced as well by up to (fdt_totalsize / 2) - 1 bytes. Signed-off-by: Andreas Gnau <andreas.gnau@xxxxxxxxx> --- util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util.c b/util.c index 093303078d5f..c5873e91608e 100644 --- a/util.c +++ b/util.c @@ -263,7 +263,7 @@ int utilfdt_read_err(const char *filename, char **buffp, size_t *len) do { /* Expand the buffer to hold the next chunk */ if (offset == bufsize) { - bufsize *= 2; + bufsize = (totalsize != UINT32_MAX) ? totalsize : 2 * bufsize; buf = xrealloc(buf, bufsize); } -- 2.46.1