Skip to content

Commit

Permalink
g_free() to auto_gfree, introduce auto_guchar
Browse files Browse the repository at this point in the history
Fix 11 potential mem leaks in theme.c
  • Loading branch information
H3rnand3zzz committed Jul 7, 2023
1 parent ef8d2c9 commit 1d53a1f
Show file tree
Hide file tree
Showing 39 changed files with 225 additions and 239 deletions.
3 changes: 1 addition & 2 deletions src/command/cmd_ac.c
Original file line number Diff line number Diff line change
Expand Up @@ -4387,9 +4387,8 @@ _url_autocomplete(ProfWin* window, const char* const input, gboolean previous)
if ((num_args == 1 && space_at_end) || (num_args == 2 && !space_at_end)) {
result = autocomplete_param_with_func(input, "/url save", wins_get_url, previous, window);
} else if ((num_args == 2 && space_at_end) || (num_args == 3 && !space_at_end)) {
gchar* cmd = g_strdup_printf("/url save %s", args[1]);
auto_gchar gchar* cmd = g_strdup_printf("/url save %s", args[1]);
result = cmd_ac_complete_filepath(input, cmd, previous);
g_free(cmd);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/command/cmd_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3039,14 +3039,13 @@ command_mangen(void)
create_dir("docs");

GDateTime* now = g_date_time_new_now_local();
gchar* date = g_date_time_format(now, "%F");
gchar* header = g_strdup_printf(".TH man 1 \"%s\" \"" PACKAGE_VERSION "\" \"Profanity XMPP client\"\n", date);
auto_gchar gchar* date = g_date_time_format(now, "%F");
auto_gchar gchar* header = g_strdup_printf(".TH man 1 \"%s\" \"" PACKAGE_VERSION "\" \"Profanity XMPP client\"\n", date);
if (!header) {
log_error("command_mangen(): could not allocate memory");
return;
}
g_date_time_unref(now);
g_free(date);

GList* curr = cmds;
while (curr) {
Expand Down Expand Up @@ -3098,6 +3097,5 @@ command_mangen(void)

printf("\nProcessed %d commands.\n\n", g_list_length(cmds));

g_free(header);
g_list_free(cmds);
}
38 changes: 7 additions & 31 deletions src/command/cmd_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3632,7 +3632,7 @@ cmd_join(ProfWin* window, const char* const command, gchar** args)
return TRUE;
}

char* room = NULL;
auto_gchar gchar* room = NULL;
char* nick = NULL;
char* passwd = NULL;
char* account_name = session_get_account_name();
Expand Down Expand Up @@ -3660,7 +3660,6 @@ cmd_join(ProfWin* window, const char* const command, gchar** args)
if (!parsed) {
cons_bad_cmd_usage(command);
cons_show("");
g_free(room);
jid_destroy(room_arg);
return TRUE;
}
Expand Down Expand Up @@ -3690,7 +3689,6 @@ cmd_join(ProfWin* window, const char* const command, gchar** args)
ui_switch_to_room(room);
}

g_free(room);
jid_destroy(room_arg);
account_free(account);

Expand Down Expand Up @@ -4575,8 +4573,8 @@ cmd_rooms(ProfWin* window, const char* const command, gchar** args)
return TRUE;
}

gchar* service = NULL;
gchar* filter = NULL;
auto_gchar gchar* service = NULL;
auto_gchar gchar* filter = NULL;
if (args[0] != NULL) {
if (g_strcmp0(args[0], "service") == 0) {
if (args[1] == NULL) {
Expand Down Expand Up @@ -4625,8 +4623,6 @@ cmd_rooms(ProfWin* window, const char* const command, gchar** args)
if (args[3] == NULL) {
cons_bad_cmd_usage(command);
cons_show("");
g_free(service);
g_free(filter);
return TRUE;
}
g_free(service);
Expand All @@ -4635,17 +4631,13 @@ cmd_rooms(ProfWin* window, const char* const command, gchar** args)
if (args[3] == NULL) {
cons_bad_cmd_usage(command);
cons_show("");
g_free(service);
g_free(filter);
return TRUE;
}
g_free(filter);
filter = g_strdup(args[3]);
} else {
cons_bad_cmd_usage(command);
cons_show("");
g_free(service);
g_free(filter);
return TRUE;
}
}
Expand All @@ -4658,8 +4650,6 @@ cmd_rooms(ProfWin* window, const char* const command, gchar** args)
} else {
cons_show("Account MUC service property not found.");
account_free(account);
g_free(service);
g_free(filter);
return TRUE;
}
}
Expand All @@ -4672,9 +4662,6 @@ cmd_rooms(ProfWin* window, const char* const command, gchar** args)
}
iq_room_list_request(service, filter);

