Skip to content

Commit 28d80ce

Browse files
committed
Use automatic cleanup where possible
1 parent 8a05490 commit 28d80ce

File tree

14 files changed

+148
-302
lines changed

14 files changed

+148
-302
lines changed

zathura/callbacks.c

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,11 @@ void cb_page_layout_value_changed(girara_session_t* session, const char* name, g
307307
girara_setting_get(session, "pages-per-row", &pages_per_row);
308308

309309
/* get list of first_page_column settings */
310-
char* first_page_column_list = NULL;
310+
g_autofree char* first_page_column_list = NULL;
311311
girara_setting_get(session, "first-page-column", &first_page_column_list);
312312

313313
/* find value for first_page_column */
314314
unsigned int first_page_column = find_first_page_column(first_page_column_list, pages_per_row);
315-
g_free(first_page_column_list);
316315

317316
unsigned int page_v_padding = 1;
318317
girara_setting_get(zathura->ui.session, "page-v-padding", &page_v_padding);
@@ -367,7 +366,7 @@ static gboolean handle_link(GtkEntry* entry, girara_session_t* session, zathura_
367366
zathura_t* zathura = session->global.data;
368367
gboolean eval = TRUE;
369368

370-
char* input = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
369+
g_autofree char* input = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
371370
if (input == NULL || strlen(input) == 0) {
372371
eval = FALSE;
373372
}
@@ -409,20 +408,17 @@ static gboolean handle_link(GtkEntry* entry, girara_session_t* session, zathura_
409408
zathura_link_display(zathura, link);
410409
break;
411410
case ZATHURA_LINK_ACTION_COPY: {
412-
GdkAtom* selection = get_selection(zathura);
411+
g_autofree GdkAtom* selection = get_selection(zathura);
413412
if (selection == NULL) {
414413
break;
415414
}
416415

417416
zathura_link_copy(zathura, link, selection);
418-
g_free(selection);
419417
break;
420418
}
421419
}
422420
}
423421

424-
g_free(input);
425-
426422
return eval;
427423
}
428424

@@ -608,7 +604,7 @@ void cb_page_widget_text_selected(ZathuraPage* page, const char* text, void* dat
608604
return;
609605
}
610606

611-
GdkAtom* selection = get_selection(zathura);
607+
g_autofree GdkAtom* selection = get_selection(zathura);
612608
if (selection == NULL) {
613609
return;
614610
}
@@ -620,19 +616,15 @@ void cb_page_widget_text_selected(ZathuraPage* page, const char* text, void* dat
620616
girara_setting_get(zathura->ui.session, "selection-notification", &notification);
621617

622618
if (notification == true) {
623-
char* target = NULL;
619+
g_autofree char* target = NULL;
624620
girara_setting_get(zathura->ui.session, "selection-clipboard", &target);
625621

626-
char* stripped_text = g_strdelimit(g_strdup(text), "\n\t\r\n", ' ');
627-
char* escaped_text = g_markup_printf_escaped(_("Copied selected text to selection %s: %s"), target, stripped_text);
628-
g_free(target);
629-
g_free(stripped_text);
622+
g_autofree char* stripped_text = g_strdelimit(g_strdup(text), "\n\t\r\n", ' ');
623+
g_autofree char* escaped_text =
624+
g_markup_printf_escaped(_("Copied selected text to selection %s: %s"), target, stripped_text);
630625

631626
girara_notify(zathura->ui.session, GIRARA_INFO, "%s", escaped_text);
632-
g_free(escaped_text);
633627
}
634-
635-
g_free(selection);
636628
}
637629

