From 7037afb2bc01b216ce8760968381b5443c45c4da Mon Sep 17 00:00:00 2001 From: John Chen Date: Mon, 30 Jan 2017 10:42:24 +0800 Subject: [PATCH 1/4] Fix include. Add empty lines between include layers. --- src/dbus.c | 17 +++++++++-------- src/dbus.h | 2 -- src/dunst.c | 45 ++++++++++++++------------------------------- src/dunst.h | 1 - src/menu.c | 27 +++++++++++++++------------ src/menu.h | 3 --- src/notification.c | 28 +++++++++++++++------------- src/notification.h | 5 ++++- src/option_parser.c | 9 ++++----- src/option_parser.h | 1 + src/rules.c | 5 +++-- src/rules.h | 2 +- src/settings.c | 12 +++++++----- src/settings.h | 4 ++++ src/utils.c | 13 +++++-------- src/x.c | 34 +++++++++++++++++++--------------- src/x.h | 10 ++-------- 17 files changed, 103 insertions(+), 115 deletions(-) diff --git a/src/dbus.c b/src/dbus.c index 44aeaf1..a30413d 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -1,15 +1,16 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ - -#include -#include -#include -#include -#include -#include "dunst.h" #include "dbus.h" + +#include +#include +#include +#include +#include + +#include "dunst.h" #include "notification.h" -#include "utils.h" #include "settings.h" +#include "utils.h" GDBusConnection *dbus_conn; diff --git a/src/dbus.h b/src/dbus.h index 01af660..eb18c74 100644 --- a/src/dbus.h +++ b/src/dbus.h @@ -3,8 +3,6 @@ #ifndef DUNST_DBUS_H #define DUNST_DBUS_H -#include - #include "notification.h" int initdbus(void); diff --git a/src/dunst.c b/src/dunst.c index 49e37b0..ee963fc 100644 --- a/src/dunst.c +++ b/src/dunst.c @@ -3,41 +3,24 @@ #define _GNU_SOURCE #define XLIB_ILLEGAL_ACCESS -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#ifdef XINERAMA -#include -#endif -#include - #include "dunst.h" -#include "x.h" -#include "dbus.h" -#include "utils.h" -#include "rules.h" -#include "notification.h" -#include "menu.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dbus.h" +#include "menu.h" +#include "notification.h" #include "option_parser.h" #include "settings.h" +#include "x.h" #define LENGTH(X) (sizeof X / sizeof X[0]) diff --git a/src/dunst.h b/src/dunst.h index bc15127..5e65080 100644 --- a/src/dunst.h +++ b/src/dunst.h @@ -7,7 +7,6 @@ #include #include -#include "x.h" #include "notification.h" #define ERR(msg) printf("%s : %d\n", (msg), __LINE__) diff --git a/src/menu.c b/src/menu.c index a3319c1..963f016 100644 --- a/src/menu.c +++ b/src/menu.c @@ -1,20 +1,23 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ #define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "menu.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include -#include "dunst.h" -#include "utils.h" -#include "settings.h" #include "dbus.h" +#include "dunst.h" +#include "settings.h" +#include "notification.h" +#include "utils.h" static bool is_initialized = false; static regex_t cregex; diff --git a/src/menu.h b/src/menu.h index 43cd1e6..10fc045 100644 --- a/src/menu.h +++ b/src/menu.h @@ -2,9 +2,6 @@ #ifndef DUNST_MENU_H #define DUNST_MENU_H -#include "dunst.h" -#include - char *extract_urls(const char *to_match); void open_browser(const char *url); void invoke_action(const char *action); diff --git a/src/notification.c b/src/notification.c index 3df433a..0eef38d 100644 --- a/src/notification.c +++ b/src/notification.c @@ -1,24 +1,26 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ #define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include +#include "notification.h" + #include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include "dbus.h" -#include "x.h" -#include "notification.h" #include "dunst.h" -#include "utils.h" -#include "settings.h" -#include "rules.h" #include "menu.h" +#include "rules.h" +#include "settings.h" +#include "utils.h" +#include "x.h" int next_notification_id = 1; diff --git a/src/notification.h b/src/notification.h index 9c306a7..777ce73 100644 --- a/src/notification.h +++ b/src/notification.h @@ -2,7 +2,10 @@ #ifndef DUNST_NOTIFICATION_H #define DUNST_NOTIFICATION_H -#include "x.h" +#include +#include +#include + #include "settings.h" #define LOW 0 diff --git a/src/option_parser.c b/src/option_parser.c index 1486037..d915505 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -1,15 +1,14 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ #define _GNU_SOURCE +#include "option_parser.h" -#include -#include -#include +#include #include #include -#include +#include +#include -#include "option_parser.h" #include "utils.h" typedef struct _entry_t { diff --git a/src/option_parser.h b/src/option_parser.h index 394fa26..1f9728e 100644 --- a/src/option_parser.h +++ b/src/option_parser.h @@ -2,6 +2,7 @@ #ifndef DUNST_OPTION_PARSER_H #define DUNST_OPTION_PARSER_H +#include #include int load_ini_file(FILE *); diff --git a/src/rules.c b/src/rules.c index a3989b6..a40fc55 100644 --- a/src/rules.c +++ b/src/rules.c @@ -1,10 +1,11 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ -#include +#include "rules.h" + #include +#include #include "dunst.h" -#include "rules.h" /* * Apply rule to notification. diff --git a/src/rules.h b/src/rules.h index 4fad056..b9cbcea 100644 --- a/src/rules.h +++ b/src/rules.h @@ -3,8 +3,8 @@ #define DUNST_RULES_H #include +#include -#include "dunst.h" #include "notification.h" #include "settings.h" diff --git a/src/settings.c b/src/settings.c index f4876f6..7cc3666 100644 --- a/src/settings.c +++ b/src/settings.c @@ -1,19 +1,21 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ +#include "settings.h" + #include +#include #include #include - #ifndef STATIC_CONFIG #include #include #endif -#include "dunst.h" -#include "rules.h" -#include "option_parser.h" -#include "settings.h" +#include "rules.h" // put before config.h to fix missing include #include "config.h" +#include "dunst.h" +#include "notification.h" +#include "option_parser.h" #include "utils.h" settings_t settings; diff --git a/src/settings.h b/src/settings.h index eb386fd..160da14 100644 --- a/src/settings.h +++ b/src/settings.h @@ -2,6 +2,10 @@ #ifndef DUNST_SETTINGS_H #define DUNST_SETTINGS_H +#include + +#include "x.h" + enum alignment { left, center, right }; enum icon_position_t { icons_left, icons_right, icons_off }; enum separator_color { FOREGROUND, AUTO, FRAME, CUSTOM }; diff --git a/src/utils.c b/src/utils.c index 73ade27..1ff3d5a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,14 +1,11 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ #define _GNU_SOURCE - -#include -#include -#include -#include -#include - #include "utils.h" -#include "dunst.h" + +#include +#include +#include +#include char *string_replace_char(char needle, char replacement, char *haystack) { char *current = haystack; diff --git a/src/x.c b/src/x.c index 7a1c776..b37a788 100644 --- a/src/x.c +++ b/src/x.c @@ -1,26 +1,30 @@ /* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "x.h" + #include +#include #include -#include +#include +#include +#ifdef XINERAMA +#include +#include +#endif #include #include +#include +#include +#include +#include +#include +#include +#include +#include -#include "x.h" -#include "utils.h" #include "dunst.h" -#include "settings.h" #include "notification.h" +#include "settings.h" +#include "utils.h" #define WIDTH 400 #define HEIGHT 400 diff --git a/src/x.h b/src/x.h index 55e4b6e..b016cb3 100644 --- a/src/x.h +++ b/src/x.h @@ -2,16 +2,10 @@ #ifndef DUNST_X_H #define DUNST_X_H -#include -#include "glib.h" #include -#include -#include -#include -#ifdef XINERAMA -#include -#endif #include +#include +#include #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) #define FONT_HEIGHT_BORDER 2 From 17e2929ee754399954723f8f4dc0f09416daba9b Mon Sep 17 00:00:00 2001 From: John Chen Date: Mon, 30 Jan 2017 11:31:35 +0800 Subject: [PATCH 2/4] Change malloc and related functions to glib ones. * malloc -> g_malloc * realloc -> g_realloc * free -> g_free * strdup -> g_strdup * strndup -> g_strndup --- src/dbus.c | 11 ++++----- src/dunst.c | 7 +++--- src/menu.c | 18 +++++++------- src/notification.c | 60 ++++++++++++++++++++++----------------------- src/option_parser.c | 30 +++++++++++------------ src/settings.c | 15 ++++++------ src/utils.c | 6 ++--- src/x.c | 8 +++--- 8 files changed, 76 insertions(+), 79 deletions(-) diff --git a/src/dbus.c b/src/dbus.c index a30413d..04b69d0 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -5,7 +5,6 @@ #include #include #include -#include #include "dunst.h" #include "notification.h" @@ -132,7 +131,7 @@ static void on_notify(GDBusConnection * connection, gchar *icon = NULL; gchar *summary = NULL; gchar *body = NULL; - Actions *actions = malloc(sizeof(Actions)); + Actions *actions = g_malloc(sizeof(Actions)); if(actions == NULL) { die("Unable to allocate memory", EXIT_FAILURE); } @@ -309,13 +308,13 @@ static void on_notify(GDBusConnection * connection, n->progress = (progress < 0 || progress > 100) ? 0 : progress + 1; n->urgency = urgency; n->category = category; - n->dbus_client = strdup(sender); + n->dbus_client = g_strdup(sender); if (actions->count > 0) { n->actions = actions; } else { n->actions = NULL; g_strfreev(actions->actions); - free(actions); + g_free(actions); } for (int i = 0; i < ColLast; i++) { @@ -434,7 +433,7 @@ static void on_name_lost(GDBusConnection * connection, static RawImage * get_raw_image_from_data_hint(GVariant *icon_data) { - RawImage *image = malloc(sizeof(RawImage)); + RawImage *image = g_malloc(sizeof(RawImage)); GVariant *data_variant; gsize expected_len; @@ -456,7 +455,7 @@ static RawImage * get_raw_image_from_data_hint(GVariant *icon_data) " but got a " "length of %" G_GSIZE_FORMAT, expected_len, g_variant_get_size (data_variant)); - free(image); + g_free(image); return NULL; } diff --git a/src/dunst.c b/src/dunst.c index ee963fc..464736f 100644 --- a/src/dunst.c +++ b/src/dunst.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include "dbus.h" @@ -328,9 +327,9 @@ int dunst_main(int argc, char *argv[]) if (settings.startup_notification) { notification *n = notification_create(); - n->appname = strdup("dunst"); - n->summary = strdup("startup"); - n->body = strdup("dunst is up and running"); + n->appname = g_strdup("dunst"); + n->summary = g_strdup("startup"); + n->body = g_strdup("dunst is up and running"); n->progress = 0; n->timeout = 10; n->markup = MARKUP_NO; diff --git a/src/menu.c b/src/menu.c index 963f016..035d317 100644 --- a/src/menu.c +++ b/src/menu.c @@ -79,11 +79,11 @@ char *extract_urls(const char *to_match) start = m.rm_so + (p - to_match); finish = m.rm_eo + (p - to_match); - char *match = strndup(to_match + start, finish - start); + char *match = g_strndup(to_match + start, finish - start); urls = string_append(urls, match, "\n"); - free(match); + g_free(match); p += m.rm_eo; } @@ -162,7 +162,7 @@ void invoke_action(const char *action) */ void dispatch_menu_result(const char *input) { - char *in = strdup(input); + char *in = g_strdup(input); g_strstrip(in); switch (in[0]) { case '#': @@ -177,12 +177,12 @@ void dispatch_menu_result(const char *input) char *maybe_url = extract_urls(in); if (maybe_url) { open_browser(maybe_url); - free(maybe_url); + g_free(maybe_url); break; } } } - free(in); + g_free(in); } /* @@ -215,12 +215,12 @@ void context_menu(void) int parent_io[2]; if (pipe(child_io) != 0) { PERR("pipe()", errno); - free(dmenu_input); + g_free(dmenu_input); return; } if (pipe(parent_io) != 0) { PERR("pipe()", errno); - free(dmenu_input); + g_free(dmenu_input); return; } int pid = fork(); @@ -250,7 +250,7 @@ void context_menu(void) size_t len = read(parent_io[0], buf, 1023); if (len == 0) { - free(dmenu_input); + g_free(dmenu_input); return; } @@ -262,6 +262,6 @@ void context_menu(void) dispatch_menu_result(buf); - free(dmenu_input); + g_free(dmenu_input); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/src/notification.c b/src/notification.c index 0eef38d..45332c5 100644 --- a/src/notification.c +++ b/src/notification.c @@ -168,12 +168,12 @@ int notification_is_duplicate(const notification *a, const notification *b) void notification_free(notification * n) { assert(n != NULL); - free(n->appname); - free(n->summary); - free(n->body); - free(n->icon); - free(n->msg); - free(n->dbus_client); + g_free(n->appname); + g_free(n->summary); + g_free(n->body); + g_free(n->icon); + g_free(n->msg); + g_free(n->dbus_client); g_free(n->category); if (n->text_to_render) @@ -184,16 +184,16 @@ void notification_free(notification * n) if (n->actions) { g_strfreev(n->actions->actions); - free(n->actions->dmenu_str); + g_free(n->actions->dmenu_str); } if (n->raw_icon) { if (n->raw_icon->data) - free(n->raw_icon->data); - free(n->raw_icon); + g_free(n->raw_icon->data); + g_free(n->raw_icon); } - free(n); + g_free(n); } /* @@ -246,10 +246,10 @@ char *notification_replace_format(const char *needle, const char *replacement, char* ret; if (markup_mode == MARKUP_NO) { - tmp = strdup(replacement); + tmp = g_strdup(replacement); tmp = notification_quote_markup(tmp); } else { - tmp = strdup(replacement); + tmp = g_strdup(replacement); if (settings.ignore_newline) { tmp = string_replace_all("
", " ", tmp); tmp = string_replace_all("
", " ", tmp); @@ -273,7 +273,7 @@ char *notification_replace_format(const char *needle, const char *replacement, } ret = string_replace_all(needle, tmp, haystack); - free(tmp); + g_free(tmp); return ret; } @@ -286,7 +286,7 @@ char *notification_extract_markup_urls(char **str_ptr) { while ((start = strstr(str, ""); if (end != NULL) { - replace_buf = strndup(start, end - start + 1); + replace_buf = g_strndup(start, end - start + 1); url = extract_urls(replace_buf); if (url != NULL) { str = string_replace(replace_buf, "[", str); @@ -297,18 +297,18 @@ char *notification_extract_markup_urls(char **str_ptr) { } else { char *tmp = urls; urls = g_strconcat(tmp, "\n", index_buf, " ", url, NULL); - free(tmp); + g_free(tmp); } index_buf[0] = ' '; str = string_replace("", index_buf, str); - free(index_buf); - free(url); + g_free(index_buf); + g_free(url); } else { str = string_replace(replace_buf, "", str); str = string_replace("", "", str); } - free(replace_buf); + g_free(replace_buf); } else { break; } @@ -323,7 +323,7 @@ char *notification_extract_markup_urls(char **str_ptr) { */ notification *notification_create(void) { - notification *n = malloc(sizeof(notification)); + notification *n = g_malloc(sizeof(notification)); if(n == NULL) die("Unable to allocate memory", EXIT_FAILURE); memset(n, 0, sizeof(notification)); return n; @@ -398,12 +398,12 @@ int notification_init(notification * n, int id) n->msg = g_strchomp(n->msg); if (n->icon != NULL && strlen(n->icon) <= 0) { - free(n->icon); + g_free(n->icon); n->icon = NULL; } if (n->raw_icon == NULL && n->icon == NULL) { - n->icon = strdup(settings.icons[n->urgency]); + n->icon = g_strdup(settings.icons[n->urgency]); } if (id == 0) { @@ -432,8 +432,8 @@ int notification_init(notification * n, int id) /* notifications that differ only in progress hints should be expected equal, * but we want the latest message, with the latest hint value */ - free(orig->msg); - orig->msg = strdup(n->msg); + g_free(orig->msg); + orig->msg = g_strdup(n->msg); notification_free(n); wake_up(); return orig->id; @@ -447,8 +447,8 @@ int notification_init(notification * n, int id) /* notifications that differ only in progress hints should be expected equal, * but we want the latest message, with the latest hint value */ - free(orig->msg); - orig->msg = strdup(n->msg); + g_free(orig->msg); + orig->msg = g_strdup(n->msg); /* If the progress differs this was probably intended to replace the notification * but notify-send was used. So don't increment dup_count in this case */ @@ -506,7 +506,7 @@ int notification_init(notification * n, int id) if (tmp_urls != NULL) { if (n->urls != NULL) { n->urls = string_append(n->urls, tmp_urls, "\n"); - free(tmp_urls); + g_free(tmp_urls); } else { n->urls = tmp_urls; } @@ -522,12 +522,12 @@ int notification_init(notification * n, int id) char *act_str = g_strdup_printf("#%s [%s]", human_readable, n->appname); if (act_str) { n->actions->dmenu_str = string_append(n->actions->dmenu_str, act_str, "\n"); - free(act_str); + g_free(act_str); } } } - free(tmp); + g_free(tmp); if (settings.print_notifications) notification_print(n); @@ -590,7 +590,7 @@ int notification_close(notification * n, int reason) void notification_update_text_to_render(notification *n) { if (n->text_to_render) { - free(n->text_to_render); + g_free(n->text_to_render); n->text_to_render = NULL; } @@ -638,7 +638,7 @@ void notification_update_text_to_render(notification *n) new_buf = g_strdup_printf("%s (%ds old)", buf, seconds); } - free(buf); + g_free(buf); buf = new_buf; } diff --git a/src/option_parser.c b/src/option_parser.c index d915505..f6c43f4 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -48,7 +48,7 @@ section_t *new_section(char *name) } section_count++; - sections = realloc(sections, sizeof(section_t) * section_count); + sections = g_realloc(sections, sizeof(section_t) * section_count); if(sections == NULL) die("Unable to allocate memory.\n", 1); sections[section_count - 1].name = g_strdup(name); sections[section_count - 1].entries = NULL; @@ -60,13 +60,13 @@ void free_ini(void) { for (int i = 0; i < section_count; i++) { for (int j = 0; j < sections[i].entry_count; j++) { - free(sections[i].entries[j].key); - free(sections[i].entries[j].value); + g_free(sections[i].entries[j].key); + g_free(sections[i].entries[j].value); } - free(sections[i].entries); - free(sections[i].name); + g_free(sections[i].entries); + g_free(sections[i].name); } - free(sections); + g_free(sections); section_count = 0; sections = NULL; } @@ -90,7 +90,7 @@ void add_entry(char *section_name, char *key, char *value) s->entry_count++; int len = s->entry_count; - s->entries = realloc(s->entries, sizeof(entry_t) * len); + s->entries = g_realloc(s->entries, sizeof(entry_t) * len); s->entries[s->entry_count - 1].key = g_strdup(key); s->entries[s->entry_count - 1].value = clean_value(value); } @@ -234,7 +234,7 @@ int load_ini_file(FILE * fp) *end = '\0'; if (current_section) - free(current_section); + g_free(current_section); current_section = (g_strdup(start + 1)); new_section(current_section); continue; @@ -282,7 +282,7 @@ int load_ini_file(FILE * fp) } free(line); if (current_section) - free(current_section); + g_free(current_section); return 0; } @@ -308,7 +308,7 @@ int cmdline_find_option(char *key) /* look for first key */ for (int i = 0; i < cmdline_argc; i++) { if (strcmp(key1, cmdline_argv[i]) == 0) { - free(key1); + g_free(key1); return i; } } @@ -317,13 +317,13 @@ int cmdline_find_option(char *key) if (key2) { for (int i = 0; i < cmdline_argc; i++) { if (strcmp(key2, cmdline_argv[i]) == 0) { - free(key1); + g_free(key1); return i; } } } - free(key1); + g_free(key1); return -1; } @@ -466,16 +466,16 @@ void cmdline_usage_append(char *key, char *type, char *description) if (!usage_str) { usage_str = g_strdup_printf("%-40s - %s\n", key_type, description); - free(key_type); + g_free(key_type); return; } char *tmp; tmp = g_strdup_printf("%s%-40s - %s\n", usage_str, key_type, description); - free(key_type); + g_free(key_type); - free(usage_str); + g_free(usage_str); usage_str = tmp; } diff --git a/src/settings.c b/src/settings.c index 7cc3666..4fa2dad 100644 --- a/src/settings.c +++ b/src/settings.c @@ -4,7 +4,6 @@ #include #include -#include #include #ifndef STATIC_CONFIG #include @@ -66,7 +65,7 @@ static int ini_get_urgency(char *section, char *key, int def) urg); } if (urg) - free(urg); + g_free(urg); return ret; } @@ -114,7 +113,7 @@ void load_settings(char *cmdline_config_path) ); settings.markup = parse_markup_mode(c); - free(c); + g_free(c); } else if (ini_is_set("global", "allow_markup")) { bool allow_markup = option_get_bool( "global", @@ -180,7 +179,7 @@ void load_settings(char *cmdline_config_path) if (strlen(c) > 0) { parse_follow_mode(c); - free(c); + g_free(c); } } @@ -243,7 +242,7 @@ void load_settings(char *cmdline_config_path) else fprintf(stderr, "Warning: unknown alignment\n"); - free(c); + g_free(c); } } @@ -319,7 +318,7 @@ void load_settings(char *cmdline_config_path) settings.sep_color = CUSTOM; settings.sep_custom_color_str = g_strdup(c); } - free(c); + g_free(c); } } @@ -375,7 +374,7 @@ void load_settings(char *cmdline_config_path) else fprintf(stderr, "Warning: unknown icon position: %s\n", c); - free(c); + g_free(c); } } @@ -576,7 +575,7 @@ void load_settings(char *cmdline_config_path) if (strlen(c) > 0) { r->markup = parse_markup_mode(c); - free(c); + g_free(c); } } diff --git a/src/utils.c b/src/utils.c index 1ff3d5a..78ff3c9 100644 --- a/src/utils.c +++ b/src/utils.c @@ -26,7 +26,7 @@ char *string_replace_at(char *buf, int pos, int len, const char *repl) if (repl_len <= len) { tmp = buf; } else { - tmp = malloc(size); + tmp = g_malloc(size); } memcpy(tmp, buf, pos); @@ -34,7 +34,7 @@ char *string_replace_at(char *buf, int pos, int len, const char *repl) memmove(tmp + pos + repl_len, buf + pos + len, buf_len - (pos + len) + 1); if(tmp != buf) { - free(buf); + g_free(buf); } return tmp; @@ -84,7 +84,7 @@ char *string_append(char *a, const char *b, const char *sep) new = g_strconcat(a, b, NULL); else new = g_strconcat(a, sep, b, NULL); - free(a); + g_free(a); return new; diff --git a/src/x.c b/src/x.c index b37a788..766813c 100644 --- a/src/x.c +++ b/src/x.c @@ -336,14 +336,14 @@ static GdkPixbuf *get_pixbuf_from_path(char *icon_path) end = strchr(start, ':'); if (end == NULL) end = strchr(settings.icon_folders, '\0'); /* end = end of string */ - current_folder = strndup(start, end - start); + current_folder = g_strndup(start, end - start); /* try svg */ maybe_icon_path = g_strconcat(current_folder, "/", icon_path, ".svg", NULL); if (!does_file_exist(maybe_icon_path)) { /* fallback to png */ maybe_icon_path = g_strconcat(current_folder, "/", icon_path, ".png", NULL); } - free(current_folder); + g_free(current_folder); pixbuf = get_pixbuf_from_file(maybe_icon_path); g_free(maybe_icon_path); @@ -384,7 +384,7 @@ static GdkPixbuf *get_pixbuf_from_raw_image(const RawImage *raw_image) static colored_layout *r_init_shared(cairo_t *c, notification *n) { - colored_layout *cl = malloc(sizeof(colored_layout)); + colored_layout *cl = g_malloc(sizeof(colored_layout)); if(cl == NULL) { die("Unable to allocate memory", EXIT_FAILURE); } @@ -1418,7 +1418,7 @@ void x_shortcut_init(keyboard_shortcut * ks) ks->is_valid = true; } - free(str_begin); + g_free(str_begin); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ From 656846900a65d4ceed478f18914b3b5c8640cdb7 Mon Sep 17 00:00:00 2001 From: John Chen Date: Mon, 30 Jan 2017 11:36:15 +0800 Subject: [PATCH 3/4] Fix missing * in option_parser.c --- src/option_parser.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/option_parser.c b/src/option_parser.c index f6c43f4..8bcaee2 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -262,12 +262,10 @@ int load_ini_file(FILE * fp) printf("Missing '\"'\n"); continue; } - - closing_quote = '\0'; } else { char *comment = strpbrk(value, "#;"); if (comment) - comment = '\0'; + *comment = '\0'; } value = g_strstrip(value); From 8ba62dc78a932770b645a2f6cdc9da944353cc3f Mon Sep 17 00:00:00 2001 From: John Chen Date: Mon, 30 Jan 2017 12:05:41 +0800 Subject: [PATCH 4/4] Use g_malloc0 where appropriate; remove unneeded null check --- src/dbus.c | 9 +-------- src/notification.c | 5 +---- src/x.c | 3 --- 3 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/dbus.c b/src/dbus.c index 04b69d0..cd24158 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -131,10 +131,7 @@ static void on_notify(GDBusConnection * connection, gchar *icon = NULL; gchar *summary = NULL; gchar *body = NULL; - Actions *actions = g_malloc(sizeof(Actions)); - if(actions == NULL) { - die("Unable to allocate memory", EXIT_FAILURE); - } + Actions *actions = g_malloc0(sizeof(Actions)); gint timeout = -1; /* hints */ @@ -145,10 +142,6 @@ static void on_notify(GDBusConnection * connection, gchar *category = NULL; RawImage *raw_icon = NULL; - actions->actions = NULL; - actions->count = 0; - actions->dmenu_str = NULL; - { GVariantIter *iter = g_variant_iter_new(parameters); GVariant *content; diff --git a/src/notification.c b/src/notification.c index 45332c5..a08bd1d 100644 --- a/src/notification.c +++ b/src/notification.c @@ -323,10 +323,7 @@ char *notification_extract_markup_urls(char **str_ptr) { */ notification *notification_create(void) { - notification *n = g_malloc(sizeof(notification)); - if(n == NULL) die("Unable to allocate memory", EXIT_FAILURE); - memset(n, 0, sizeof(notification)); - return n; + return g_malloc0(sizeof(notification)); } void notification_init_defaults(notification *n) diff --git a/src/x.c b/src/x.c index 766813c..62dbce7 100644 --- a/src/x.c +++ b/src/x.c @@ -385,9 +385,6 @@ static GdkPixbuf *get_pixbuf_from_raw_image(const RawImage *raw_image) static colored_layout *r_init_shared(cairo_t *c, notification *n) { colored_layout *cl = g_malloc(sizeof(colored_layout)); - if(cl == NULL) { - die("Unable to allocate memory", EXIT_FAILURE); - } cl->l = pango_cairo_create_layout(c); if (!settings.word_wrap) {