diff mbox

[v2,2/2] hw/openrisc: avoid undefined shift in openrisc_pic_cpu_handler()

Message ID 1376459728-2029-2-git-send-email-xi.wang@gmail.com
State New
Headers show

Commit Message

Xi Wang Aug. 14, 2013, 5:55 a.m. UTC
In C99 signed shift (1 << 31) is undefined behavior, since the result
exceeds INT_MAX.  Use 1U instead and move the shift after the check.

Cc: Jia Liu <proljc@gmail.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Xi Wang <xi.wang@gmail.com>
---
 hw/openrisc/pic_cpu.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Jia Liu Aug. 14, 2013, 1:17 p.m. UTC | #1
Hi Xi,

On Wed, Aug 14, 2013 at 1:55 PM, Xi Wang <xi.wang@gmail.com> wrote:
> In C99 signed shift (1 << 31) is undefined behavior, since the result
> exceeds INT_MAX.  Use 1U instead and move the shift after the check.
>
> Cc: Jia Liu <proljc@gmail.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Xi Wang <xi.wang@gmail.com>
> ---
>  hw/openrisc/pic_cpu.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/hw/openrisc/pic_cpu.c b/hw/openrisc/pic_cpu.c
> index 3fcee02..2af1d60 100644
> --- a/hw/openrisc/pic_cpu.c
> +++ b/hw/openrisc/pic_cpu.c
> @@ -26,12 +26,14 @@ static void openrisc_pic_cpu_handler(void *opaque, int irq, int level)
>  {
>      OpenRISCCPU *cpu = (OpenRISCCPU *)opaque;
>      CPUState *cs = CPU(cpu);
> -    uint32_t irq_bit = 1 << irq;
> +    uint32_t irq_bit;
>
>      if (irq > 31 || irq < 0) {
>          return;
>      }
>
> +    irq_bit = 1U << irq;
> +

Thanks for making the code better.

Acked-by: Jia Liu <proljc@gmail.com>

>      if (level) {
>          cpu->env.picsr |= irq_bit;
>      } else {
> --
> 1.8.1.2
>

Regards,
Jia
diff mbox

Patch

diff --git a/hw/openrisc/pic_cpu.c b/hw/openrisc/pic_cpu.c
index 3fcee02..2af1d60 100644
--- a/hw/openrisc/pic_cpu.c
+++ b/hw/openrisc/pic_cpu.c
@@ -26,12 +26,14 @@  static void openrisc_pic_cpu_handler(void *opaque, int irq, int level)
 {
     OpenRISCCPU *cpu = (OpenRISCCPU *)opaque;
     CPUState *cs = CPU(cpu);
-    uint32_t irq_bit = 1 << irq;
+    uint32_t irq_bit;
 
     if (irq > 31 || irq < 0) {
         return;
     }
 
+    irq_bit = 1U << irq;
+
     if (level) {
         cpu->env.picsr |= irq_bit;
     } else {