[PATCH] Restore support for clang and other non-GCC compilers

Charlotte Delenk <darkkirb at darkkirb.de>
Newsgroups dev.linux.lists.ell
Message-ID <[email protected]>
The _auto_-macro used in cert.c and tls.c uses a GCC extension that
allows for nested functions. This is not supported by other compilers,
including clang.

Compiling ell with these compilers will result error messages such as:

../ell-0.44/ell/tls.c:1895:2: error: function definition is not allowed here
        _auto_(l_certchain_free) struct l_certchain *certchain = NULL;
        ^
../ell-0.44/ell/useful.h:71:2: note: expanded from macro '_auto_'
        _AUTODESTRUCT(__COUNTER__, func)
        ^
../ell-0.44/ell/useful.h:68:2: note: expanded from macro '_AUTODESTRUCT'
        __AUTODESTRUCT(var, func)
        ^
../ell-0.44/ell/useful.h:64:2: note: expanded from macro '__AUTODESTRUCT'
        { func(*(void **) ptr); }                       \
        ^
../ell-0.44/ell/tls.c:1895:2: error: use of undeclared identifier 'cleanup_0'
../ell-0.44/ell/useful.h:71:2: note: expanded from macro '_auto_'
        _AUTODESTRUCT(__COUNTER__, func)
        ^
../ell-0.44/ell/useful.h:68:2: note: expanded from macro '_AUTODESTRUCT'
        __AUTODESTRUCT(var, func)
        ^
../ell-0.44/ell/useful.h:65:23: note: expanded from macro '__AUTODESTRUCT'
        __attribute((cleanup(cleanup_ ## var)))
                             ^

This patch will removes the _auto_ macro and implement its functionality
manually in the two locations it is used.

It does not appear to be possible to replace this macro with a more
compatible alternative that does not involve C++.

Signed-off-by: Charlotte Delenk <darkkirb(a)darkkirb.de>
---
 ell/cert.c   |  6 +++++-
 ell/tls.c    |  6 +++++-
 ell/useful.h | 11 -----------
 3 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/ell/cert.c b/ell/cert.c
index 141ea1c..13a89e0 100644
--- a/ell/cert.c
+++ b/ell/cert.c
@@ -446,12 +446,16 @@ static struct l_key *cert_try_link(struct l_cert *cert, struct l_keyring *ring)
 		return false;	\
 	} while (0)
 
+static void cleanup_keyring(void * ptr) {
+	l_keyring_free(*(void **)ptr);
+}
+
 LIB_EXPORT bool l_certchain_verify(struct l_certchain *chain,
 					struct l_queue *ca_certs,
 					const char **error)
 {
 	struct l_keyring *ca_ring = NULL;
-	_auto_(l_keyring_free) struct l_keyring *verify_ring = NULL;
+	__attribute__((cleanup(cleanup_keyring))) struct l_keyring *verify_ring = NULL;
 	struct l_cert *cert;
 	struct l_key *prev_key = NULL;
 	int verified = 0;
diff --git a/ell/tls.c b/ell/tls.c
index c246f1f..1d0e91d 100644
--- a/ell/tls.c
+++ b/ell/tls.c
@@ -1888,11 +1888,15 @@ decode_error:
 			"ServerHello decode error");
 }
 
+static void cleanup_certchain(void * ptr) {
+	l_certchain_free(*(void **)ptr);
+}
+
 static void tls_handle_certificate(struct l_tls *tls,
 					const uint8_t *buf, size_t len)
 {
 	size_t total;
-	_auto_(l_certchain_free) struct l_certchain *certchain = NULL;
+	__attribute__((cleanup(cleanup_certchain))) struct l_certchain *certchain = NULL;
 	struct l_cert *leaf;
 	size_t der_len;
 	const uint8_t *der;
diff --git a/ell/useful.h b/ell/useful.h
index b4783ce..cd7ec0f 100644
--- a/ell/useful.h
+++ b/ell/useful.h
@@ -59,17 +59,6 @@ static inline unsigned char bit_field(const unsigned char oct,
 	_x / _d;					\
 })
 
-#define __AUTODESTRUCT(var, func)			\
-	void cleanup_ ## var(void *ptr)			\
-	{ func(*(void **) ptr); }			\
-	__attribute((cleanup(cleanup_ ## var)))
-
-#define _AUTODESTRUCT(var, func)			\
-	__AUTODESTRUCT(var, func)
-
-#define _auto_(func)					\
-	_AUTODESTRUCT(__COUNTER__, func)
-
 /*
  * Trick the compiler into thinking that var might be changed somehow by
  * the asm
-- 
2.33.0
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEEIG2l4doJBLbuSRa6PO9d2pFa7LAFAmFG4b8ACgkQPO9d2pFa
7LCA0BAApTEe4q7IhdmBmKRKWdIcYL92y88d2BaPzDAUlTsP1vryqKgFDAP0SZM5
KLiloCChClfAt8cS0Via73h1nuxSu3KSfDM6hOf61r24QdG9EsOtW528W8d18fZg
23ek5tYwwS6zwGday5cDv5eDzQHl0qeDgBQn9b4jtg6LD47ZrWCayKzy09P26ad2
hfFjahiuKXJlpiZoS1rbRyXXRo4jtU8hd5RD7KXHsPXyTG6INhWeDu3oJdVEK+JE
kaS90lew6E9Tc30sgeo4X+7RARy6+Do9I3KltBT0g6d5KRuX9MKcQJfDj3CBvRqE
96bIp5I7SO6EJ3WtZUZ5OIDBKO7xCoZ1F2AAnbc4Viuzdd2s/2t98IPXMfgWe56x
56onPgVsm+5Dl9MJ7Kshqw+yAy5mz4UL1GmQMDrOqrwKymoUhv9EE8cQP42NP7SO
rWVhXrWuCo5flfAVFjc15zQSKzSoWwm1SN5ll6i+wk3DhLWHifN+7bVTBs+bV+sh
2HOo8mXjlXUWtD1yaUMVz6/asvJzoNCUuHV61j55W2kRe1B24NuoMSsFKWE0bALc
iN8iaKjm50rquKGxpF3kPXVYasyE7GsY9UNgGIcNaVEOYAixil31qZ8uraHNarBu
gQiQXtlPFQmWgtEzFj8vBNNWb5rROE+hMnOJARxBJmk3J4oTi4s=
=lry5
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.