From a8efb20cb824c914bd459875761b386a39d4f40b Mon Sep 17 00:00:00 2001 From: NRK Date: Tue, 8 Mar 2022 17:29:47 +0600 Subject: [PATCH 1/5] sync copyright notice with --version the copyright notice on the --version output was updated on 07fd608. but the comment on the top of the file remained outdated. --- dragon.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/dragon.c b/dragon.c index cf2ecf4..df4ad0f 100644 --- a/dragon.c +++ b/dragon.c @@ -1,5 +1,5 @@ -// dragon - very lightweight DnD file source/target -// Copyright 2014 Michael Homer. +// dragon - very lightweight DnD file source/target. +// Copyright (C) 2014-2022 Michael Homer and contributors. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -473,7 +473,7 @@ int main (int argc, char **argv) { } else if (strcmp(argv[i], "--version") == 0) { mode = MODE_VERSION; puts("dragon " VERSION); - puts("Copyright (C) 2014-2022 Michael Homer and contributors"); + puts("Copyright (C) 2014-2022 Michael Homer and contributors."); puts("This program comes with ABSOLUTELY NO WARRANTY."); puts("See the source for copying conditions."); exit(0); From d21c84adf165192efcf8ce8d0338b37b5afee364 Mon Sep 17 00:00:00 2001 From: NRK Date: Tue, 8 Mar 2022 17:34:19 +0600 Subject: [PATCH 2/5] use proper conversion specifer for size_t --- dragon.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dragon.c b/dragon.c index df4ad0f..db82742 100644 --- a/dragon.c +++ b/dragon.c @@ -416,7 +416,7 @@ static void readstdin(void) { cur_size = newline - stdin_files + 1; if (max_size < cur_size + BUFSIZ) { if (!(stdin_files = realloc(stdin_files, (max_size += BUFSIZ)))) - fprintf(stderr, "%s: cannot realloc %lu bytes.\n", progname, max_size); + fprintf(stderr, "%s: cannot realloc %zu bytes.\n", progname, max_size); newline = stdin_files + cur_size - 1; } write_pos = newline + 1; From 3e5f234b49d4dd0e4896292761f4795de881da5f Mon Sep 17 00:00:00 2001 From: NRK Date: Tue, 8 Mar 2022 17:37:47 +0600 Subject: [PATCH 3/5] avoid unnecessary heap allocation --- dragon.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/dragon.c b/dragon.c index db82742..c8dbc1c 100644 --- a/dragon.c +++ b/dragon.c @@ -119,6 +119,7 @@ void drag_end(GtkWidget *widget, GdkDragContext *context, gpointer user_data) { gboolean succeeded = gdk_drag_drop_succeeded(context); GdkDragAction action = gdk_drag_context_get_selected_action (context); char* action_str; + char buf[20]; switch (action) { case GDK_ACTION_COPY: action_str = "COPY"; break; @@ -129,13 +130,11 @@ void drag_end(GtkWidget *widget, GdkDragContext *context, gpointer user_data) { case GDK_ACTION_ASK: action_str = "ASK"; break; default: - action_str = malloc(sizeof(char) * 20); - snprintf(action_str, 20, "invalid (%d)", action); + snprintf(buf, sizeof(buf), "invalid (%d)", action); + action_str = buf; break; } fprintf(stderr, "Selected drop action: %s; Succeeded: %d\n", action_str, succeeded); - if (action_str[0] == 'i') - free(action_str); } if (and_exit) gtk_main_quit(); From 618ff8be0a3200378846b3fdc28675d252159512 Mon Sep 17 00:00:00 2001 From: NRK Date: Sun, 20 Mar 2022 17:15:23 +0600 Subject: [PATCH 4/5] check for malloc failures --- dragon.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/dragon.c b/dragon.c index c8dbc1c..e0f9cdb 100644 --- a/dragon.c +++ b/dragon.c @@ -68,6 +68,15 @@ GtkWidget *all_button; void add_target_button(); +void *emalloc(size_t n) { + void *ret = malloc(n); + if (!ret) { + fprintf(stderr, "Allocation failed\n"); + exit(1); + } + return ret; +} + void do_quit(GtkWidget *widget, gpointer data) { exit(0); } @@ -212,7 +221,7 @@ void add_file_button(GFile *file) { add_uri(uri); return; } - struct draggable_thing *dragdata = malloc(sizeof(struct draggable_thing)); + struct draggable_thing *dragdata = emalloc(sizeof(struct draggable_thing)); dragdata->text = filename; dragdata->uri = uri; @@ -259,7 +268,7 @@ void add_uri_button(char *uri) { add_uri(uri); return; } - struct draggable_thing *dragdata = malloc(sizeof(struct draggable_thing)); + struct draggable_thing *dragdata = emalloc(sizeof(struct draggable_thing)); dragdata->text = uri; dragdata->uri = uri; GtkButton *button = add_button(uri, dragdata, TARGET_TYPE_URI); @@ -445,7 +454,7 @@ void create_all_button() { int main (int argc, char **argv) { bool from_stdin = false; - stdin_files = malloc(BUFSIZ * 2); + stdin_files = emalloc(BUFSIZ * 2); progname = argv[0]; for (int i=1; i MAX_SIZE ? argc : MAX_SIZE) + 1)); + uri_collection = emalloc(sizeof(char*) * ((argc > MAX_SIZE ? argc : MAX_SIZE) + 1)); for (int i=1; i Date: Wed, 9 Mar 2022 00:35:33 +0600 Subject: [PATCH 5/5] avoid using function with unspecified arguments in C, fun(void) means this function takes _no arguments_, and it would be a compile time error if this function was called with one. however fun() means the function takes _unspecified_ arguments, so the compiler can't do any type checking. calling such a function with _any_ amount or any type of arguments will still end up compiling. for example: void fun1() { return; } void fun2(void) { return; } int main(void) { fun1(10, "string"); /* should compile fine */ fun2(1); /* compile time error */ } --- dragon.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dragon.c b/dragon.c index e0f9cdb..72189a4 100644 --- a/dragon.c +++ b/dragon.c @@ -66,7 +66,7 @@ struct draggable_thing fake_dragdata; GtkWidget *all_button; // --- -void add_target_button(); +void add_target_button(void); void *emalloc(size_t n) { void *ret = malloc(n); @@ -318,7 +318,7 @@ gboolean drag_drop (GtkWidget *widget, return true; } -void update_all_button() { +void update_all_button(void) { sprintf(file_num_label, "%d files", uri_count); gtk_button_set_label((GtkButton *)all_button, file_num_label); } @@ -371,7 +371,7 @@ drag_data_received (GtkWidget *widget, gtk_main_quit(); } -void add_target_button() { +void add_target_button(void) { GtkWidget *label = gtk_button_new(); gtk_button_set_label(GTK_BUTTON(label), "Drag something here..."); gtk_container_add(GTK_CONTAINER(vbox), label); @@ -392,7 +392,7 @@ void add_target_button() { G_CALLBACK(drag_data_received), NULL); } -void target_mode() { +void target_mode(void) { add_target_button(); gtk_widget_show_all(window); gtk_main(); @@ -431,7 +431,7 @@ static void readstdin(void) { } } -void create_all_button() { +void create_all_button(void) { sprintf(file_num_label, "%d files", uri_count); all_button = gtk_button_new_with_label(file_num_label);