638630
void cb_page_widget_image_selected(ZathuraPage* page, GdkPixbuf* pixbuf, void* data) {
@@ -641,7 +633,7 @@ void cb_page_widget_image_selected(ZathuraPage* page, GdkPixbuf* pixbuf, void* d
641633
g_return_if_fail(data != NULL);
642634

643635
zathura_t* zathura = data;
644-
GdkAtom* selection = get_selection(zathura);
636+
g_autofree GdkAtom* selection = get_selection(zathura);
645637

646638
if (selection != NULL) {
647639
gtk_clipboard_set_image(gtk_clipboard_get(*selection), pixbuf);
@@ -650,16 +642,13 @@ void cb_page_widget_image_selected(ZathuraPage* page, GdkPixbuf* pixbuf, void* d
650642
girara_setting_get(zathura->ui.session, "selection-notification", &notification);
651643

652644
if (notification == true) {
653-
char* target = NULL;
645+
g_autofree char* target = NULL;
654646
girara_setting_get(zathura->ui.session, "selection-clipboard", &target);
655647

656-
char* escaped_text = g_markup_printf_escaped(_("Copied selected image to selection %s"), target);
657-
g_free(target);
648+
g_autofree char* escaped_text = g_markup_printf_escaped(_("Copied selected image to selection %s"), target);
658649

659650
girara_notify(zathura->ui.session, GIRARA_INFO, "%s", escaped_text);
660651
}
661-
662-
g_free(selection);
663652
}
664653
}
665654

@@ -698,16 +687,14 @@ void cb_page_widget_scaled_button_release(ZathuraPage* page_widget, GdkEventButt
698687
zathura_dbus_edit(zathura, zathura_page_get_index(page), event->x, event->y);
699688
}
700689

701-
char* editor = NULL;
690+
g_autofree char* editor = NULL;
702691
girara_setting_get(zathura->ui.session, "synctex-editor-command", &editor);
703692
if (editor == NULL || *editor == '\0') {
704693
girara_debug("No SyncTeX editor specified.");
705-
g_free(editor);
706694
return;
707695
}
708696

709697
synctex_edit(zathura, editor, page, event->x, event->y);
710-
g_free(editor);
711698
}
712699
}
713700

