]> git.mdlowis.com Git - proto/labwc.git/commitdiff
src/theme.c: fix misuse of wl_list_for_each
authortokyo4j <hrak1529@gmail.com>
Sun, 13 Oct 2024 22:55:15 +0000 (07:55 +0900)
committerJohan Malm <johanmalm@users.noreply.github.com>
Mon, 14 Oct 2024 19:02:26 +0000 (20:02 +0100)
The iterator value of `wl_list_for_each` should never be referenced
outside the loop because it points to invalid memory location when the
list is empty.

src/theme.c

index ae2ae4005bbdbf4d398a83681d948eae4569fb81..c0af2ad604f5759e087ac14b2a7ff85966dd3549 100644 (file)
@@ -297,29 +297,30 @@ load_button(struct theme *theme, struct button *b, int active)
                        buttons[b->type][non_hover_state_set]);
        }
 
-       struct title_button *leftmost_button = NULL;
+       /*
+        * If the loaded button is at the corner of the titlebar, also create
+        * rounded variants.
+        */
+       uint8_t rounded_state_set = b->state_set | LAB_BS_ROUNDED;
+
+       struct title_button *leftmost_button;
        wl_list_for_each(leftmost_button,
                        &rc.title_buttons_left, link) {
+               if (leftmost_button->type == b->type) {
+                       create_rounded_buffer(theme, LAB_CORNER_TOP_LEFT,
+                               &buttons[b->type][rounded_state_set], *buffer);
+               }
                break;
        }
-       struct title_button *rightmost_button = NULL;
+       struct title_button *rightmost_button;
        wl_list_for_each_reverse(rightmost_button,
                        &rc.title_buttons_right, link) {
+               if (rightmost_button->type == b->type) {
+                       create_rounded_buffer(theme, LAB_CORNER_TOP_RIGHT,
+                               &buttons[b->type][rounded_state_set], *buffer);
+               }
                break;
        }
-
-       /*
-        * If the loaded button is at the corner of the titlebar, also create
-        * rounded variants.
-        */
-       uint8_t rounded_state_set = b->state_set | LAB_BS_ROUNDED;
-       if (leftmost_button && leftmost_button->type == b->type) {
-               create_rounded_buffer(theme, LAB_CORNER_TOP_LEFT,
-                       &buttons[b->type][rounded_state_set], *buffer);
-       } else if (rightmost_button && rightmost_button->type == b->type) {
-               create_rounded_buffer(theme, LAB_CORNER_TOP_RIGHT,
-                       &buttons[b->type][rounded_state_set], *buffer);
-       }
 }
 
 /*