target/mips: Avoid shift by negative number in page_table_walk_refill()
authorPeter Maydell <peter.maydell@linaro.org>
Mon, 17 Jul 2023 16:29:40 +0000 (18:29 +0200)
committerPhilippe Mathieu-Daudé <philmd@linaro.org>
Tue, 25 Jul 2023 12:41:16 +0000 (14:41 +0200)
commit0fe4cac5dda1028c22ec3a6997e1b9155a768004
treea40fb65668656fd8720dddcddf9f6896102a6540
parent60a38a3a57befec24a768cbda811d224f1ab89dd
target/mips: Avoid shift by negative number in page_table_walk_refill()

Coverity points out that in page_table_walk_refill() we can
shift by a negative number, which is undefined behaviour
(CID 145291814529201452922).  We already catch the
negative directory_shift and leaf_shift as being a "bail
out early" case, but not until we've already used them to
calculated some offset values.

The shifts can be negative only if ptew > 1, so make the
bail-out-early check look directly at that, and only
calculate the shift amounts and the offsets based on them
after we have done that check. This allows
us to simplify the expressions used to calculate the
shift amounts, use an unsigned type, and avoids the
undefined behaviour.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
[PMD: Check for ptew > 1, use unsigned type]
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Message-Id: <20230717213504.24777-3-philmd@linaro.org>
target/mips/tcg/sysemu/tlb_helper.c