Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
Build with address sanitizer and run the tests

Signed-off-by: Hubert Figuière <[email protected]>
  • Loading branch information
hfiguiere authored and TingPing committed Feb 13, 2024
1 parent 8eee4bd commit 6e3cc82
Show file tree
Hide file tree
Showing 15 changed files with 39 additions and 27 deletions.
1 change: 1 addition & 0 deletions app/flatpak-builtins-create-usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ ostree_create_usb (GOptionContext *context,
break;
}
}
glnx_dirfd_iterator_clear (&repos_iter);

/* If we need a symlink, find a unique name for it and create it. */
if (need_symlink)
Expand Down
6 changes: 3 additions & 3 deletions app/flatpak-cli-transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ install_authenticator (FlatpakTransaction *old_transaction,
FlatpakCliTransaction *old_cli = FLATPAK_CLI_TRANSACTION (old_transaction);
g_autoptr(FlatpakTransaction) transaction2 = NULL;
g_autoptr(GError) local_error = NULL;
FlatpakInstallation *installation = flatpak_transaction_get_installation (old_transaction);
g_autoptr(FlatpakInstallation) installation = flatpak_transaction_get_installation (old_transaction);
FlatpakDir *dir = flatpak_installation_get_dir (installation, NULL);

if (dir == NULL)
Expand Down Expand Up @@ -888,7 +888,7 @@ end_of_lifed_with_rebase (FlatpakTransaction *transaction,
EolAction action = EOL_UNDECIDED;
EolAction old_action = EOL_UNDECIDED;
gboolean can_rebase = rebased_to_ref != NULL && remote != NULL;
FlatpakInstallation *installation = flatpak_transaction_get_installation (transaction);
g_autoptr(FlatpakInstallation) installation = flatpak_transaction_get_installation (transaction);
FlatpakDir *dir = flatpak_installation_get_dir (installation, NULL);

if (ref == NULL)
Expand Down Expand Up @@ -1287,7 +1287,7 @@ static gboolean
transaction_ready_pre_auth (FlatpakTransaction *transaction)
{
FlatpakCliTransaction *self = FLATPAK_CLI_TRANSACTION (transaction);
GList *ops = flatpak_transaction_get_operations (transaction);
g_autolist(FlatpakTransactionOperation) ops = flatpak_transaction_get_operations (transaction);
GList *l;
int i;
FlatpakTablePrinter *printer;
Expand Down
2 changes: 1 addition & 1 deletion app/flatpak-quiet-transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ install_authenticator (FlatpakTransaction *old_transaction,
{
g_autoptr(FlatpakTransaction) transaction2 = NULL;
g_autoptr(GError) local_error = NULL;
FlatpakInstallation *installation = flatpak_transaction_get_installation (old_transaction);
g_autoptr(FlatpakInstallation) installation = flatpak_transaction_get_installation (old_transaction);
FlatpakDir *dir = flatpak_installation_get_dir (installation, NULL);

if (dir == NULL)
Expand Down
4 changes: 2 additions & 2 deletions common/flatpak-dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -11513,7 +11513,7 @@ flatpak_dir_get_if_deployed (FlatpakDir *self,
}

if (g_file_query_file_type (deploy_dir, G_FILE_QUERY_INFO_NONE, cancellable) == G_FILE_TYPE_DIRECTORY)
return g_object_ref (deploy_dir);
return g_steal_pointer (&deploy_dir);

/* Maybe it was removed but is still living? */
if (checksum != NULL)
Expand All @@ -11527,7 +11527,7 @@ flatpak_dir_get_if_deployed (FlatpakDir *self,
removed_deploy_dir = g_file_get_child (removed_dir, dirname);

if (g_file_query_file_type (removed_deploy_dir, G_FILE_QUERY_INFO_NONE, cancellable) == G_FILE_TYPE_DIRECTORY)
return g_object_ref (removed_deploy_dir);
return g_steal_pointer (&removed_deploy_dir);
}

return NULL;
Expand Down
2 changes: 1 addition & 1 deletion common/flatpak-locale-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ flatpak_get_locale_langs_from_accounts_dbus_for_user (GDBusProxy *proxy, GPtrArr
value = g_dbus_proxy_get_cached_property (accounts_proxy, "Languages");
if (value != NULL)
{
const char **locales = g_variant_get_strv (value, NULL);
g_autofree const char **locales = g_variant_get_strv (value, NULL);
guint i;

for (i = 0; locales != NULL && locales[i] != NULL; i++)
Expand Down
1 change: 1 addition & 0 deletions common/flatpak-prune.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ flatpak_repo_prune (OstreeRepo *repo,

g_timer_stop (timer);
g_info ("Elapsed time: %.1f sec", g_timer_elapsed (timer, NULL));
g_clear_pointer (&timer, g_timer_destroy);
}

{
Expand Down
11 changes: 7 additions & 4 deletions common/flatpak-transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -3941,9 +3941,11 @@ request_tokens_for_remote (FlatpakTransaction *self,
g_autoptr(GFile) deploy = NULL;
deploy = flatpak_dir_get_if_deployed (priv->dir, auto_install_ref, NULL, cancellable);
if (deploy == NULL)
g_signal_emit (self, signals[INSTALL_AUTHENTICATOR], 0,
remote, flatpak_decomposed_get_ref (auto_install_ref));
deploy = flatpak_dir_get_if_deployed (priv->dir, auto_install_ref, NULL, cancellable);
{
g_signal_emit (self, signals[INSTALL_AUTHENTICATOR], 0,
remote, flatpak_decomposed_get_ref (auto_install_ref));
deploy = flatpak_dir_get_if_deployed (priv->dir, auto_install_ref, NULL, cancellable);
}
if (deploy == NULL)
return flatpak_fail (error, _("No authenticator installed for remote '%s'"), remote);
}
Expand Down Expand Up @@ -4024,7 +4026,7 @@ request_tokens_for_remote (FlatpakTransaction *self,
g_assert (priv->active_request_id == 0); /* No outstanding requests */
priv->active_request = NULL;

results = data.results; /* Make sure its freed as needed */
results = data.results; /* Make sure it's freed as needed */

{
g_autofree char *results_str = results != NULL ? g_variant_print (results, FALSE) : g_strdup ("NULL");
Expand Down Expand Up @@ -4082,6 +4084,7 @@ request_tokens_for_remote (FlatpakTransaction *self,
token = token_for_refs;
break;
}
g_clear_pointer (&refs_strv, g_free);
}

if (token == NULL)
Expand Down
12 changes: 7 additions & 5 deletions common/flatpak-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ flatpak_get_bwrap (void)
gboolean
flatpak_bwrap_is_unprivileged (void)
{
const char *path = g_find_program_in_path (flatpak_get_bwrap ());
g_autofree char *path = g_find_program_in_path (flatpak_get_bwrap ());
struct stat st;

/* Various features are supported only if bwrap exists and is not setuid */
Expand Down Expand Up @@ -3530,6 +3530,7 @@ _ostree_repo_static_delta_superblock_digest (OstreeRepo *repo,
glnx_autofd int fd = -1;
guint8 digest[OSTREE_SHA256_DIGEST_LEN];
gsize len;
gpointer data = NULL;

if (!glnx_openat_rdonly (ostree_repo_get_dfd (repo), superblock, TRUE, &fd, error))
return NULL;
Expand All @@ -3543,9 +3544,10 @@ _ostree_repo_static_delta_superblock_digest (OstreeRepo *repo,
len = sizeof digest;
g_checksum_get_digest (checksum, digest, &len);

data = g_memdup2 (digest, len);
return g_variant_new_from_data (G_VARIANT_TYPE ("ay"),
g_memdup2 (digest, len), len,
FALSE, g_free, FALSE);
data, len,
FALSE, g_free, data);
}

static char *
Expand Down Expand Up @@ -4630,7 +4632,7 @@ flatpak_repo_gc_digested_summaries (OstreeRepo *repo,
{
if (strcmp (ext, ".gz") == 0 && strlen (dent->d_name) == 64 + 3)
{
char *sha256 = g_strndup (dent->d_name, 64);
g_autofree char *sha256 = g_strndup (dent->d_name, 64);

/* Keep all the referenced summaries */
if (g_hash_table_contains (digested_summary_cache, sha256))
Expand All @@ -4646,7 +4648,7 @@ flatpak_repo_gc_digested_summaries (OstreeRepo *repo,
const char *dash = strchr (dent->d_name, '-');
if (dash != NULL && dash < ext && (ext - dash) == 1 + 64)
{
char *to_sha256 = g_strndup (dash + 1, 64);
g_autofree char *to_sha256 = g_strndup (dash + 1, 64);

/* Only keep deltas going to a generated summary */
if (g_hash_table_contains (digested_summaries, to_sha256))
Expand Down
4 changes: 2 additions & 2 deletions icon-validator/validate-icon.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ validate_icon (const char *arg_width,
GdkPixbufFormat *format;
int max_width, max_height;
int width, height;
const char *name;
g_autofree char *name = NULL;
const char *allowed_formats[] = { "png", "jpeg", "svg", NULL };
g_autoptr(GdkPixbuf) pixbuf = NULL;
g_autoptr(GError) error = NULL;
Expand Down Expand Up @@ -252,7 +252,7 @@ static GOptionEntry entries[] = {
int
main (int argc, char *argv[])
{
GOptionContext *context;
g_autoptr(GOptionContext) context = NULL;
GError *error = NULL;

context = g_option_context_new ("WIDTH HEIGHT PATH");
Expand Down
4 changes: 3 additions & 1 deletion portal/flatpak-portal-app-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ name_owner_changed (GDBusConnection *connection,
GVariant *parameters,
gpointer user_data)
{
const char *name, *from, *to;
g_autofree char *name = NULL;
g_autofree char *from = NULL;
g_autofree char *to = NULL;

g_variant_get (parameters, "(sss)", &name, &from, &to);

Expand Down
2 changes: 1 addition & 1 deletion portal/flatpak-portal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3013,7 +3013,7 @@ main (int argc,
ssize_t exe_path_len;
gboolean replace;
gboolean show_version;
GOptionContext *context;
g_autoptr(GOptionContext) context = NULL;
GBusNameOwnerFlags flags;
g_autoptr(GError) error = NULL;
const GOptionEntry options[] = {
Expand Down
6 changes: 4 additions & 2 deletions session-helper/flatpak-session-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ file_monitor_do (MonitorData *data)
/* We can't update the /etc/localtime symlink at runtime, nor can we make it a of the
* correct form "../usr/share/zoneinfo/$timezone". So, instead we use the old debian
* /etc/timezone file for telling the sandbox the timezone. */
char *dest = g_build_filename (monitor_dir, "timezone", NULL);
g_autofree char *dest = g_build_filename (monitor_dir, "timezone", NULL);
g_autofree char *raw_timezone = flatpak_get_timezone ();
g_autofree char *timezone_content = g_strdup_printf ("%s\n", raw_timezone);

Expand Down Expand Up @@ -769,6 +769,7 @@ main (int argc,
gboolean show_version;
GOptionContext *context;
GBusNameOwnerFlags flags;
g_autofree char *pk11_program = NULL;
g_autofree char *flatpak_dir = NULL;
g_autoptr(GError) error = NULL;
const GOptionEntry options[] = {
Expand Down Expand Up @@ -849,7 +850,8 @@ main (int argc,
exit (1);
}

if (g_find_program_in_path ("p11-kit"))
pk11_program = g_find_program_in_path ("p11-kit");
if (pk11_program)
start_p11_kit_server (flatpak_dir);
else
g_info ("p11-kit not found");
Expand Down
2 changes: 1 addition & 1 deletion system-helper/flatpak-system-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -2314,7 +2314,7 @@ main (int argc,
gboolean replace;
gboolean show_version;
GBusNameOwnerFlags flags;
GOptionContext *context;
g_autoptr(GOptionContext) context = NULL;
g_autoptr(GError) error = NULL;
const GOptionEntry options[] = {
{ "replace", 'r', 0, G_OPTION_ARG_NONE, &replace, "Replace old daemon.", NULL },
Expand Down
1 change: 1 addition & 0 deletions tests/test-portal.c
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ teardown (Fixture *f,
tests_dbus_daemon_teardown (&f->dbus_daemon);
g_clear_object (&f->portal);
g_free (f->portal_path);
g_free (f->mock_flatpak);
}

int
Expand Down
8 changes: 4 additions & 4 deletions tests/test-update-portal.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ update_test (PortalFlatpak *portal, int status_pipe)
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GVariantBuilder opt_builder;
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
g_autoptr(GMainLoop) loop = g_main_loop_new (NULL, FALSE);
UpdateData data = { loop };

monitor = create_monitor (portal, NULL, NULL, &error);
Expand Down Expand Up @@ -241,7 +241,7 @@ update_null_test (PortalFlatpak *portal, int status_pipe)
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GVariantBuilder opt_builder;
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
g_autoptr(GMainLoop) loop = g_main_loop_new (NULL, FALSE);
UpdateData data = { loop };

monitor = create_monitor (portal, NULL, NULL, &error);
Expand Down Expand Up @@ -277,7 +277,7 @@ update_fail_test (PortalFlatpak *portal, int status_pipe)
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GVariantBuilder opt_builder;
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
g_autoptr(GMainLoop) loop = g_main_loop_new (NULL, FALSE);
UpdateData data = { loop };

monitor = create_monitor (portal, NULL, NULL, &error);
Expand Down Expand Up @@ -313,7 +313,7 @@ update_notsupp_test (PortalFlatpak *portal, int status_pipe)
g_autoptr(GError) error = NULL;
PortalFlatpakUpdateMonitor *monitor;
GVariantBuilder opt_builder;
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
g_autoptr(GMainLoop) loop = g_main_loop_new (NULL, FALSE);
UpdateData data = { loop };

monitor = create_monitor (portal, NULL, NULL, &error);
Expand Down

0 comments on commit 6e3cc82

Please sign in to comment.