dbus-launch-x11: Ignore X11 connection when checking for memory leaks

The X11 connection is opened and never closed. Because dbus-launch
forks and continues to run non-trivial code in a forked child, it is
not clear whether (or where) it would be safe to close it; instead, we
leave it open until process exit, at which point the socket is cleaned
up by the kernel.

Any memory allocated for the X11 connection is only allocated once per
run of dbus-launch, so there's no need to keep track of it, and we can
silence these memory leak warnings as uninteresting.

Signed-off-by: Simon McVittie <smcv@collabora.com>
This commit is contained in:
Simon McVittie 2022-07-15 15:08:02 +01:00
parent 9490157ef9
commit 5cd05bbb5a
2 changed files with 42 additions and 1 deletions

View file

@ -466,6 +466,28 @@ dbus_bool_t _dbus_get_local_machine_uuid_encoded (DBusString *uuid_str,
#define _DBUS_STRINGIFY(x) #x
#define _DBUS_FILE_LINE __FILE__ ":" _DBUS_STRINGIFY(__LINE__)
#ifndef __has_feature
# define __has_feature(x) 0
#endif
/* MSVC defines __SANITIZE_ADDRESS__, but does not provide the special
* builtins associated with it. */
#if ((defined(__SANITIZE_ADDRESS__) || __has_feature(address_sanitizer)) && \
!defined(_MSC_VER))
# include <sanitizer/lsan_interface.h>
/* Defined if we are building with AddressSanitizer */
# define _DBUS_ADDRESS_SANITIZER
/* Ignore memory allocations until the next _DBUS_END_IGNORE_LEAKS when
* checking for memory leaks */
# define _DBUS_BEGIN_IGNORE_LEAKS __lsan_disable ()
/* End the scope of a previous _DBUS_BEGIN_IGNORE_LEAKS */
# define _DBUS_END_IGNORE_LEAKS __lsan_enable ()
#else
# undef _DBUS_ADDRESS_SANITIZER
# define _DBUS_BEGIN_IGNORE_LEAKS do { } while (0)
# define _DBUS_END_IGNORE_LEAKS do { } while (0)
#endif
DBUS_END_DECLS
#endif /* DBUS_INTERNALS_H */

View file

@ -37,6 +37,8 @@
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include "dbus/dbus-internals.h"
Display *xdisplay = NULL;
static Atom selection_atom;
static Atom address_atom;
@ -451,7 +453,24 @@ x11_save_address (char *address, pid_t pid, long *wid)
int
x11_init (void)
{
return open_x11 () != NULL && init_x_atoms (xdisplay);
int ok;
/*
* The X11 connection is opened and never closed. Because dbus-launch
* forks and continues to run non-trivial code in a forked child, it is
* not clear whether (or where) it would be safe to close it; instead, we
* leave it open until process exit, at which point the socket is cleaned
* up by the kernel.
*
* Any memory allocated for the X11 connection is only allocated once per
* run of dbus-launch, so there's no need to keep track of it, and we can
* silence memory leak warnings from AddressSanitizer as uninteresting.
*/
_DBUS_BEGIN_IGNORE_LEAKS;
ok = open_x11 () != NULL && init_x_atoms (xdisplay);
_DBUS_END_IGNORE_LEAKS;
return ok;
}
void