diff mbox

[4/8] hw/arm/omap1: Avoid unintended sign extension writing omap_rtc YEARS_REG

Message ID 1399574818-19349-5-git-send-email-peter.maydell@linaro.org
State Superseded
Headers show

Commit Message

Peter Maydell May 8, 2014, 6:46 p.m. UTC
When writing to the YEARS_REG register, if the year value is
99 then the multiplication by 31536000 will overflow into
the sign bit of a 32 bit value and then be erroneously
sign-extended if time_t is 64 bits. Add a cast to avoid this.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 hw/arm/omap1.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Peter Crosthwaite May 9, 2014, 11:45 p.m. UTC | #1
On Fri, May 9, 2014 at 4:46 AM, Peter Maydell <peter.maydell@linaro.org> wrote:
> When writing to the YEARS_REG register, if the year value is
> 99 then the multiplication by 31536000 will overflow into
> the sign bit of a 32 bit value and then be erroneously
> sign-extended if time_t is 64 bits. Add a cast to avoid this.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Reviewed-by: Peter Crosthwaite <peter.crosthwaite@xilinx.com>

> ---
>  hw/arm/omap1.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/hw/arm/omap1.c b/hw/arm/omap1.c
> index b433748..b28e052 100644
> --- a/hw/arm/omap1.c
> +++ b/hw/arm/omap1.c
> @@ -2709,8 +2709,8 @@ static void omap_rtc_write(void *opaque, hwaddr addr,
>              s->ti += ti[1];
>          } else {
>              /* A less accurate version */
> -            s->ti -= (s->current_tm.tm_year % 100) * 31536000;
> -            s->ti += from_bcd(value) * 31536000;
> +            s->ti -= (time_t)(s->current_tm.tm_year % 100) * 31536000;
> +            s->ti += (time_t)from_bcd(value) * 31536000;
>          }
>          return;
>
> --
> 1.9.2
>
>
diff mbox

Patch

diff --git a/hw/arm/omap1.c b/hw/arm/omap1.c
index b433748..b28e052 100644
--- a/hw/arm/omap1.c
+++ b/hw/arm/omap1.c
@@ -2709,8 +2709,8 @@  static void omap_rtc_write(void *opaque, hwaddr addr,
             s->ti += ti[1];
         } else {
             /* A less accurate version */
-            s->ti -= (s->current_tm.tm_year % 100) * 31536000;
-            s->ti += from_bcd(value) * 31536000;
+            s->ti -= (time_t)(s->current_tm.tm_year % 100) * 31536000;
+            s->ti += (time_t)from_bcd(value) * 31536000;
         }
         return;