From f8e3736385a5530945b3dfd893dc888ede7d7ba3 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Apr 2022 14:35:44 +0100 Subject: [PATCH 1/4] dbus: Move DBUS_ENABLE_EMBEDDED_TESTS to a new dbus-macros-internal.h This avoids a circular dependency: I want to use DBUS_ENABLE_EMBEDDED_TESTS in dbus-string.h, but DBUS_ENABLE_EMBEDDED_TESTS was previously defined in dbus-internals.h, which depends on dbus-string.h. Signed-off-by: Simon McVittie --- dbus/CMakeLists.txt | 1 + dbus/Makefile.am | 1 + dbus/dbus-internals.h | 7 +------ dbus/dbus-macros-internal.h | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 dbus/dbus-macros-internal.h diff --git a/dbus/CMakeLists.txt b/dbus/CMakeLists.txt index 9e3f0a14..2f56e62c 100644 --- a/dbus/CMakeLists.txt +++ b/dbus/CMakeLists.txt @@ -135,6 +135,7 @@ set(DBUS_SHARED_HEADERS dbus-hash.h dbus-internals.h dbus-list.h + dbus-macros-internal.h dbus-marshal-basic.h dbus-mempool.h dbus-string.h diff --git a/dbus/Makefile.am b/dbus/Makefile.am index 7a71f468..fbd81758 100644 --- a/dbus/Makefile.am +++ b/dbus/Makefile.am @@ -227,6 +227,7 @@ DBUS_SHARED_SOURCES= \ dbus-internals.h \ dbus-list.c \ dbus-list.h \ + dbus-macros-internal.h \ dbus-marshal-basic.c \ dbus-marshal-basic.h \ dbus-memory.c \ diff --git a/dbus/dbus-internals.h b/dbus/dbus-internals.h index 3b293b05..1ca0065d 100644 --- a/dbus/dbus-internals.h +++ b/dbus/dbus-internals.h @@ -31,16 +31,11 @@ #include #include #include +#include #include DBUS_BEGIN_DECLS -#ifdef DBUS_ENABLE_EMBEDDED_TESTS -#define DBUS_EMBEDDED_TESTS_EXPORT DBUS_PRIVATE_EXPORT -#else -#define DBUS_EMBEDDED_TESTS_EXPORT /* nothing */ -#endif - DBUS_PRIVATE_EXPORT void _dbus_warn (const char *format, ...) _DBUS_GNUC_PRINTF (1, 2); diff --git a/dbus/dbus-macros-internal.h b/dbus/dbus-macros-internal.h new file mode 100644 index 00000000..d98891a8 --- /dev/null +++ b/dbus/dbus-macros-internal.h @@ -0,0 +1,36 @@ +/* + * Copyright © 2019 Collabora Ltd. + * SPDX-License-Identifier: AFL-2.1 or GPL-2.0-or-later + * + * Licensed under the Academic Free License version 2.1 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifdef DBUS_INSIDE_DBUS_H +#error "You can't include dbus-macros-internal.h in the public header dbus.h" +#endif + +#ifndef DBUS_MACROS_INTERNAL_H +#define DBUS_MACROS_INTERNAL_H + +#include + +#ifdef DBUS_ENABLE_EMBEDDED_TESTS +# define DBUS_EMBEDDED_TESTS_EXPORT DBUS_PRIVATE_EXPORT +#else +# define DBUS_EMBEDDED_TESTS_EXPORT /* nothing */ +#endif + +#endif From 76fe49b7ab7d4835f23e1e3afb6233d768a4cd3f Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Thu, 21 Apr 2022 14:38:55 +0100 Subject: [PATCH 2/4] dbus: Move DBUS_PRIVATE_EXPORT to private header Unlike the rest of the macros in dbus-macros.h, this one would never have made sense to use in a public header. Signed-off-by: Simon McVittie --- dbus/dbus-macros-internal.h | 19 ++++++++++++++++++- dbus/dbus-macros.h | 16 ---------------- dbus/dbus-uuidgen.h | 1 + 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/dbus/dbus-macros-internal.h b/dbus/dbus-macros-internal.h index d98891a8..5da73c26 100644 --- a/dbus/dbus-macros-internal.h +++ b/dbus/dbus-macros-internal.h @@ -1,5 +1,6 @@ /* - * Copyright © 2019 Collabora Ltd. + * Copyright © 2010-2015 Ralf Habacker + * Copyright © 2015-2019 Collabora Ltd. * SPDX-License-Identifier: AFL-2.1 or GPL-2.0-or-later * * Licensed under the Academic Free License version 2.1 @@ -33,4 +34,20 @@ # define DBUS_EMBEDDED_TESTS_EXPORT /* nothing */ #endif +#if defined(DBUS_PRIVATE_EXPORT) + /* value forced by compiler command line, don't redefine */ +#elif defined(_WIN32) +# if defined(DBUS_STATIC_BUILD) +# define DBUS_PRIVATE_EXPORT /* no decoration */ +# elif defined(dbus_1_EXPORTS) +# define DBUS_PRIVATE_EXPORT __declspec(dllexport) +# else +# define DBUS_PRIVATE_EXPORT __declspec(dllimport) +# endif +#elif defined(__GNUC__) && __GNUC__ >= 4 +# define DBUS_PRIVATE_EXPORT __attribute__ ((__visibility__ ("default"))) +#else +# define DBUS_PRIVATE_EXPORT /* no decoration */ +#endif + #endif diff --git a/dbus/dbus-macros.h b/dbus/dbus-macros.h index 80faed29..2dd9fcd3 100644 --- a/dbus/dbus-macros.h +++ b/dbus/dbus-macros.h @@ -210,22 +210,6 @@ #define DBUS_EXPORT #endif -#if defined(DBUS_PRIVATE_EXPORT) - /* value forced by compiler command line, don't redefine */ -#elif defined(_WIN32) -# if defined(DBUS_STATIC_BUILD) -# define DBUS_PRIVATE_EXPORT /* no decoration */ -# elif defined(dbus_1_EXPORTS) -# define DBUS_PRIVATE_EXPORT __declspec(dllexport) -# else -# define DBUS_PRIVATE_EXPORT __declspec(dllimport) -# endif -#elif defined(__GNUC__) && __GNUC__ >= 4 -# define DBUS_PRIVATE_EXPORT __attribute__ ((__visibility__ ("default"))) -#else -# define DBUS_PRIVATE_EXPORT /* no decoration */ -#endif - /* Implementation for dbus_clear_message() etc. This is not API, * do not use it directly. * diff --git a/dbus/dbus-uuidgen.h b/dbus/dbus-uuidgen.h index bf07b139..bd098b03 100644 --- a/dbus/dbus-uuidgen.h +++ b/dbus/dbus-uuidgen.h @@ -27,6 +27,7 @@ #ifndef DBUS_UUIDGEN_H #define DBUS_UUIDGEN_H +#include #include #include From 2e63543b932ad72234e2cb84e29714d56b247dd1 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Thu, 7 Apr 2022 12:24:26 +0200 Subject: [PATCH 3/4] dbus-string: Add _dbus_string_append_buffer_as_hex() This function provides a portable way to print data as hex values. Signed-off-by: Ralf Habacker [smcv: Only compile this when needed, improve assertions, coding style] Signed-off-by: Simon McVittie --- dbus/dbus-string.c | 35 +++++++++++++++++++++++++++++++++++ dbus/dbus-string.h | 6 ++++++ 2 files changed, 41 insertions(+) diff --git a/dbus/dbus-string.c b/dbus/dbus-string.c index 9c917cd9..6ea1c039 100644 --- a/dbus/dbus-string.c +++ b/dbus/dbus-string.c @@ -2345,6 +2345,41 @@ _dbus_string_append_byte_as_hex (DBusString *str, return TRUE; } +/* Currently only used when embedded tests are enabled */ +#ifdef DBUS_ENABLE_EMBEDDED_TESTS +/** + * Appends \p size bytes from the buffer \p buf as hex digits to the string \p str + * + * If \p size is nonzero, then \p buf must be non-NULL. + * + * @param str the string + * @param buf the buffer to add bytes from + * @param size the number of bytes to add + * @returns #FALSE if no memory + */ +dbus_bool_t +_dbus_string_append_buffer_as_hex (DBusString *str, + void *buf, + int size) +{ + unsigned char *p; + int i; + + _dbus_assert (size >= 0); + _dbus_assert (size == 0 || buf != NULL); + + p = (unsigned char *) buf; + + for (i = 0; i < size; i++) + { + if (!_dbus_string_append_byte_as_hex (str, p[i])) + return FALSE; + } + + return TRUE; +} +#endif + /** * Encodes a string in hex, the way MD5 and SHA-1 are usually * encoded. (Each byte is two hex digits.) diff --git a/dbus/dbus-string.h b/dbus/dbus-string.h index b75da1aa..dc223c61 100644 --- a/dbus/dbus-string.h +++ b/dbus/dbus-string.h @@ -31,6 +31,8 @@ #include +#include + DBUS_BEGIN_DECLS /** @@ -358,6 +360,10 @@ DBUS_PRIVATE_EXPORT void _dbus_string_chop_white (DBusString *str); dbus_bool_t _dbus_string_append_byte_as_hex (DBusString *str, unsigned char byte); +DBUS_EMBEDDED_TESTS_EXPORT +dbus_bool_t _dbus_string_append_buffer_as_hex (DBusString *str, + void *buf, + int size); DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_hex_encode (const DBusString *source, int start, From 29946ad7c139c1955cfa01e3e1bc83557529a4c0 Mon Sep 17 00:00:00 2001 From: Ralf Habacker Date: Thu, 7 Apr 2022 12:34:00 +0200 Subject: [PATCH 4/4] tests: Fix build warning "dereferencing type-punned pointer will break strict-aliasing rules" To avoid that build break in test-marshall-recursive-util.c the newly added function _dbus_string_append_buffer_as_hex() is used to print the hex bytes. Signed-off-by: Ralf Habacker --- test/internals/dbus-marshal-recursive-util.c | 29 +++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/test/internals/dbus-marshal-recursive-util.c b/test/internals/dbus-marshal-recursive-util.c index 0727225a..f05232f9 100644 --- a/test/internals/dbus-marshal-recursive-util.c +++ b/test/internals/dbus-marshal-recursive-util.c @@ -2625,13 +2625,28 @@ double_read_value (TestTypeNode *node, expected = double_from_seed (seed); if (!_DBUS_DOUBLES_BITWISE_EQUAL (v, expected)) - _dbus_test_fatal ("Expected double %g got %g\n" - " bits = 0x%" DBUS_INT64_MODIFIER "x vs.\n" - " bits = 0x%" DBUS_INT64_MODIFIER "x", - expected, v, - *(dbus_uint64_t*)(char*)&expected, - *(dbus_uint64_t*)(char*)&v); - + { + DBusString es = _DBUS_STRING_INIT_INVALID; + DBusString vs = _DBUS_STRING_INIT_INVALID; + if (!_dbus_string_init (&es)) + goto out; + if (!_dbus_string_init (&vs)) + goto out; + if (!_dbus_string_append_buffer_as_hex (&es, &expected, sizeof(double))) + goto out; + if (!_dbus_string_append_buffer_as_hex (&vs, &v, sizeof(double))) + goto out; + _dbus_test_fatal ("Expected double %g got %g\n" + " bits = 0x%s vs.\n" + " bits = 0x%s", + expected, v, + _dbus_string_get_const_data (&es), + _dbus_string_get_const_data (&vs)); +out: + _dbus_string_free (&es); + _dbus_string_free (&vs); + return FALSE; + } return TRUE; }