zathura/commands.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ bool cmd_bookmark_list(girara_session_t* session, girara_list_t* GIRARA_UNUSED(a
100100
return false;
101101
}
102102

103-
GString* string = g_string_new(NULL);
103+
g_autoptr(GString) string = g_string_new(NULL);
104104
for (size_t idx = 0; idx != girara_list_size(zathura->bookmarks.bookmarks); ++idx) {
105105
zathura_bookmark_t* bookmark = girara_list_nth(zathura->bookmarks.bookmarks, idx);
106106
g_string_append_printf(string, "<b>%s</b>: %u\n", bookmark->id, bookmark->page);
@@ -113,7 +113,6 @@ bool cmd_bookmark_list(girara_session_t* session, girara_list_t* GIRARA_UNUSED(a
113113
girara_notify(session, GIRARA_INFO, _("No bookmarks available."));
114114
}
115115

116-
g_string_free(string, TRUE);
117116
return true;
118117
}
119118

@@ -181,7 +180,7 @@ bool cmd_jumplist_list(girara_session_t* session, girara_list_t* argument_list)
181180
}
182181

183182
zathura_jump_t* current_jump = zathura_jumplist_current(zathura);
184-
GString* string = g_string_new(NULL);
183+
g_autoptr(GString) string = g_string_new(NULL);
185184
for (int i = zathura->jumplist.size - 1; i >= 0 && num_entries > 0; --i, --num_entries) {
186185
const zathura_jump_t* j = girara_list_nth(zathura->jumplist.list, i);
187186
g_string_append_printf(string, _("[%d]: page=<b>%2d</b>, x=%f, y=%f %s\n"), i, j->page + 1, j->x, j->y,
@@ -195,7 +194,6 @@ bool cmd_jumplist_list(girara_session_t* session, girara_list_t* argument_list)
195194
girara_notify(session, GIRARA_INFO, _("No jumplist available."));
196195
}
197196

198-
g_string_free(string, TRUE);
199197
return true;
200198
}
201199

@@ -245,8 +243,7 @@ bool cmd_info(girara_session_t* session, girara_list_t* UNUSED(argument_list)) {
245243
return false;
246244
}
247245

248-
GString* string = g_string_new(NULL);
249-
246+
g_autoptr(GString) string = g_string_new(NULL);
250247
for (size_t i = 0; i < LENGTH(meta_fields); i++) {
251248
for (size_t idx = 0; idx != girara_list_size(information); ++idx) {
252249
zathura_document_information_entry_t* entry = girara_list_nth(information, idx);
@@ -263,8 +260,6 @@ bool cmd_info(girara_session_t* session, girara_list_t* UNUSED(argument_list)) {
263260
girara_notify(session, GIRARA_INFO, _("No information available."));
264261
}
265262

266-
g_string_free(string, TRUE);
267-
268263
return false;
269264
}
270265

@@ -604,12 +599,10 @@ bool cmd_exec(girara_session_t* session, girara_list_t* argument_list) {
604599
g_ascii_dtostr(page_buf, G_ASCII_DTOSTR_BUF_SIZE, page + 1);
605600

606601
for (size_t idx = 0; idx != girara_list_size(argument_list); ++idx) {
607-
char* value = girara_list_nth(argument_list, idx);
608-
char* r = girara_replace_substring(value, "$PAGE", page_buf);
602+
char* value = girara_list_nth(argument_list, idx);
603+
g_autofree char* r = girara_replace_substring(value, "$PAGE", page_buf);
609604
if (r != NULL) {
610605
char* s = girara_replace_substring(r, "$FILE", path);
611-
g_free(r);
612-
613606
if (s != NULL) {
614607
girara_list_set_nth(argument_list, idx, s);
615608
}
@@ -656,16 +649,13 @@ bool cmd_version(girara_session_t* session, girara_list_t* UNUSED(argument_list)
656649
g_return_val_if_fail(session->global.data != NULL, false);
657650
zathura_t* zathura = session->global.data;
658651

659-
char* string = zathura_get_version_string(zathura->plugins.manager, true);
652+
g_autofree char* string = zathura_get_version_string(zathura->plugins.manager, true);
660653
if (string == NULL) {
661654
return false;
662655
}
663656

664657
/* display information */
665658
girara_notify(session, GIRARA_INFO, "%s", string);
666-
667-
g_free(string);
668-
669659
return true;
670660
}
671661

zathura/completion.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ static int compare_case_insensitive(const void* data1, const void* data2) {
2424
const char* str1 = data1;
2525
const char* str2 = data2;
2626

27-
char* ustr1 = g_utf8_casefold(str1, -1);
28-
char* ustr2 = g_utf8_casefold(str2, -1);
29-
int res = g_utf8_collate(ustr1, ustr2);
30-
g_free(ustr1);
31-
g_free(ustr2);
32-
return res;
27+
g_autofree char* ustr1 = g_utf8_casefold(str1, -1);
28+
g_autofree char* ustr2 = g_utf8_casefold(str2, -1);
29+
return g_utf8_collate(ustr1, ustr2);
3330
}
3431

3532
static girara_list_t* list_files(zathura_t* zathura, const char* current_path, const char* current_file,
@@ -41,7 +38,7 @@ static girara_list_t* list_files(zathura_t* zathura, const char* current_path, c
4138
girara_debug("checking files in %s", current_path);
4239

4340
/* read directory */
44-
GDir* dir = g_dir_open(current_path, 0, NULL);
41+
g_autoptr(GDir) dir = g_dir_open(current_path, 0, NULL);
4542
if (dir == NULL) {
4643
return NULL;
4744
}
@@ -56,20 +53,18 @@ static girara_list_t* list_files(zathura_t* zathura, const char* current_path, c
5653
/* read files */
5754
const char* name = NULL;
5855
while ((name = g_dir_read_name(dir)) != NULL) {
59-
char* e_name = g_filename_display_name(name);
56+
g_autofree char* e_name = g_filename_display_name(name);
6057
if (e_name == NULL) {
6158
goto error_free;
6259
}
6360

6461
size_t e_length = strlen(e_name);
6562

6663
if (show_hidden == false && e_name[0] == '.') {
67-
g_free(e_name);
6864
continue;
6965
}
7066

7167
if ((current_file_length > e_length) || strncmp(current_file, e_name, current_file_length)) {
72-
g_free(e_name);
7368
continue;
7469
}
7570

@@ -79,7 +74,6 @@ static girara_list_t* list_files(zathura_t* zathura, const char* current_path, c
7974
}
8075

8176
char* full_path = g_strdup_printf("%s%s%s", current_path, tmp, e_name);
82-
g_free(e_name);
8377

8478
if (g_file_test(full_path, G_FILE_TEST_IS_DIR) == true) {
8579
if (show_directories == false) {
@@ -98,8 +92,6 @@ static girara_list_t* list_files(zathura_t* zathura, const char* current_path, c
9892
}
9993
}
10094

101-
g_dir_close(dir);
102-
10395
if (girara_list_size(res) == 1) {
10496
char* path = girara_list_nth(res, 0);
10597
if (g_file_test(path, G_FILE_TEST_IS_DIR) == true) {
@@ -113,7 +105,6 @@ static girara_list_t* list_files(zathura_t* zathura, const char* current_path, c
113105
return res;
114106

115107
error_free:
116-
g_dir_close(dir);
117108
girara_list_free(res);
118109
return NULL;
119110
}

zathura/config.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ static void cb_color(girara_session_t* session, const char* name, girara_setting
4242
GdkRGBA color = {0, 0, 0, 0};
4343
gdk_rgba_parse(&color, str_value);
4444

45-
char* colorstr = gdk_rgba_to_string(&color);
45+
g_autofree char* colorstr = gdk_rgba_to_string(&color);
4646
girara_template_set_variable_value(csstemplate, name, colorstr);
47-
g_free(colorstr);
4847
}
4948

5049

@@ -157,9 +156,8 @@ static void cb_window_statbusbar_changed(girara_session_t* session, const char*
157156

158157
const bool is_window_setting = g_str_has_prefix(name, "window-");
159158
if (is_window_setting) {
160-
char* formatted_filename = get_formatted_filename(zathura, !is_window_setting);
159+
g_autofree char* formatted_filename = get_formatted_filename(zathura, !is_window_setting);
161160
girara_set_window_title(zathura->ui.session, formatted_filename);
162-
g_free(formatted_filename);
163161
} else {
164162
statusbar_page_number_update(zathura);
165163
}
@@ -711,24 +709,21 @@ void config_load_default(zathura_t* zathura) {
711709

712710
void config_load_files(zathura_t* zathura) {
713711
/* load global configuration files */
714-
char* config_path = girara_get_xdg_path(XDG_CONFIG_DIRS);
712+
g_autofree char* config_path = girara_get_xdg_path(XDG_CONFIG_DIRS);
715713
if (config_path != NULL && config_path[0] != '\0') {
716714
char** config_dirs = g_strsplit(config_path, ":", 0);
717715
ssize_t size = g_strv_length(config_dirs) - 1;
718716
for (; size >= 0; --size) {
719-
const char* dir = config_dirs[size];
720-
char* file = g_build_filename(dir, ZATHURA_RC, NULL);
717+
const char* dir = config_dirs[size];
718+
g_autofree char* file = g_build_filename(dir, ZATHURA_RC, NULL);
721719
girara_config_parse(zathura->ui.session, file);
722-
g_free(file);
723720
}
724721
g_strfreev(config_dirs);
725-
g_free(config_path);
726722
}
727723

728724
girara_config_parse(zathura->ui.session, SYSCONFDIR "/" ZATHURA_RC);
729725

730726
/* load local configuration files */
731-
char* configuration_file = g_build_filename(zathura->config.config_dir, ZATHURA_RC, NULL);
727+
g_autofree char* configuration_file = g_build_filename(zathura->config.config_dir, ZATHURA_RC, NULL);
732728
girara_config_parse(zathura->ui.session, configuration_file);
733-
g_free(configuration_file);
734729
}

0 commit comments

Comments
 (0)