[PATCH] lib: sbi: Fix sign-extension in sbi_misaligned_load_handler()

Anup Patel anup at brainfault.org
Sun Dec 20 23:05:23 EST 2020


On Sun, Dec 20, 2020 at 10:02 AM Jessica Clarke <jrtc27 at jrtc27.com> wrote:
>
> > On 20 Dec 2020, at 04:26, Jessica Clarke <jrtc27 at jrtc27.com> wrote:
> >
> >> --- a/lib/sbi/sbi_misaligned_ldst.c
> >> +++ b/lib/sbi/sbi_misaligned_ldst.c
> >> @@ -128,7 +128,7 @@ int sbi_misaligned_load_handler(ulong addr, ulong tval2, ulong tinst,
> >>      }
> >>
> >>      if (!fp)
> >> -            SET_RD(insn, regs, val.data_ulong << shift >> shift);
> >> +            SET_RD(insn, regs, (long)val.data_ulong << shift >> shift);
> >
> > Left-shifting is undefined behaviour if it sets the sign bit, so a
> > standards-conforming C compiler is free to ignore the cast entirely
> > and assume the intermediate left-shifted value is non-negative.
>
> i.e. do the left shift and then cast, which reduces it (the cast, and
> right-shifting a potentially-negative number) to implementation-defined
> behaviour (that is two's complement arithmetic in the context of RISC-V
> compilers, as desired).

Sure, I will update.

Anyways we need braces to make this more readable.

Regards,
Anup



More information about the opensbi mailing list