Skip to content

Commit

Permalink
Fix double free
Browse files Browse the repository at this point in the history
  • Loading branch information
bynect committed Aug 17, 2024
1 parent be4f1bd commit 1a82bc2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ void gradient_pattern(struct gradient *grad)
}
}

struct gradient *gradient_copy(const struct gradient *grad)
{
if (grad == NULL)
return NULL;

struct gradient *copy = gradient_alloc(grad->length);
memcpy(copy->colors, grad->colors, grad->length * sizeof(struct color));
gradient_pattern(copy);
return copy;
}

char *gradient_to_string(const struct gradient *grad)
{
if (!GRADIENT_VALID(grad)) return NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ void gradient_free(struct gradient *grad);

void gradient_pattern(struct gradient *grad);

struct gradient *gradient_copy(const struct gradient *grad);

char *gradient_to_string(const struct gradient *grad);


Expand Down
2 changes: 1 addition & 1 deletion src/notification.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ void notification_init(struct notification *n)
if (!COLOR_VALID(n->colors.bg)) n->colors.bg = defcolors.bg;
if (!COLOR_VALID(n->colors.frame)) n->colors.frame = defcolors.frame;

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

/* Sanitize misc hints */
if (n->progress < 0)
Expand Down
7 changes: 6 additions & 1 deletion test/dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,12 @@ TEST test_dbus_notify_colors(void)

// Invalid color strings are ignored
ASSERTm("Invalid color strings should not change the color struct", COLOR_SAME(n->colors.bg, settings.colors_norm.bg));
ASSERTm("Invalid color strings should not change the gradient struct", n->colors.highlight == settings.colors_norm.highlight);

ASSERTm("Invalid color strings should not change the gradient struct", n->colors.highlight->length == settings.colors_norm.highlight->length);

for (int i = 0; i < settings.colors_norm.highlight->length; i++)
ASSERTm("Invalid color strings should not change the gradient struct",
COLOR_SAME(n->colors.highlight->colors[i], settings.colors_norm.highlight->colors[i]));

dbus_notification_free(n_dbus);

Expand Down

0 comments on commit 1a82bc2

Please sign in to comment.