g_free(service);
g_free(filter);

return TRUE;
}

Expand Down Expand Up @@ -7024,7 +7011,7 @@ cmd_receipts(ProfWin* window, const char* const command, gchar** args)
gboolean
cmd_plugins_install(ProfWin* window, const char* const command, gchar** args)
{
char* path = NULL;
auto_char char* path = NULL;

if (args[1] == NULL) {
cons_bad_cmd_usage(command);
Expand All @@ -7047,28 +7034,24 @@ cmd_plugins_install(ProfWin* window, const char* const command, gchar** args)

if (access(path, R_OK) != 0) {
cons_show("Cannot access: %s", path);
free(path);
return TRUE;
}

if (is_regular_file(path)) {
if (!g_str_has_suffix(path, ".py") && !g_str_has_suffix(path, ".so")) {
cons_show("Plugins must have one of the following extensions: '.py' '.so'");
free(path);
return TRUE;
}

GString* error_message = g_string_new(NULL);
gchar* plugin_name = g_path_get_basename(path);
auto_gchar gchar* plugin_name = g_path_get_basename(path);
gboolean result = plugins_install(plugin_name, path, error_message);
if (result) {
cons_show("Plugin installed and loaded: %s", plugin_name);
} else {
cons_show("Failed to install plugin: %s. %s", plugin_name, error_message->str);
}
g_free(plugin_name);
g_string_free(error_message, TRUE);
free(path);
return TRUE;
} else if (is_dir(path)) {
PluginsInstallResult* result = plugins_install_all(path);
Expand All @@ -7094,14 +7077,12 @@ cmd_plugins_install(ProfWin* window, const char* const command, gchar** args)
} else {
cons_show("No plugins found in: %s", path);
}
free(path);
plugins_free_install_result(result);
return TRUE;
} else {
cons_show("Argument must be a file or directory.");
}

free(path);
return TRUE;
}

Expand Down Expand Up @@ -9627,11 +9608,9 @@ _cmd_executable_template(const preference_t setting, const char* command, gchar*
}

if (g_strcmp0(args[1], "set") == 0 && num_args >= 3) {
gchar* str = g_strjoinv(" ", &args[2]);
auto_gchar gchar* str = g_strjoinv(" ", &args[2]);
prefs_set_string(setting, str);
cons_show("`%s` command set to invoke '%s'", command, str);
g_free(str);

} else if (g_strcmp0(args[1], "default") == 0) {
prefs_set_string(setting, NULL);
auto_gchar gchar* def = prefs_get_string(setting);
Expand Down Expand Up @@ -10076,7 +10055,7 @@ cmd_vcard_photo(ProfWin* window, const char* const command, gchar** args)
return TRUE;
}

char* jid = NULL;
auto_gchar gchar* jid = NULL;
char* filepath = NULL;
int index = 0;

Expand Down Expand Up @@ -10188,9 +10167,6 @@ cmd_vcard_photo(ProfWin* window, const char* const command, gchar** args)
cons_bad_cmd_usage(command);
}

if (!jidless) {
g_free(jid);
}
return TRUE;
}

Expand Down
22 changes: 14 additions & 8 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ auto_free_gchar(gchar** str)
g_free(*str);
}

void
auto_free_guchar(guchar** ptr)
{
if (ptr == NULL) {
return;
}
g_free(*ptr);
}

void
auto_free_gcharv(gchar*** args)
{
Expand Down Expand Up @@ -545,16 +554,14 @@ basename_from_url(const char* url)
const char* default_name = "index";

GFile* file = g_file_new_for_commandline_arg(url);
char* basename = g_file_get_basename(file);
auto_gchar gchar* basename = g_file_get_basename(file);
g_object_unref(file);

if (_has_directory_suffix(basename)) {
g_free(basename);
basename = strdup(default_name);
return strdup(default_name);
}

g_object_unref(file);

return basename;
return strdup(basename);
}

