hw/riscv/virt.c: Assemble plic_hart_config string with g_strjoinv()
commit33fcedfac8af376afad478f029cebb9ddb09f74a
authorPeter Maydell <peter.maydell@linaro.org>
Thu, 12 Aug 2021 14:46:47 +0000 (12 15:46 +0100)
committerAlistair Francis <alistair.francis@wdc.com>
Wed, 1 Sep 2021 01:59:12 +0000 (1 11:59 +1000)
treee7e912a27eb1715df145ef4f5e798ddb20698607
parent42109837b521e3a1f93e38f5058126a85ebc7f9f
hw/riscv/virt.c: Assemble plic_hart_config string with g_strjoinv()

In the riscv virt machine init function, We assemble a string
plic_hart_config which is a comma-separated list of N copies of the
VIRT_PLIC_HART_CONFIG string.  The code that does this has a
misunderstanding of the strncat() length argument.  If the source
string is too large strncat() will write a maximum of length+1 bytes
(length bytes from the source string plus a trailing NUL), but the
code here assumes that it will write only length bytes at most.

This isn't an actual bug because the code has correctly precalculated
the amount of memory it needs to allocate so that it will never be
too small (i.e.  we could have used plain old strcat()), but it does
mean that the code looks like it has a guard against accidental
overrun when it doesn't.

Rewrite the string handling here to use the glib g_strjoinv()
function, which means we don't need to do careful accountancy of
string lengths, and makes it clearer that what we're doing is
"create a comma-separated string".

Fixes: Coverity 1460752
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
Message-id: 20210812144647.10516-1-peter.maydell@linaro.org
Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
hw/riscv/virt.c