From 0f3c706336f237e693c2789ce5c4db7e350e938c Mon Sep 17 00:00:00 2001 From: Brenden Matthews Date: Thu, 4 Mar 2021 07:18:59 -0600 Subject: [PATCH] Replace temporary colour array with smart pointer. This should fix #1070. --- src/colours.cc | 8 ++++---- src/colours.h | 3 ++- src/conky.cc | 3 +-- src/hsv_gradient.cc | 10 ++++++---- src/hsv_gradient.h | 5 ++++- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/colours.cc b/src/colours.cc index 8c24426a..928f5771 100644 --- a/src/colours.cc +++ b/src/colours.cc @@ -85,15 +85,15 @@ unsigned int adjust_colours(unsigned int colour) { } /* this function returns the next colour between two colours for a gradient */ -unsigned long *do_gradient(int width, unsigned long first_colour, - unsigned long last_colour) { +std::unique_ptr do_gradient(int width, + unsigned long first_colour, + unsigned long last_colour) { int red1, green1, blue1; // first colour int red2, green2, blue2; // last colour int reddiff, greendiff, bluediff; // difference short redshift = (2 * colour_depth / 3 + colour_depth % 3); short greenshift = (colour_depth / 3); - auto *colours = - static_cast(malloc(width * sizeof(unsigned long))); + std::unique_ptr colours(new unsigned long[width]); int i; if (colour_depth == 0) { set_up_gradient(); } diff --git a/src/colours.h b/src/colours.h index b1aefb22..e943b8a9 100644 --- a/src/colours.h +++ b/src/colours.h @@ -29,10 +29,11 @@ #ifndef _COLOURS_H #define _COLOURS_H +#include #include unsigned int adjust_colours(unsigned int); -unsigned long *do_gradient(int, unsigned long, unsigned long); +std::unique_ptr do_gradient(int, unsigned long, unsigned long); long get_x11_color(const std::string &colour); // XXX: when everyone uses C++ strings, remove this C version diff --git a/src/conky.cc b/src/conky.cc index ebf722a4..b116b2be 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -1458,7 +1458,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) { /* in case we don't have a graph yet */ if (current->graph != nullptr) { - unsigned long *tmpcolour = nullptr; + std::unique_ptr tmpcolour; if (current->last_colour != 0 || current->first_colour != 0) { #ifdef BUILD_HSV_GRADIENT @@ -1492,7 +1492,6 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) { current->scale)); ++j; } - free_and_zero(tmpcolour); } if (h > cur_y_add && h > font_h) { cur_y_add = h; } if (show_graph_range.get(*state)) { diff --git a/src/hsv_gradient.cc b/src/hsv_gradient.cc index 8eab665f..38000339 100644 --- a/src/hsv_gradient.cc +++ b/src/hsv_gradient.cc @@ -143,16 +143,18 @@ void scaled_hsv_to_scaled_rgb(long *const hsv, long *rgb) { /* this function returns the next colour between two colours in hsv space for a * gradient */ -unsigned long *do_hsv_gradient(int width, unsigned long first_colour, - unsigned long last_colour) { +std::unique_ptr do_hsv_gradient(int width, + unsigned long first_colour, + unsigned long last_colour) { long rgb1[3], rgb2[3], rgb3[3]; long hsv1[3], hsv2[3]; long hueDiff, satDiff, valDiff; int redshift = (2 * colour_depth / 3 + colour_depth % 3); int greenshift = (colour_depth / 3); - unsigned long *colours = - static_cast(malloc(width * sizeof(unsigned long))); + std::unique_ptr colours(new unsigned long[width]); + // unsigned long *colours = + // static_cast(malloc(width * sizeof(unsigned long))); int i; if (colour_depth == 0) { set_up_gradient(); } diff --git a/src/hsv_gradient.h b/src/hsv_gradient.h index a2454789..9c624538 100644 --- a/src/hsv_gradient.h +++ b/src/hsv_gradient.h @@ -29,12 +29,15 @@ #ifndef _HSV_GRADIENT_H #define _HSV_GRADIENT_H +#include + // needed by hsv_gradient extern short colour_depth; extern long redmask, greenmask, bluemask; extern void set_up_gradient(); -unsigned long *do_hsv_gradient(int, unsigned long, unsigned long); +std::unique_ptr do_hsv_gradient(int, unsigned long, + unsigned long); long to_decimal_scale(long value, long max_value); long from_decimal_scale(long value, long max_value);