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

Jessica Clarke jrtc27 at jrtc27.com
Sat Dec 19 23:32:00 EST 2020


> 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).

Jess




More information about the opensbi mailing list