loongarch: Avoid undefined behavior when popping from an empty reloc stack
PATCH 1/1 loongarch: Avoid undefined behavior when popping from an empty reloc stack
The return value of grub_loongarch64_stack_pop is unsigned, so -1 should not be used in the first place. Replacing with 0 is enough to avoid the UB in this edge case.
Technically though, proper error handling is needed throughout the management of the reloc stack, so no unexpected behavior will happen even in case of malformed object code input (right now, pushes become no-ops when the stack is full, and garbage results if the stack does not contain enough operands for an op). The refactor would touch some more places so would be best done in a separate series.
--- grub-core/kern/loongarch64/dl_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/kern/loongarch64/dl_helper.c +1 -1
@@ -46,7 +46,7 @@ static grub_uint64_t grub_loongarch64_stack_pop (grub_loongarch64_stack_t stack) {if (stack->top ==-1 ) -return -1 ; +return 0 ;return stack->data[stack->top--]; }
Signature · 1 line
2.40.0
LGTM, thank you.
Quoted 37 lines · 在 2023-05-18星期四的 10:52 +0800,WANG Xuerui写道:
The return value of
grub_loongarch64_stack_popis unsigned, so -1 should
not be used in the first place. Replacing with 0 is enough to avoid the
UB in this edge case.Technically though, proper error handling is needed throughout the management of the reloc stack, so no unexpected behavior will happen even in case of malformed object code input (right now, pushes become no-ops when the stack is full, and garbage results if the stack does not
contain enough operands for an op). The refactor would touch some more
places so would be best done in a separate series.Fixes CID 407777Fixes CID 407778Signed-off-by WANG Xuerui <[email protected]>---
grub-core/kern/loongarch64/dl_helper.c | 2 +-1 file changed, 1 insertion(+), 1 deletion(-)grub- renamed from grub-core/kern/loongarch64/dl_helper.c +0
-0
core/kern/loongarch64/dl_helper.c
indexe8ec1219f..cda1a53c8100644grub-core/kern/loongarch64/dl_helper.c +1
-1@@ -46,7 +46,7 @@ static grub_uint64_t grub_loongarch64_stack_pop (grub_loongarch64_stack_t stack) { if (stack->top == -1) - return -1; + return 0; return stack->data[stack->top--]; }
Signature · 2 lines
Best Regards Xiaotian Wu
Quoted 11 lines · WANG Xuerui
The return value of
grub_loongarch64_stack_popis unsigned, so -1 should not be used in the first place. Replacing with 0 is enough to avoid the UB in this edge case.Technically though, proper error handling is needed throughout the management of the reloc stack, so no unexpected behavior will happen even in case of malformed object code input (right now, pushes become no-ops when the stack is full, and garbage results if the stack does not contain enough operands for an op). The refactor would touch some more places so would be best done in a separate series.
Quoted 4 lines
Fixes CID 407777Fixes CID 407778Signed-off-by WANG Xuerui <[email protected]>
Daniel