Skip to contentSkip to conversation

normal/charset: Fix underflow and overflow in loop init

PATCH In review Lidong Chen via Grub-devel 2 participants 3 messages Jun 18, 2025 – Jun 23, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 normal/charset: Fix underflow and overflow in loop init

In bidi_line_wrap(), "kk - 1" in the for loop init, "i = kk - 1", underflows when 'kk' (unsigned int) is 0. Assigning the result of 'kk - 1' to signed int 'i' may cause overflow. To address both issues, cast 'kk' to a signed type before subtraction to ensure safe arithmetic and assignment.

Fixed: CID 473874

---
 grub-core/normal/charset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/normal/charset.c +1 -1
index 4f6647116..a321c8438 100644
@@ -738,7 +738,7 @@ bidi_line_wrap (struct grub_unicode_glyph *visual_out,
 	  {
 	    int right_join = 0;
 	    signed i;
-	    for (i = kk - 1; i >= 0 && (unsigned) i + 1 > line_start;
+	    for (i = (signed) kk - 1; i >= 0 && (unsigned) i + 1 > line_start;
 		 i--)
 	      {
 		enum grub_join_type join_type = get_join_type (visual[i].base);
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
author Reviewed-by Daniel Kiper on v1 1/1
#1 lore raw
Quoted 9 lines · Lidong Chen

In bidi_line_wrap(), "kk - 1" in the for loop init, "i = kk - 1", underflows when 'kk' (unsigned int) is 0. Assigning the result of 'kk - 1' to signed int 'i' may cause overflow. To address both issues, cast 'kk' to a signed type before subtraction to ensure safe arithmetic and assignment.

Fixed: CID 473874

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

Reviewed-by on v1 1/1
#2 lore raw
In reply to Lidong Chen via Grub-devel
Quoted 26 lines · Lidong Chen via Grub-devel

In bidi_line_wrap(), "kk - 1" in the for loop init, "i = kk - 1", underflows when 'kk' (unsigned int) is 0. Assigning the result of 'kk - 1' to signed int 'i' may cause overflow. To address both issues, cast 'kk' to a signed type before subtraction to ensure safe arithmetic and assignment.

Fixed: CID 473874

---
 grub-core/normal/charset.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
grub-core/normal/charset.c +1 -1
index 4f6647116..a321c8438 100644
@@ -738,7 +738,7 @@ bidi_line_wrap (struct grub_unicode_glyph 
 *visual_out,
 	  {
 	    int right_join = 0;
 	    signed i;
-	    for (i = kk - 1; i >= 0 && (unsigned) i + 1 > line_start;
+	    for (i = (signed) kk - 1; i >= 0 && (unsigned) i + 1 > 
 line_start;
 		 i--)

grub-core/normal/charset.c bidi_line_wrap (struct grub_unicode_glyph *visual_out, · line 744

Quoted 2 lines
grub-core/normal/charset.c +0 -0
 	      {
 		enum grub_join_type join_type = get_join_type (visual[i].base);

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel