1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-25 04:06:03 +00:00

Fix goto in ncurses mode when compiled without BUILD_X11 (#680)

* Fix goto in ncurses mode when compiled without BUILD_X11

* Fix some issues raised by SonarCloud

* Define delete_block_and_zero() function and use it to replace some malloc calls
This commit is contained in:
livanh 2018-12-02 17:28:28 +01:00 committed by Brenden Matthews
parent f8ff46c2dc
commit 8d149471b5
2 changed files with 27 additions and 20 deletions

View File

@ -140,15 +140,15 @@
#ifdef BUILD_BUILTIN_CONFIG
#include "defconfig.h"
#ifdef BUILD_OLD_CONFIG
#include "convertconf.h"
#endif
namespace {
const char builtin_config_magic[] = "==builtin==";
} // namespace
#endif
#ifdef BUILD_OLD_CONFIG
#include "convertconf.h"
#endif
#ifndef S_ISSOCK
#define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
#endif
@ -704,7 +704,7 @@ int spaced_print(char *buf, int size, const char *format, int width, ...) {
char *tempbuf;
if (size < 1) { return 0; }
tempbuf = static_cast<char *>(malloc(size * sizeof(char)));
tempbuf = new char[size];
// Passes the varargs along to vsnprintf
va_start(argp, width);
@ -722,7 +722,7 @@ int spaced_print(char *buf, int size, const char *format, int width, ...) {
len = snprintf(buf, size, "%-*s", width, tempbuf);
break;
}
free(tempbuf);
delete [] tempbuf;
return len;
}
@ -806,9 +806,9 @@ long get_current_text_color() { return current_text_color; }
static void extract_variable_text(const char *p) {
free_text_objects(&global_root_object);
free_and_zero(tmpstring1);
free_and_zero(tmpstring2);
free_and_zero(text_buffer);
delete_block_and_zero(tmpstring1);
delete_block_and_zero(tmpstring2);
delete_block_and_zero(text_buffer);
extract_variable_text_internal(&global_root_object, p);
}
@ -846,7 +846,7 @@ void generate_text_internal(char *p, int p_max_size, struct text_object root) {
#ifdef BUILD_ICONV
char *buff_in;
buff_in = (char *)malloc(p_max_size);
buff_in = new char[p_max_size];
memset(buff_in, 0, p_max_size);
#endif /* BUILD_ICONV */
@ -888,7 +888,7 @@ void generate_text_internal(char *p, int p_max_size, struct text_object root) {
load_fonts(utf8_mode.get(*state));
#endif /* BUILD_X11 */
#ifdef BUILD_ICONV
free(buff_in);
delete [] buff_in;
#endif /* BUILD_ICONV */
}
@ -1648,9 +1648,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) {
char *tmp_str;
cur_x += font_ascent() / 2;
cur_y += font_h / 2;
const int tmp_str_len = 64;
tmp_str = static_cast<char *>(calloc(tmp_str_len, sizeof(char)));
sprintf(tmp_str, "%.1f", current->scale);
asprintf(&tmp_str, "%.1f", current->scale);
draw_string(tmp_str);
free(tmp_str);
cur_x = tmp_x;
@ -1749,6 +1747,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) {
if (draw_mode == BG) { cur_x++; }
#endif /* BUILD_X11 */
#ifdef BUILD_NCURSES
cur_x = static_cast<int>(current->arg);
if (out_to_ncurses.get(*state)) {
int x, y;
getyx(ncurses_window, y, x);
@ -2575,9 +2574,9 @@ void clean_up_without_threads(void *memtofree1, void *memtofree2) {
}
free_text_objects(&global_root_object);
free_and_zero(tmpstring1);
free_and_zero(tmpstring2);
free_and_zero(text_buffer);
delete_block_and_zero(tmpstring1);
delete_block_and_zero(tmpstring2);
delete_block_and_zero(text_buffer);
free_and_zero(global_text);
#ifdef BUILD_PORT_MONITORS
@ -3032,11 +3031,11 @@ void initialisation(int argc, char **argv) {
}
}
text_buffer = static_cast<char *>(malloc(max_user_text.get(*state)));
text_buffer = new char[max_user_text.get(*state)];
memset(text_buffer, 0, max_user_text.get(*state));
tmpstring1 = static_cast<char *>(malloc(text_buffer_size.get(*state)));
tmpstring1 = new char [text_buffer_size.get(*state)];
memset(tmpstring1, 0, text_buffer_size.get(*state));
tmpstring2 = static_cast<char *>(malloc(text_buffer_size.get(*state)));
tmpstring2 = new char [text_buffer_size.get(*state)];
memset(tmpstring2, 0, text_buffer_size.get(*state));
#ifdef BUILD_X11

View File

@ -358,6 +358,14 @@ void free_and_zero(T *&ptr) {
}
}
template <class T>
void delete_block_and_zero(T *&ptr) {
if (ptr) {
delete [] ptr;
ptr = nullptr;
}
}
extern std::unique_ptr<lua::state> state;
extern int argc_copy;