From: Simon McVittie Date: Fri, 15 Jul 2022 14:08:02 +0000 (+0100) Subject: dbus-launch-x11: Ignore X11 connection when checking for memory leaks X-Git-Tag: dbus-1.15.0~27^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5cd05bbb5a69a8d96084deafe2810d06a6f81e28;p=thirdparty%2Fdbus.git 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 --- diff --git a/dbus/dbus-internals.h b/dbus/dbus-internals.h index 96df3ff70..1272e8c5a 100644 --- a/dbus/dbus-internals.h +++ b/dbus/dbus-internals.h @@ -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 +/* 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 */ diff --git a/tools/dbus-launch-x11.c b/tools/dbus-launch-x11.c index 0b5d2928b..48ab51d89 100644 --- a/tools/dbus-launch-x11.c +++ b/tools/dbus-launch-x11.c @@ -37,6 +37,8 @@ #include #include +#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