gchar*
Expand Down Expand Up @@ -589,9 +596,8 @@ unique_filename_from_url(const char* url, const char* path)
if (_has_directory_suffix(realpath) || g_file_test(realpath, G_FILE_TEST_IS_DIR)) {
// The target should be used as a directory. Assume that the basename
// should be derived from the URL.
char* basename = basename_from_url(url);
auto_char char* basename = basename_from_url(url);
filename = g_build_filename(g_file_peek_path(target), basename, NULL);
g_free(basename);
} else {
// Just use the target as filename.
filename = g_build_filename(g_file_peek_path(target), NULL);
Expand Down
102 changes: 102 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
* @file common.h
*
* @brief Common utilities for the project.
*/

#ifndef COMMON_H
Expand All @@ -50,11 +53,84 @@
#define PROF_STRINGIFY_(n) #n
#define PROF_STRINGIFY(n) PROF_STRINGIFY_(n)

/**
* Frees the memory allocated for a gchar* string.
*
* @param str Pointer to the gchar* string to be freed. If NULL, no action is taken.
*/
void auto_free_gchar(gchar** str);

/**
* @brief Macro for automatically freeing a gchar* string when it goes out of scope.
*
* This macro is used to automatically free a gchar* string when it goes out of scope.
* It utilizes the `auto_free_gchar` function and should be placed before the variable declaration.
*
* Example usage:
* ```
* auto_gchar gchar* myString = g_strdup("Hello, world!");
* ```
*/
#define auto_gchar __attribute__((__cleanup__(auto_free_gchar)))

/**
* Frees the memory allocated for a guchar* string.
*
* @param str Pointer to the guchar* string to be freed. If NULL, no action is taken.
*/
void auto_free_guchar(guchar** str);

/**
* @brief Macro for automatically freeing a guchar* string when it goes out of scope.
*
* This macro is used to automatically free a guchar* string when it goes out of scope.
* It utilizes the `auto_free_guchar` function and should be placed before the variable declaration.
*
* Example usage:
* ```
* auto_guchar guchar* myString = g_base64_decode("SGVsbG8sIHdvcmxkIQ==", NULL);
* ```
*/
#define auto_guchar __attribute__((__cleanup__(auto_free_guchar)))

/**
* Frees the memory allocated for a gchar** string array.
*
* @param args Pointer to the gchar** string array to be freed. If NULL, no action is taken.
*/
void auto_free_gcharv(gchar*** args);

/**
* @brief Macro for automatically freeing a gchar** string array when it goes out of scope.
*
* This macro is used to automatically free a gchar** string array when it goes out of scope.
* It utilizes the `auto_free_gcharv` function and should be placed before the variable declaration.
*
* Example usage:
* ```
* auto_gcharv gchar** stringArray = g_strsplit("Hello, world!", " ", -1);
* ```
*/
#define auto_gcharv __attribute__((__cleanup__(auto_free_gcharv)))

/**
* Frees the memory allocated for a char* string.
*
* @param str Pointer to the char* string to be freed. If NULL, no action is taken.
*/
void auto_free_char(char** str);

/**
* @brief Macro for automatically freeing a char* string when it goes out of scope.
*
* This macro is used to automatically free a char* string when it goes out of scope.
* It utilizes the `auto_free_char` function and should be placed before the variable declaration.
*
* Example usage:
* ```
* auto_char char* myString = strdup("Hello, world!");
* ```
*/
#define auto_char __attribute__((__cleanup__(auto_free_char)))

#if defined(__OpenBSD__)
Expand Down Expand Up @@ -153,10 +229,36 @@ gboolean call_external(gchar** argv);
*/
gchar** format_call_external_argv(const char* template, const char* url, const char* filename);

/**
* Generates a unique filename based on the provided URL and path.
*
* @param url The URL to derive the basename from.
* @param path The path to prepend to the generated filename. If NULL, the current directory is used.
* @return A unique filename combining the path and derived basename from the URL.
*
* @note Remember to free the returned argument vector using `auto_gchar` or `g_free()`.
*/
gchar* unique_filename_from_url(const char* url, const char* path);

/**
* Expands the provided path by resolving special characters like '~' and 'file://'.
*
* @param path The path to expand.
* @return The expanded path.
*
* @note Remember to free the returned argument vector using `auto_gchar` or `g_free()`.
*/
gchar* get_expanded_path(const char* path);

void glib_hash_table_free(GHashTable* hash_table);

/**
* @brief Extracts the basename from a given URL.
*
* @param url The URL to extract the basename from.
* @return The extracted basename or a default name ("index") if the basename has a directory suffix.
* The returned string must be freed by the caller using free().
*/
char* basename_from_url(const char* url);

#endif
Loading

0 comments on commit 1d53a1f

Please sign in to comment.