From fa2f0580327e65e59c0c37274fb0930867569b5a Mon Sep 17 00:00:00 2001 From: Zedhugh Chen Date: Tue, 9 Jun 2026 10:28:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(bar):=20=E4=BF=AE=E5=A4=8D=20bar=5Fcell=5Ft?= =?UTF-8?q?=20text=20=E5=AD=97=E6=AE=B5=E5=8F=AF=E8=83=BD=E4=B8=BA?= =?UTF-8?q?=E9=87=8E=E6=8C=87=E9=92=88=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bar/bar.c | 5 +++++ src/bar/cell.c | 6 ++++-- src/bar/types.h | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/bar/bar.c b/src/bar/bar.c index 19f9b44..e4443b8 100644 --- a/src/bar/bar.c +++ b/src/bar/bar.c @@ -33,6 +33,11 @@ static void bar_item_cleanup(zdwm_bar_item_t *item) { if (destroy_state) destroy_state(item->state); item->state = nullptr; + for (size_t i = 0; i < item->count; ++i) { + auto cell = &item->cells[i]; + p_delete(&cell->text); + } + p_delete(&item->cells); p_clear(item, 1); } diff --git a/src/bar/cell.c b/src/bar/cell.c index 90819f7..a0a09b4 100644 --- a/src/bar/cell.c +++ b/src/bar/cell.c @@ -68,9 +68,11 @@ void bar_cell_set_text(zdwm_bar_item_t *item, size_t index, const char *text) { if (index >= item->count) return; auto cell = &item->cells[index]; - if (cell->text == text || strcmp(cell->text, text) == 0) return; + if (cell->text && text && strcmp(cell->text, text) == 0) return; - cell->text = text; + p_delete(&cell->text); + + cell->text = p_strdup_nullable(text); cell->dirty = true; item->dirty = true; diff --git a/src/bar/types.h b/src/bar/types.h index 28ebe16..0ec5e41 100644 --- a/src/bar/types.h +++ b/src/bar/types.h @@ -14,7 +14,7 @@ typedef struct bar_x_region_t { typedef struct bar_cell_t { zdwm_icon_t icon; - const char *text; + char *text; /* 持有内存,避免野指针问题 */ const color_t *fg; const color_t *bg; bar_x_region_t region;