Define and use cleanup helpers for libarchive
This should fix some of the ASAN leaks around libarchive usage, and is generally better. Closes: #609 Approved by: jlebon
This commit is contained in:
parent
c733e21a84
commit
7bb0ff46a4
|
|
@ -134,6 +134,7 @@ libostree_1_la_SOURCES = \
|
||||||
if USE_LIBARCHIVE
|
if USE_LIBARCHIVE
|
||||||
libostree_1_la_SOURCES += src/libostree/ostree-libarchive-input-stream.h \
|
libostree_1_la_SOURCES += src/libostree/ostree-libarchive-input-stream.h \
|
||||||
src/libostree/ostree-libarchive-input-stream.c \
|
src/libostree/ostree-libarchive-input-stream.c \
|
||||||
|
src/libostree/ostree-libarchive-private.h \
|
||||||
$(NULL)
|
$(NULL)
|
||||||
endif
|
endif
|
||||||
if HAVE_LIBSOUP_CLIENT_CERTS
|
if HAVE_LIBSOUP_CLIENT_CERTS
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
|
#include "ostree-libarchive-private.h"
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016 Colin Walters <walters@verbum.org>
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation; either
|
||||||
|
* version 2 of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General
|
||||||
|
* Public License along with this library; if not, write to the
|
||||||
|
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||||
|
* Boston, MA 02111-1307, USA.
|
||||||
|
*
|
||||||
|
* Author: Alexander Larsson <alexl@redhat.com>
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <gio/gio.h>
|
||||||
|
#include "libglnx.h"
|
||||||
|
#ifdef HAVE_LIBARCHIVE
|
||||||
|
#include <archive.h>
|
||||||
|
#include <archive_entry.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
|
#ifdef HAVE_LIBARCHIVE
|
||||||
|
GLNX_DEFINE_CLEANUP_FUNCTION (void *, flatpak_local_free_write_archive, archive_write_free)
|
||||||
|
#define ot_cleanup_write_archive __attribute__((cleanup (flatpak_local_free_write_archive)))
|
||||||
|
|
||||||
|
GLNX_DEFINE_CLEANUP_FUNCTION (void *, flatpak_local_free_read_archive, archive_read_free)
|
||||||
|
#define ot_cleanup_read_archive __attribute__((cleanup (flatpak_local_free_read_archive)))
|
||||||
|
#endif
|
||||||
|
|
||||||
|
G_END_DECLS
|
||||||
|
|
@ -913,10 +913,9 @@ ostree_repo_write_archive_to_mtree (OstreeRepo *self,
|
||||||
{
|
{
|
||||||
#ifdef HAVE_LIBARCHIVE
|
#ifdef HAVE_LIBARCHIVE
|
||||||
gboolean ret = FALSE;
|
gboolean ret = FALSE;
|
||||||
struct archive *a = NULL;
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0, };
|
OstreeRepoImportArchiveOptions opts = { 0, };
|
||||||
|
|
||||||
a = archive_read_new ();
|
|
||||||
#ifdef HAVE_ARCHIVE_READ_SUPPORT_FILTER_ALL
|
#ifdef HAVE_ARCHIVE_READ_SUPPORT_FILTER_ALL
|
||||||
archive_read_support_filter_all (a);
|
archive_read_support_filter_all (a);
|
||||||
#else
|
#else
|
||||||
|
|
@ -945,7 +944,6 @@ ostree_repo_write_archive_to_mtree (OstreeRepo *self,
|
||||||
if (a)
|
if (a)
|
||||||
{
|
{
|
||||||
(void)archive_read_close (a);
|
(void)archive_read_close (a);
|
||||||
(void)archive_read_free (a);
|
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
#else
|
#else
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
#include "ot-builtins.h"
|
#include "ot-builtins.h"
|
||||||
#include "ostree.h"
|
#include "ostree.h"
|
||||||
#include "ostree-repo-file.h"
|
#include "ostree-repo-file.h"
|
||||||
|
#include "ostree-libarchive-private.h"
|
||||||
#include "otutil.h"
|
#include "otutil.h"
|
||||||
|
|
||||||
#ifdef HAVE_LIBARCHIVE
|
#ifdef HAVE_LIBARCHIVE
|
||||||
|
|
@ -67,7 +68,7 @@ ostree_builtin_export (int argc, char **argv, GCancellable *cancellable, GError
|
||||||
g_autoptr(GFile) subtree = NULL;
|
g_autoptr(GFile) subtree = NULL;
|
||||||
g_autofree char *commit = NULL;
|
g_autofree char *commit = NULL;
|
||||||
g_autoptr(GVariant) commit_data = NULL;
|
g_autoptr(GVariant) commit_data = NULL;
|
||||||
struct archive *a = NULL;
|
ot_cleanup_write_archive struct archive *a = NULL;
|
||||||
OstreeRepoExportArchiveOptions opts = { 0, };
|
OstreeRepoExportArchiveOptions opts = { 0, };
|
||||||
|
|
||||||
context = g_option_context_new ("COMMIT - Stream COMMIT to stdout in tar format");
|
context = g_option_context_new ("COMMIT - Stream COMMIT to stdout in tar format");
|
||||||
|
|
@ -154,10 +155,6 @@ ostree_builtin_export (int argc, char **argv, GCancellable *cancellable, GError
|
||||||
|
|
||||||
ret = TRUE;
|
ret = TRUE;
|
||||||
out:
|
out:
|
||||||
#ifdef HAVE_LIBARCHIVE
|
|
||||||
if (a)
|
|
||||||
archive_write_free (a);
|
|
||||||
#endif
|
|
||||||
if (context)
|
if (context)
|
||||||
g_option_context_free (context);
|
g_option_context_free (context);
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include <ostree.h>
|
#include <ostree.h>
|
||||||
|
#include "ostree-libarchive-private.h"
|
||||||
#include <archive.h>
|
#include <archive.h>
|
||||||
#include <archive_entry.h>
|
#include <archive_entry.h>
|
||||||
|
|
||||||
|
|
@ -40,7 +41,7 @@ static void
|
||||||
test_data_init (TestData *td)
|
test_data_init (TestData *td)
|
||||||
{
|
{
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
struct archive *a = archive_write_new ();
|
ot_cleanup_write_archive struct archive *a = archive_write_new ();
|
||||||
struct archive_entry *ae;
|
struct archive_entry *ae;
|
||||||
uid_t uid = getuid ();
|
uid_t uid = getuid ();
|
||||||
gid_t gid = getgid ();
|
gid_t gid = getgid ();
|
||||||
|
|
@ -115,12 +116,12 @@ test_data_init (TestData *td)
|
||||||
archive_entry_free (ae);
|
archive_entry_free (ae);
|
||||||
|
|
||||||
g_assert_cmpint (ARCHIVE_OK, ==, archive_write_close (a));
|
g_assert_cmpint (ARCHIVE_OK, ==, archive_write_close (a));
|
||||||
g_assert_cmpint (ARCHIVE_OK, ==, archive_write_free (a));
|
|
||||||
|
|
||||||
td->fd_empty = openat (AT_FDCWD, "empty.tar.gz", O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0644);
|
td->fd_empty = openat (AT_FDCWD, "empty.tar.gz", O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0644);
|
||||||
g_assert (td->fd_empty >= 0);
|
g_assert (td->fd_empty >= 0);
|
||||||
(void) unlink ("empty.tar.gz");
|
(void) unlink ("empty.tar.gz");
|
||||||
|
|
||||||
|
g_assert_cmpint (ARCHIVE_OK, ==, archive_write_free (a));
|
||||||
a = archive_write_new ();
|
a = archive_write_new ();
|
||||||
g_assert (a);
|
g_assert (a);
|
||||||
|
|
||||||
|
|
@ -128,7 +129,6 @@ test_data_init (TestData *td)
|
||||||
g_assert_cmpint (0, ==, archive_write_add_filter_gzip (a));
|
g_assert_cmpint (0, ==, archive_write_add_filter_gzip (a));
|
||||||
g_assert_cmpint (0, ==, archive_write_open_fd (a, td->fd_empty));
|
g_assert_cmpint (0, ==, archive_write_open_fd (a, td->fd_empty));
|
||||||
g_assert_cmpint (ARCHIVE_OK, ==, archive_write_close (a));
|
g_assert_cmpint (ARCHIVE_OK, ==, archive_write_close (a));
|
||||||
g_assert_cmpint (ARCHIVE_OK, ==, archive_write_free (a));
|
|
||||||
|
|
||||||
{ g_autoptr(GFile) repopath = g_file_new_for_path ("repo");
|
{ g_autoptr(GFile) repopath = g_file_new_for_path ("repo");
|
||||||
td->repo = ostree_repo_new (repopath);
|
td->repo = ostree_repo_new (repopath);
|
||||||
|
|
@ -165,7 +165,7 @@ test_libarchive_noautocreate_empty (gconstpointer data)
|
||||||
{
|
{
|
||||||
TestData *td = (void*)data;
|
TestData *td = (void*)data;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
struct archive *a = archive_read_new ();
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0, };
|
OstreeRepoImportArchiveOptions opts = { 0, };
|
||||||
glnx_unref_object OstreeMutableTree *mtree = ostree_mutable_tree_new ();
|
glnx_unref_object OstreeMutableTree *mtree = ostree_mutable_tree_new ();
|
||||||
|
|
||||||
|
|
@ -181,7 +181,7 @@ test_libarchive_autocreate_empty (gconstpointer data)
|
||||||
{
|
{
|
||||||
TestData *td = (void*)data;
|
TestData *td = (void*)data;
|
||||||
g_autoptr(GError) error = NULL;
|
g_autoptr(GError) error = NULL;
|
||||||
struct archive *a = archive_read_new ();
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0, };
|
OstreeRepoImportArchiveOptions opts = { 0, };
|
||||||
glnx_unref_object OstreeMutableTree *mtree = ostree_mutable_tree_new ();
|
glnx_unref_object OstreeMutableTree *mtree = ostree_mutable_tree_new ();
|
||||||
|
|
||||||
|
|
@ -199,7 +199,7 @@ test_libarchive_error_device_file (gconstpointer data)
|
||||||
{
|
{
|
||||||
TestData *td = (void*)data;
|
TestData *td = (void*)data;
|
||||||
g_autoptr(GError) error = NULL;
|
g_autoptr(GError) error = NULL;
|
||||||
struct archive *a = archive_read_new ();
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0, };
|
OstreeRepoImportArchiveOptions opts = { 0, };
|
||||||
glnx_unref_object OstreeMutableTree *mtree = ostree_mutable_tree_new ();
|
glnx_unref_object OstreeMutableTree *mtree = ostree_mutable_tree_new ();
|
||||||
|
|
||||||
|
|
@ -207,6 +207,7 @@ test_libarchive_error_device_file (gconstpointer data)
|
||||||
|
|
||||||
(void)ostree_repo_import_archive_to_mtree (td->repo, &opts, a, mtree, NULL, NULL, &error);
|
(void)ostree_repo_import_archive_to_mtree (td->repo, &opts, a, mtree, NULL, NULL, &error);
|
||||||
g_assert (error != NULL);
|
g_assert (error != NULL);
|
||||||
|
g_clear_error (&error);
|
||||||
}
|
}
|
||||||
|
|
||||||
static gboolean
|
static gboolean
|
||||||
|
|
@ -268,8 +269,8 @@ static void
|
||||||
test_libarchive_ignore_device_file (gconstpointer data)
|
test_libarchive_ignore_device_file (gconstpointer data)
|
||||||
{
|
{
|
||||||
TestData *td = (void*)data;
|
TestData *td = (void*)data;
|
||||||
GError *error = NULL;
|
g_autoptr(GError) error = NULL;
|
||||||
struct archive *a = archive_read_new ();
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0, };
|
OstreeRepoImportArchiveOptions opts = { 0, };
|
||||||
|
|
||||||
if (skip_if_no_xattr (td))
|
if (skip_if_no_xattr (td))
|
||||||
|
|
@ -331,7 +332,7 @@ test_libarchive_ostree_convention (gconstpointer data)
|
||||||
{
|
{
|
||||||
TestData *td = (void*)data;
|
TestData *td = (void*)data;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
struct archive *a = archive_read_new ();
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0, };
|
OstreeRepoImportArchiveOptions opts = { 0, };
|
||||||
|
|
||||||
if (skip_if_no_xattr (td))
|
if (skip_if_no_xattr (td))
|
||||||
|
|
@ -373,7 +374,7 @@ test_libarchive_xattr_callback (gconstpointer data)
|
||||||
{
|
{
|
||||||
TestData *td = (void*)data;
|
TestData *td = (void*)data;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
struct archive *a = archive_read_new ();
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0 };
|
OstreeRepoImportArchiveOptions opts = { 0 };
|
||||||
OstreeRepoCommitModifier *modifier = NULL;
|
OstreeRepoCommitModifier *modifier = NULL;
|
||||||
char buf[7] = { 0 };
|
char buf[7] = { 0 };
|
||||||
|
|
@ -428,7 +429,7 @@ static void
|
||||||
entry_pathname_test_helper (gconstpointer data, gboolean on)
|
entry_pathname_test_helper (gconstpointer data, gboolean on)
|
||||||
{
|
{
|
||||||
TestData *td = (void*)data; GError *error = NULL;
|
TestData *td = (void*)data; GError *error = NULL;
|
||||||
struct archive *a = archive_read_new ();
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0, };
|
OstreeRepoImportArchiveOptions opts = { 0, };
|
||||||
OstreeRepoCommitModifier *modifier = NULL;
|
OstreeRepoCommitModifier *modifier = NULL;
|
||||||
gboolean met_etc_file = FALSE;
|
gboolean met_etc_file = FALSE;
|
||||||
|
|
@ -468,7 +469,6 @@ entry_pathname_test_helper (gconstpointer data, gboolean on)
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
archive_read_free (a);
|
|
||||||
ostree_repo_commit_modifier_unref (modifier);
|
ostree_repo_commit_modifier_unref (modifier);
|
||||||
out:
|
out:
|
||||||
g_assert_no_error (error);
|
g_assert_no_error (error);
|
||||||
|
|
@ -491,7 +491,7 @@ test_libarchive_selinux (gconstpointer data)
|
||||||
{
|
{
|
||||||
TestData *td = (void*)data;
|
TestData *td = (void*)data;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
struct archive *a = archive_read_new ();
|
ot_cleanup_read_archive struct archive *a = archive_read_new ();
|
||||||
OstreeRepoImportArchiveOptions opts = { 0 };
|
OstreeRepoImportArchiveOptions opts = { 0 };
|
||||||
glnx_unref_object OstreeSePolicy *sepol = NULL;
|
glnx_unref_object OstreeSePolicy *sepol = NULL;
|
||||||
OstreeRepoCommitModifier *modifier = NULL;
|
OstreeRepoCommitModifier *modifier = NULL;
|
||||||
|
|
@ -536,7 +536,6 @@ test_libarchive_selinux (gconstpointer data)
|
||||||
g_assert_cmpstr (buf, ==, "system_u:object_r:etc_t:s0");
|
g_assert_cmpstr (buf, ==, "system_u:object_r:etc_t:s0");
|
||||||
|
|
||||||
out:
|
out:
|
||||||
archive_read_free (a);
|
|
||||||
if (modifier)
|
if (modifier)
|
||||||
ostree_repo_commit_modifier_unref (modifier);
|
ostree_repo_commit_modifier_unref (modifier);
|
||||||
g_assert_no_error (error);
|
g_assert_no_error (error);
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@
|
||||||
#include <gio/gio.h>
|
#include <gio/gio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "ot-opt-utils.h"
|
#include "ot-opt-utils.h"
|
||||||
|
#include "libglnx.h"
|
||||||
|
|
||||||
static GString *printerr_str = NULL;
|
static GString *printerr_str = NULL;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue