Linux bluetooth development
 help / color / mirror / Atom feed
From: Bastien Nocera <hadess@hadess.net>
To: linux-bluetooth@vger.kernel.org
Subject: [BlueZ v3 2/6] shared/util: Add helper for "cleanup" variable attribute
Date: Mon, 11 May 2026 15:18:05 +0200	[thread overview]
Message-ID: <20260511132131.1283892-3-hadess@hadess.net> (raw)
In-Reply-To: <20260511132131.1283892-1-hadess@hadess.net>

Use the widespread "cleanup" variable attribute:
https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-cleanup

It is implemented by both GCC and clang on platforms where bluez is
used, and can help reduce memory leaks, while improving readability.

This implements:
- generic cleanup (_cleanup_free_)
- cleanup with specific free function (_cleanup_())
- cleanup for specific types (_cleanup_type_(type))
- cleanup for file descriptors
- capturing a variable before it is freed (so it is only freed in error
  paths for example, _steal_() and _steal_fd())

This commit includes tests which should cover all those new helpers.

See also:
https://systemd.io/CODING_STYLE/#memory-allocation
https://docs.gtk.org/glib/auto-cleanup.html
---
 src/shared/util.h | 40 +++++++++++++++++++++++++++++
 unit/test-util.c  | 65 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)

diff --git a/src/shared/util.h b/src/shared/util.h
index 67629dddfaa9..c8ce90c5f1b5 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -15,9 +15,11 @@
 #include <stdbool.h>
 #include <alloca.h>
 #include <byteswap.h>
+#include <errno.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/uio.h>
+#include <unistd.h>
 
 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
 #define BIT(n)  (1 << (n))
@@ -93,6 +95,44 @@ do {						\
 #define newa(t, n) ((t*) alloca(sizeof(t)*(n)))
 #define malloc0(n) (calloc(1, (n)))
 
+static inline void freep(void *p)
+{
+	free(*(void **) p);
+}
+
+static inline void * _steal_(void *p)
+{
+	void **orig = (void **) p;
+	void *ret = *orig;
+	*orig = NULL;
+	return ret;
+}
+
+static inline int _steal_fd_(int *fdp)
+{
+	int fd = *fdp;
+	*fdp = -1;
+	return fd;
+}
+
+static inline void closefd(int *fdp)
+{
+	int errno_save = errno;
+	int fd = *fdp;
+	*fdp = -1;
+	if (fd < 0)
+		return;
+	close(fd);
+	errno = errno_save;
+}
+
+#define CLEANUP_FREEFUNC(type, func) static inline void cleanup_##type (type **_ptr) { (func) (*_ptr); }
+
+#define _cleanup_(f) __attribute__((cleanup(f)))
+#define _cleanup_type_(type) __attribute__((cleanup(cleanup_##type)))
+#define _cleanup_free_ _cleanup_(freep)
+#define _cleanup_fd_ _cleanup_(closefd)
+
 char *strdelimit(char *str, char *del, char c);
 int strsuffix(const char *str, const char *suffix);
 char *strstrip(char *str);
diff --git a/unit/test-util.c b/unit/test-util.c
index 443c4f70362c..1672b32eb39c 100644
--- a/unit/test-util.c
+++ b/unit/test-util.c
@@ -9,14 +9,73 @@
  */
 
 #include <assert.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #include "src/shared/util.h"
 #include "src/shared/tester.h"
+#include "bluetooth/bluetooth.h"
 
 /* XXX glib.h must not be included, or it will clobber the
  * MIN/MAX macros.
  */
 
+static void test_cleanup_free(const void *data)
+{
+	_cleanup_free_ char *p1 = NULL;
+	_cleanup_free_ char *p2 = NULL;
+	_cleanup_free_ char *is_null = NULL;
+
+	p1 = malloc0(10);
+	p2 = malloc0(15);
+
+	p1[0] = 1;
+	p2[0] = 1;
+
+	{
+		_cleanup_free_ uint8_t *data = NULL;
+		_cleanup_free_ uint8_t *is_null_too = NULL;
+
+		data = malloc0(128);
+		data[0] = 1;
+
+		assert(is_null_too == NULL);
+	}
+	{
+		_cleanup_free_ uint8_t *data = NULL;
+		data = malloc0(128 * 2);
+		data[0] = 3;
+	}
+
+	assert(is_null == NULL);
+	tester_test_passed();
+}
+
+CLEANUP_FREEFUNC(bdaddr_t, free);
+
+static void test_cleanup_type(const void *data)
+{
+#define ADDR "FF:FF:FF:FF:FF:FF"
+	_cleanup_type_(bdaddr_t) bdaddr_t *address = NULL;
+	char str[33];
+
+	address = strtoba(ADDR);
+	assert(bacmp(address, BDADDR_ALL) == 0);
+	printf("%d = ba2str(address, str)\n", ba2str(address, str));
+	assert(ba2str(address, str) == 17);
+	assert(strcmp(str, ADDR) == 0);
+	tester_test_passed();
+}
+
+static void test_cleanup_fd(const void *data)
+{
+	_cleanup_fd_ int fd = -1;
+
+	fd = open("/dev/null", O_RDONLY);
+	assert(fd != 0);
+	tester_test_passed();
+}
+
 static void test_min_max(const void *data)
 {
 	assert(MIN(3, 4) == 3);
@@ -30,6 +89,12 @@ int main(int argc, char *argv[])
 
 	tester_add("/util/min_max", NULL, NULL,
 			test_min_max, NULL);
+	tester_add("/util/cleanup_free", NULL, NULL,
+			test_cleanup_free, NULL);
+	tester_add("/util/cleanup_type", NULL, NULL,
+			test_cleanup_type, NULL);
+	tester_add("/util/cleanup_fd", NULL, NULL,
+			test_cleanup_fd, NULL);
 
 	return tester_run();
 }
-- 
2.54.0


  parent reply	other threads:[~2026-05-11 13:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-11 13:18 [BlueZ v3 0/6] Add helper for "cleanup" variable attribute Bastien Nocera
2026-05-11 13:18 ` [BlueZ v3 1/6] all: Remove more unneeded MIN/MAX macro definition Bastien Nocera
2026-05-11 17:27   ` Add helper for "cleanup" variable attribute bluez.test.bot
2026-05-11 13:18 ` Bastien Nocera [this message]
2026-05-11 13:18 ` [BlueZ v3 3/6] doc: Recommend using _cleanup_ and friends Bastien Nocera
2026-05-11 13:18 ` [BlueZ v3 4/6] main: Use _cleanup_() to simplify configuration parsing Bastien Nocera
2026-05-11 13:35   ` [BlueZ,v3,4/6] " bluez.test.bot
2026-05-11 13:18 ` [BlueZ v3 5/6] client: Use _cleanup_fd_ to simplify urandom access Bastien Nocera
2026-05-11 13:18 ` [BlueZ v3 6/6] btattach: Use _cleanup_fd_ to simplify error paths Bastien Nocera
2026-05-12 19:20 ` [BlueZ v3 0/6] Add helper for "cleanup" variable attribute patchwork-bot+bluetooth

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260511132131.1283892-3-hadess@hadess.net \
    --to=hadess@hadess.net \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox