Skip to content

Commit 1a82bc2

Browse files
committed
Fix double free
1 parent be4f1bd commit 1a82bc2

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/draw.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ void gradient_pattern(struct gradient *grad)
115115
}
116116
}
117117

118+
struct gradient *gradient_copy(const struct gradient *grad)
119+
{
120+
if (grad == NULL)
121+
return NULL;
122+
123+
struct gradient *copy = gradient_alloc(grad->length);
124+
memcpy(copy->colors, grad->colors, grad->length * sizeof(struct color));
125+
gradient_pattern(copy);
126+
return copy;
127+
}
128+
118129
char *gradient_to_string(const struct gradient *grad)
119130
{
120131
if (!GRADIENT_VALID(grad)) return NULL;

src/draw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ void gradient_free(struct gradient *grad);
6767

6868
void gradient_pattern(struct gradient *grad);
6969

70+
struct gradient *gradient_copy(const struct gradient *grad);
71+
7072
char *gradient_to_string(const struct gradient *grad);
7173

7274

src/notification.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ void notification_init(struct notification *n)
513513
if (!COLOR_VALID(n->colors.bg)) n->colors.bg = defcolors.bg;
514514
if (!COLOR_VALID(n->colors.frame)) n->colors.frame = defcolors.frame;
515515

516-
if (!GRADIENT_VALID(n->colors.highlight)) n->colors.highlight = defcolors.highlight;
516+
if (!GRADIENT_VALID(n->colors.highlight)) n->colors.highlight = gradient_copy(defcolors.highlight);
517517

518518
/* Sanitize misc hints */
519519
if (n->progress < 0)

test/dbus.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,12 @@ TEST test_dbus_notify_colors(void)
760760

761761
// Invalid color strings are ignored
762762
ASSERTm("Invalid color strings should not change the color struct", COLOR_SAME(n->colors.bg, settings.colors_norm.bg));
763-
ASSERTm("Invalid color strings should not change the gradient struct", n->colors.highlight == settings.colors_norm.highlight);
763+
764+
ASSERTm("Invalid color strings should not change the gradient struct", n->colors.highlight->length == settings.colors_norm.highlight->length);
765+
766+
for (int i = 0; i < settings.colors_norm.highlight->length; i++)
767+
ASSERTm("Invalid color strings should not change the gradient struct",
768+
COLOR_SAME(n->colors.highlight->colors[i], settings.colors_norm.highlight->colors[i]));
764769

765770
dbus_notification_free(n_dbus);
766771

0 commit comments

Comments
 (0)