qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <f4bug@amsat.org>
To: Peter Maydell <peter.maydell@linaro.org>, qemu-devel@nongnu.org
Cc: Magnus Damm <magnus.damm@gmail.com>,
	Yoshinori Sato <ysato@users.sourceforge.jp>
Subject: Re: [PATCH 2/2] hw/timer/renesas_tmr: Fix use of uninitialized data in read_tcnt()
Date: Sat, 20 Feb 2021 00:13:19 +0100	[thread overview]
Message-ID: <426ce624-9bbf-d76b-e25b-53ebbabb5630@amsat.org> (raw)
In-Reply-To: <20210219223241.16344-3-peter.maydell@linaro.org>

On 2/19/21 11:32 PM, Peter Maydell wrote:
> The read_tcnt() function calculates the TCNT register values for the
> two channels of the timer module; it sets these up in the local
> tcnt[] array, and eventually returns either one or both of them,
> depending on whether the access is 8 or 16 bits.  However, not all of
> the code paths through this function set both elements of this array:
> if the guest has programmed the TCCR.CSS register fields to values
> which are either documented as not to be used or which QEMU does not
> implement, then the function will return uninitialized data.  (This
> was spotted by Coverity.)
> 
> Add the missing CSS cases to this code, so that we return a
> consistent value instead of uninitialized data, and so the code
> structure indicates what's happening.
> 
> Fixes: CID 1429976
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>  hw/timer/renesas_tmr.c | 19 +++++++++++++++----
>  1 file changed, 15 insertions(+), 4 deletions(-)
> 
> diff --git a/hw/timer/renesas_tmr.c b/hw/timer/renesas_tmr.c
> index 22260aaaba5..eed39917fec 100644
> --- a/hw/timer/renesas_tmr.c
> +++ b/hw/timer/renesas_tmr.c
> @@ -46,7 +46,9 @@ REG8(TCCR, 10)
>    FIELD(TCCR, CSS,   3, 2)
>    FIELD(TCCR, TMRIS, 7, 1)
>  
> +#define CSS_EXTERNAL  0x00
>  #define CSS_INTERNAL  0x01
> +#define CSS_INVALID   0x02
>  #define CSS_CASCADING 0x03
>  #define CCLR_A    0x01
>  #define CCLR_B    0x02
> @@ -130,13 +132,20 @@ static uint16_t read_tcnt(RTMRState *tmr, unsigned size, int ch)
>      if (delta > 0) {
>          tmr->tick = now;
>  
> -        if (FIELD_EX8(tmr->tccr[1], TCCR, CSS) == CSS_INTERNAL) {
> +        switch (FIELD_EX8(tmr->tccr[1], TCCR, CSS)) {
> +        case CSS_INTERNAL:
>              /* timer1 count update */
>              elapsed = elapsed_time(tmr, 1, delta);
>              if (elapsed >= 0x100) {
>                  ovf = elapsed >> 8;
>              }
>              tcnt[1] = tmr->tcnt[1] + (elapsed & 0xff);
> +            break;
> +        case CSS_INVALID: /* guest error to have set this */
> +        case CSS_EXTERNAL: /* QEMU doesn't implement these */
> +        case CSS_CASCADING:
> +            tcnt[1] = tmr->tcnt[1];
> +            break;
>          }
>          switch (FIELD_EX8(tmr->tccr[0], TCCR, CSS)) {
>          case CSS_INTERNAL:
> @@ -144,9 +153,11 @@ static uint16_t read_tcnt(RTMRState *tmr, unsigned size, int ch)
>              tcnt[0] = tmr->tcnt[0] + elapsed;
>              break;
>          case CSS_CASCADING:
> -            if (ovf > 0) {
> -                tcnt[0] = tmr->tcnt[0] + ovf;
> -            }
> +            tcnt[0] = tmr->tcnt[0] + ovf;
> +            break;
> +        case CSS_INVALID: /* guest error to have set this */
> +        case CSS_EXTERNAL: /* QEMU doesn't implement this */
> +            tcnt[0] = tmr->tcnt[0];
>              break;
>          }

Elegant nice fix :)

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>


      reply	other threads:[~2021-02-19 23:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-19 22:32 [PATCH 0/2] hw/timer/renesas_tmr: Fix use of uninitialized data Peter Maydell
2021-02-19 22:32 ` [PATCH 1/2] hw/timer/renesas_tmr: Prefix constants for CSS values with CSS_ Peter Maydell
2021-02-19 23:10   ` Philippe Mathieu-Daudé
2021-02-19 22:32 ` [PATCH 2/2] hw/timer/renesas_tmr: Fix use of uninitialized data in read_tcnt() Peter Maydell
2021-02-19 23:13   ` Philippe Mathieu-Daudé [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=426ce624-9bbf-d76b-e25b-53ebbabb5630@amsat.org \
    --to=f4bug@amsat.org \
    --cc=magnus.damm@gmail.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=ysato@users.sourceforge.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).