Skip to contentSkip to conversation

Add helper for "cleanup" variable attribute

PATCH In review Bastien Nocera 5 participants 14 messages May 11 – Jun 9 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

v3 cover Add helper for "cleanup" variable attribute

As discussed in:
https://lore.kernel.org/linux-bluetooth/[email protected]/

Implement a cleanup helper.

The MIN/MAX fix is here because it touches the same hunk in src/main.c as the other patches. Feel free to pick it up straight away while the rest is discussed.

Changes since v2:
- Add macros to declare cleanup for specific types
- Add file descriptor cleanup
- Add "steal" helpers for fd and pointers
- Add unit tests

Changes since v1:
- Fixed checkpatch warnings

Bastien Nocera (6):
all: Remove more unneeded MIN/MAX macro definition shared/util: Add helper for "cleanup" variable attribute
doc: Recommend using _cleanup_ and friends
main: Use _cleanup_() to simplify configuration parsing
client: Use _cleanup_fd_ to simplify urandom access
btattach: Use _cleanup_fd_ to simplify error paths

 client/mgmt.c                 |   5 +-
 doc/maintainer-guidelines.rst |   3 +
 lib/bluetooth/hci.c           |   4 --
 src/main.c                    | 103 ++++++++++------------------------
 src/shared/gatt-server.c      |   8 ---
 src/shared/util.h             |  40 +++++++++++++
 tools/btattach.c              |  18 ++----
 unit/test-util.c              |  65 +++++++++++++++++++++
 8 files changed, 145 insertions(+), 101 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v3 1/6 all: Remove more unneeded MIN/MAX macro definition

---
 lib/bluetooth/hci.c      | 4 ----
 src/main.c               | 4 ----
 src/shared/gatt-server.c | 8 --------
 3 files changed, 16 deletions(-)
lib/bluetooth/hci.c +0 -4
index 44eea054b0ac..f50a30c610be 100644
@@ -33,10 +33,6 @@
 #include "hci.h"
 #include "hci_lib.h"
 
-#ifndef MIN
-#define MIN(x, y) ((x) < (y) ? (x) : (y))
-#endif
-
 typedef struct {
 	char *str;
 	unsigned int val;
src/main.c +0 -4
index 9a3d2da25d4d..8aa19a3e3346 100644
@@ -205,10 +205,6 @@ static const struct group_table {
 	{ }
 };
 
-#ifndef MIN
-#define MIN(x, y) ((x) < (y) ? (x) : (y))
-#endif
-
 static int8_t check_sirk_alpha_numeric(char *str)
 {
 	int8_t val = 0;
src/shared/gatt-server.c +0 -8
index 6273899965c0..709a8f94bb6a 100644
@@ -26,14 +26,6 @@
 #include "src/shared/util.h"
 #include "src/shared/timeout.h"
 
-#ifndef MAX
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
-#endif
-
-#ifndef MIN
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-#endif
-
 /*
  * TODO: This is an arbitrary limit. Come up with something reasonable or
  * perhaps an API to set this value if there is a use case for it.
Signature · 1 line
2.54.0
#2 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=1092801

---Test result---

Test Summary:

CheckPatch                    FAIL      2.71 seconds
GitLint                       PASS      1.76 seconds
BuildEll                      PASS      20.40 seconds
BluezMake                     PASS      666.47 seconds
MakeCheck                     PASS      18.72 seconds
MakeDistcheck                 PASS      245.36 seconds
CheckValgrind                 PASS      291.26 seconds
CheckSmatch                   WARNING   351.83 seconds
bluezmakeextell               PASS      186.73 seconds
IncrementalBuild              PASS      1330.28 seconds
ScanBuild                     PASS      1038.55 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[BlueZ,v3,2/6] shared/util: Add helper for "cleanup" variable attribute ERROR:POINTER_LOCATION: "foo * bar" should be "foo *bar" #106: FILE: src/shared/util.h:103:
+static inline void * _steal_(void *p)

WARNING:LONG_LINE: line length of 104 exceeds 80 columns
#132: FILE: src/shared/util.h:129:
+#define CLEANUP_FREEFUNC(type, func) static inline void cleanup_##type (type **_ptr) { (func) (*_ptr); }

WARNING:SPACING: space prohibited between function name and open parenthesis '('
#132: FILE: src/shared/util.h:129:
+#define CLEANUP_FREEFUNC(type, func) static inline void cleanup_##type (type **_ptr) { (func) (*_ptr); }

ERROR:SPACING: need consistent spacing around '*' (ctx:WxO)
#132: FILE: src/shared/util.h:129:
+#define CLEANUP_FREEFUNC(type, func) static inline void cleanup_##type (type **_ptr) { (func) (*_ptr); }

                                                                              ^

WARNING:LINE_SPACING: Missing a blank line after declarations #184: FILE: unit/test-util.c:46:
+ _cleanup_free_ uint8_t *data = NULL;
+ data = malloc0(128 * 2);

/github/workspace/src/patch/14565706.patch total: 2 errors, 3 warnings, 140 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/patch/14565706.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

[BlueZ,v3,4/6] main: Use _cleanup_() to simplify configuration parsing ERROR:SPACING: need consistent spacing around '*' (ctx:WxV) #84: FILE: src/main.c:258:
+ _cleanup_type_(GError) GError *err = NULL;
^

ERROR:SPACING: need consistent spacing around '*' (ctx:WxV) #108: FILE: src/main.c:440:
+ _cleanup_type_(GError) GError *err = NULL;
^

ERROR:SPACING: need consistent spacing around '*' (ctx:WxV) #204: FILE: src/main.c:888:
+ _cleanup_type_(GError) GError *err = NULL;
^

ERROR:SPACING: need consistent spacing around '*' (ctx:WxV) #409: FILE: src/main.c:1207:
+ _cleanup_type_(GError) GError *err = NULL;
^

ERROR:SPACING: need consistent spacing around '*' (ctx:WxV) #501: FILE: src/main.c:1570:
+ _cleanup_type_(GError) GError *err = NULL;
^

/github/workspace/src/patch/14565665.patch total: 5 errors, 0 warnings, 413 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/patch/14565665.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
src/main.c: note: in included file (through src/device.h):src/shared/gatt-server.c:271:25: warning: Variable length array is used.src/shared/gatt-server.c:614:25: warning: Variable length array is used.src/shared/gatt-server.c:712:25: warning: Variable length array is used.src/shared/gatt-server.c:271:25: warning: Variable length array is used.src/shared/gatt-server.c:614:25: warning: Variable length array is used.src/shared/gatt-server.c:712:25: warning: Variable length array is used.src/shared/gatt-server.c:271:25: warning: Variable length array is used.src/shared/gatt-server.c:614:25: warning: Variable length array is used.src/shared/gatt-server.c:712:25: warning: Variable length array is used.src/main.c: note: in included file (through src/device.h):

https://github.com/bluez/bluez/pull/2118

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH v3 2/6 shared/util: Add helper for "cleanup" variable attribute

In reply to Bastien Nocera

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(+)
src/shared/util.h +40 -0
index 67629dddfaa9..c8ce90c5f1b5 100644
@@ -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);
unit/test-util.c +65 -0

85 lines not on this page · raw message

Signature · 1 line
2.54.0
#4 lore raw

PATCH v3 3/6 doc: Recommend using _cleanup_ and friends

In reply to Bastien Nocera
---
 doc/maintainer-guidelines.rst | 3 +++
 1 file changed, 3 insertions(+)
doc/maintainer-guidelines.rst +3 -0
index 44d3e258db6e..b67c6596f4c1 100644
@@ -98,6 +98,9 @@ do this:
 
 The above assumes that a kernel tree resides in ``~/src/linux/``.
 
+Also make sure to use ``_cleanup_free_`` and ``_cleanup_(free_func)`` when
+possible. It makes your code much nicer to read (and shorter), and avoids
+common memory leaks on error paths.
 
 Rule 4: Pay extra attention to adding new files to the tree
 -----------------------------------------------------------
Signature · 1 line
2.54.0
#5 lore raw

PATCH v3 4/6 main: Use _cleanup_() to simplify configuration parsing

In reply to Bastien Nocera

Use helpers to simplify temporary string usage, and cleanup in error paths.

---
 src/main.c | 101 ++++++++++++++++-------------------------------------
 1 file changed, 31 insertions(+), 70 deletions(-)
src/main.c +31 -70
index 8aa19a3e3346..9053e74aaf75 100644
@@ -205,6 +205,9 @@ static const struct group_table {
 	{ }
 };
 
+CLEANUP_FREEFUNC(GError, g_error_free);
+CLEANUP_FREEFUNC(GKeyFile, g_key_file_free);
+
 static int8_t check_sirk_alpha_numeric(char *str)
 {
 	int8_t val = 0;
@@ -252,8 +255,8 @@ GKeyFile *btd_get_main_conf(void)
 
 static GKeyFile *load_config(const char *name)
 {
-	GError *err = NULL;
-	GKeyFile *keyfile;
+	_cleanup_type_(GError) GError *err = NULL;
+	_cleanup_type_(GKeyFile) GKeyFile *keyfile = NULL;
 	int len;
 
 	if (name)
@@ -285,12 +288,10 @@ static GKeyFile *load_config(const char *name)
 		if (!g_error_matches(err, G_FILE_ERROR, G_FILE_ERROR_NOENT))
 			error("Parsing %s failed: %s", main_conf_file_path,
 				err->message);
-		g_error_free(err);
-		g_key_file_free(keyfile);
 		return NULL;
 	}
 
-	return keyfile;
+	return _steal_(keyfile);
 }
 
 static void parse_did(const char *did)
@@ -436,14 +437,13 @@ static int get_mode(const char *str)
 static bool parse_config_string(GKeyFile *config, const char *group,
 					const char *key, char **val)
 {
-	GError *err = NULL;
+	_cleanup_type_(GError) GError *err = NULL;
 	char *tmp;
 
 	tmp = g_key_file_get_string(config, group, key, &err);
 	if (err) {
 		if (err->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
 			DBG("%s", err->message);
-		g_error_free(err);
 		return false;
 	}
 
@@ -462,7 +462,7 @@ static bool parse_config_int(GKeyFile *config, const char *group,
 					size_t min, size_t max)
 {
 	size_t tmp;
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 	char *endptr = NULL;
 
 	if (!parse_config_string(config, group, key, &str))
@@ -471,25 +471,21 @@ static bool parse_config_int(GKeyFile *config, const char *group,
 	tmp = strtol(str, &endptr, 0);
 	if (!endptr || *endptr != '\0') {
 		error("%s.%s = %s is not integer", group, key, str);
-		g_free(str);
 		return false;
 	}
 
 	if (tmp < min) {
-		g_free(str);
 		warn("%s.%s = %zu is out of range (< %zu)", group, key, tmp,
 									min);
 		return false;
 	}
 
 	if (tmp > max) {
-		g_free(str);
 		warn("%s.%s = %zu is out of range (> %zu)", group, key, tmp,
 									max);
 		return false;
 	}
 
-	g_free(str);
 	if (val)
 		*val = tmp;
 
@@ -500,10 +496,9 @@ static bool parse_config_signed_int(GKeyFile *config, const char *group,
 					const char *key, int8_t *val,
 					long min, long max)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 	char *endptr = NULL;
 	long tmp;
-	bool result = false;
 
 	str = g_key_file_get_string(config, group, key, NULL);
 	if (!str)
@@ -512,28 +507,24 @@ static bool parse_config_signed_int(GKeyFile *config, const char *group,
 	tmp = strtol(str, &endptr, 0);
 	if (!endptr || *endptr != '\0') {
 		warn("%s.%s = %s is not integer", group, key, str);
-		goto cleanup;
+		return false;
 	}
 
 	if (tmp < min) {
 		warn("%s.%s = %ld is out of range (< %ld)", group, key, tmp,
 			min);
-		goto cleanup;
+		return false;
 	}
 
 	if (tmp > max) {
 		warn("%s.%s = %ld is out of range (> %ld)", group, key, tmp,
 									max);
-		goto cleanup;
+		return false;
 	}
 
 	if (val)
 		*val = (int8_t) tmp;
-	result = true;
-
-cleanup:
-	g_free(str);
-	return result;
+	return true;
 }
 
 struct config_param {
@@ -894,14 +885,13 @@ static bool parse_config_u8(GKeyFile *config, const char *group,
 static bool parse_config_bool(GKeyFile *config, const char *group,
 					const char *key, bool *val)
 {
-	GError *err = NULL;
+	_cleanup_type_(GError) GError *err = NULL;
 	gboolean tmp;
 
 	tmp = g_key_file_get_boolean(config, group, key, &err);
 	if (err) {
 		if (err->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
 			DBG("%s", err->message);
-		g_error_free(err);
 		return false;
 	}
 
@@ -915,7 +905,7 @@ static bool parse_config_bool(GKeyFile *config, const char *group,
 
 static void parse_privacy(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!parse_config_string(config, "General", "Privacy", &str)) {
 		btd_opts.privacy = 0x00;
@@ -948,13 +938,11 @@ static void parse_privacy(GKeyFile *config)
 		DBG("Invalid privacy option: %s", str);
 		btd_opts.privacy = 0x00;
 	}
-
-	g_free(str);
 }
 
 static void parse_repairing(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!parse_config_string(config, "General", "JustWorksRepairing",
 						&str)) {
@@ -963,13 +951,12 @@ static void parse_repairing(GKeyFile *config)
 	}
 
 	btd_opts.jw_repairing = parse_jw_repairing(str);
-	g_free(str);
 }
 
 static bool parse_config_hex(GKeyFile *config, char *group,
 					const char *key, uint32_t *val)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!parse_config_string(config, group, key, &str))
 		return false;
@@ -977,37 +964,34 @@ static bool parse_config_hex(GKeyFile *config, char *group,
 	if (val)
 		*val = strtol(str, NULL, 16);
 
-	g_free(str);
 	return true;
 }
 
 static void parse_device_id(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	parse_config_string(config, "General", "DeviceID", &str);
 	if (!str)
 		return;
 
 	parse_did(str);
-	g_free(str);
 }
 
 static void parse_ctrl_mode(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	parse_config_string(config, "General", "ControllerMode", &str);
 	if (!str)
 		return;
 
 	btd_opts.mode = get_mode(str);
-	g_free(str);
 }
 
 static void parse_multi_profile(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	parse_config_string(config, "General", "MultiProfile", &str);
 	if (!str)
@@ -1019,8 +1003,6 @@ static void parse_multi_profile(GKeyFile *config)
 		btd_opts.mps = MPS_MULTIPLE;
 	else
 		btd_opts.mps = MPS_OFF;
-
-	g_free(str);
 }
 
 static gboolean parse_kernel_experimental(const char *key, const char *value,
@@ -1043,20 +1025,18 @@ static gboolean parse_kernel_experimental(const char *key, const char *value,
 
 static void parse_kernel_exp(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!parse_config_string(config, "General", "KernelExperimental",
 						&str))
 		return;
 
 	parse_kernel_experimental(NULL, str, NULL, NULL);
-
-	g_free(str);
 }
 
 static void parse_secure_conns(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!parse_config_string(config, "General", "SecureConnections",
 								&str))
@@ -1068,8 +1048,6 @@ static void parse_secure_conns(GKeyFile *config)
 		btd_opts.secure_conn = SC_ON;
 	else if (!strcmp(str, "only"))
 		btd_opts.secure_conn = SC_ONLY;
-
-	g_free(str);
 }
 
 static void parse_general(GKeyFile *config)
@@ -1120,14 +1098,13 @@ static void parse_general(GKeyFile *config)
 
 static void parse_gatt_cache(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	parse_config_string(config, "GATT", "Cache", &str);
 	if (!str)
 		return;
 
 	btd_opts.gatt_cache = parse_gatt_cache_str(str);
-	g_free(str);
 }
 
 static enum bt_gatt_export_t parse_gatt_export_str(const char *str)
@@ -1147,14 +1124,13 @@ static enum bt_gatt_export_t parse_gatt_export_str(const char *str)
 
 static void parse_gatt_export(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	parse_config_string(config, "GATT", "ExportClaimedServices", &str);
 	if (!str)
 		return;
 
 	btd_opts.gatt_export = parse_gatt_export_str(str);
-	g_free(str);
 }
 
 static uint8_t parse_gatt_seclevel_str(const char *str)
@@ -1176,7 +1152,7 @@ static uint8_t parse_gatt_seclevel_str(const char *str)
 
 static void parse_gatt_seclevel(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!btd_opts.testing)
 		return;
@@ -1186,7 +1162,6 @@ static void parse_gatt_seclevel(GKeyFile *config)
 		return;
 
 	btd_opts.gatt_seclevel = parse_gatt_seclevel_str(str);
-	g_free(str);
 }
 
 static void parse_gatt(GKeyFile *config)
@@ -1204,7 +1179,7 @@ static void parse_gatt(GKeyFile *config)
 
 static void parse_csis_sirk(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!parse_config_string(config, "CSIS", "SIRK", &str))
 		return;
@@ -1213,8 +1188,6 @@ static void parse_csis_sirk(GKeyFile *config)
 		hex2bin(str, btd_opts.csis.sirk, sizeof(btd_opts.csis.sirk));
 	else if (!gen_sirk(str))
 		DBG("Unable to generate SIRK from string");
-
-	g_free(str);
 }
 
 static void parse_csis(GKeyFile *config)
@@ -1231,8 +1204,8 @@ static void parse_csis(GKeyFile *config)
 static bool parse_cs_role(GKeyFile *config, const char *group,
 					const char *key, uint8_t *val)
 {
-	GError *err = NULL;
-	char *str = NULL;
+	_cleanup_type_(GError) GError *err = NULL;
+	_cleanup_(g_free) char *str = NULL;
 	char *endptr = NULL;
 	int numeric_val;
 
@@ -1241,7 +1214,6 @@ static bool parse_cs_role(GKeyFile *config, const char *group,
 	if (err) {
 		if (err->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND)
 			DBG("%s", err->message);
-		g_error_free(err);
 		return false;
 	}
 
@@ -1251,17 +1223,14 @@ static bool parse_cs_role(GKeyFile *config, const char *group,
 	if (!strcmp(str, "Initiator") || !strcmp(str, "initiator")) {
 		if (val)
 			*val = 1;
-		g_free(str);
 		return true;
 	} else if (!strcmp(str, "Reflector") || !strcmp(str, "reflector")) {
 		if (val)
 			*val = 2;
-		g_free(str);
 		return true;
 	} else if (!strcmp(str, "Both") || !strcmp(str, "both")) {
 		if (val)
 			*val = 3;
-		g_free(str);
 		return true;
 	}
 
@@ -1272,7 +1241,6 @@ static bool parse_cs_role(GKeyFile *config, const char *group,
 		warn("%s.%s = %s is not a valid value. "
 			"Expected: 1/Initiator, 2/Reflector, or 3/Both",
 			group, key, str);
-		g_free(str);
 		return false;
 	}
 
@@ -1280,14 +1248,12 @@ static bool parse_cs_role(GKeyFile *config, const char *group,
 		warn("%s.%s = %d is out of range. "
 			"Valid values: 1 (Initiator), 2 (Reflector), 3 (Both)",
 			group, key, numeric_val);
-		g_free(str);
 		return false;
 	}
 
 	if (val)
 		*val = numeric_val;
 
-	g_free(str);
 	return true;
 }
 
@@ -1305,7 +1271,7 @@ static void parse_le_cs_config(GKeyFile *config)
 
 static void parse_avdtp_session_mode(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!parse_config_string(config, "AVDTP", "SessionMode", &str))
 		return;
@@ -1318,13 +1284,11 @@ static void parse_avdtp_session_mode(GKeyFile *config)
 		DBG("Invalid mode option: %s", str);
 		btd_opts.avdtp.session_mode = BT_IO_MODE_BASIC;
 	}
-
-	g_free(str);
 }
 
 static void parse_avdtp_stream_mode(GKeyFile *config)
 {
-	char *str = NULL;
+	_cleanup_(g_free) char *str = NULL;
 
 	if (!parse_config_string(config, "AVDTP", "StreamMode", &str))
 		return;
@@ -1337,8 +1301,6 @@ static void parse_avdtp_stream_mode(GKeyFile *config)
 		DBG("Invalid mode option: %s", str);
 		btd_opts.avdtp.stream_mode = BT_IO_MODE_BASIC;
 	}
-
-	g_free(str);
 }
 
 static void parse_avdtp(GKeyFile *config)
@@ -1605,7 +1567,7 @@ static GOptionEntry options[] = {
 int main(int argc, char *argv[])
 {
 	GOptionContext *context;
-	GError *err = NULL;
+	_cleanup_type_(GError) GError *err = NULL;
 	uint16_t sdp_mtu = 0;
 	uint32_t sdp_flags = 0;
 	int gdbus_flags = 0;
@@ -1618,7 +1580,6 @@ int main(int argc, char *argv[])
 	if (g_option_context_parse(context, &argc, &argv, &err) == FALSE) {
 		if (err != NULL) {
 			g_printerr("%s\n", err->message);
-			g_error_free(err);
 		} else
 			g_printerr("An unknown error occurred\n");
 		exit(1);
Signature · 1 line
2.54.0
#6 lore raw

RE: [BlueZ,v3,4/6] main: Use _cleanup_() to simplify configuration parsing

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch failed: src/main.c:205
error: src/main.c: patch does not apply
hint: Use 'git am --show-current-patch' to see the failed patch

Please resolve the issue and submit the patches again.

---
Regards,
Linux Bluetooth

#7 lore raw

PATCH v3 5/6 client: Use _cleanup_fd_ to simplify urandom access

In reply to Bastien Nocera

fd gets auto-closed before exiting the scope.

---
 client/mgmt.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
client/mgmt.c +1 -4
index 50558a313866..e199c30540e4 100644
@@ -2681,7 +2681,7 @@ static void cmd_privacy(int argc, char **argv)
 			return bt_shell_noninteractive_quit(EXIT_FAILURE);
 		}
 	} else {
-		int fd;
+		_cleanup_fd_ int fd = -1;
 
 		fd = open("/dev/urandom", O_RDONLY);
 		if (fd < 0) {
@@ -2691,11 +2691,8 @@ static void cmd_privacy(int argc, char **argv)
 
 		if (read(fd, cp.irk, sizeof(cp.irk)) != sizeof(cp.irk)) {
 			error("Reading from urandom failed");
-			close(fd);
 			return bt_shell_noninteractive_quit(EXIT_FAILURE);
 		}
-
-		close(fd);
 	}
 
 	if (send_cmd(mgmt, MGMT_OP_SET_PRIVACY, index, sizeof(cp), &cp,
Signature · 1 line
2.54.0
#8 lore raw

PATCH v3 6/6 btattach: Use _cleanup_fd_ to simplify error paths

In reply to Bastien Nocera

Use _cleanup_fd_ and _steal_fd() to simplify error paths, and only "steal" the file descriptor on success.

---
 tools/btattach.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
tools/btattach.c +6 -12
index 5f7d19093698..8868871b0128 100644
@@ -40,7 +40,8 @@
 static int open_serial(const char *path, unsigned int speed, bool flowctl)
 {
 	struct termios ti;
-	int fd, saved_ldisc, ldisc = N_HCI;
+	_cleanup_fd_ int fd = -1;
+	int saved_ldisc, ldisc = N_HCI;
 
 	fd = open(path, O_RDWR | O_NOCTTY);
 	if (fd < 0) {
@@ -50,13 +51,11 @@ static int open_serial(const char *path, unsigned int speed, bool flowctl)
 
 	if (tcflush(fd, TCIOFLUSH) < 0) {
 		perror("Failed to flush serial port");
-		close(fd);
 		return -1;
 	}
 
 	if (ioctl(fd, TIOCGETD, &saved_ldisc) < 0) {
 		perror("Failed get serial line discipline");
-		close(fd);
 		return -1;
 	}
 
@@ -73,19 +72,17 @@ static int open_serial(const char *path, unsigned int speed, bool flowctl)
 
 	if (tcsetattr(fd, TCSANOW, &ti) < 0) {
 		perror("Failed to set serial port settings");
-		close(fd);
 		return -1;
 	}
 
 	if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
 		perror("Failed set serial line discipline");
-		close(fd);
 		return -1;
 	}
 
 	printf("Switched line discipline from %d to %d\n", saved_ldisc, ldisc);
 
-	return fd;
+	return _steal_fd_(&fd);
 }
 
 static void local_version_callback(const void *data, uint8_t size,
@@ -99,7 +96,8 @@ static void local_version_callback(const void *data, uint8_t size,
 static int attach_proto(const char *path, unsigned int proto,
 			unsigned int speed, bool flowctl, unsigned int flags)
 {
-	int fd, dev_id;
+	_cleanup_fd_ int fd = -1;
+	int dev_id;
 
 	fd = open_serial(path, speed, flowctl);
 	if (fd < 0)
@@ -107,20 +105,17 @@ static int attach_proto(const char *path, unsigned int proto,
 
 	if (ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
 		perror("Failed to set flags");
-		close(fd);
 		return -1;
 	}
 
 	if (ioctl(fd, HCIUARTSETPROTO, proto) < 0) {
 		perror("Failed to set protocol");
-		close(fd);
 		return -1;
 	}
 
 	dev_id = ioctl(fd, HCIUARTGETDEVICE);
 	if (dev_id < 0) {
 		perror("Failed to get device id");
-		close(fd);
 		return -1;
 	}
 
@@ -140,7 +135,6 @@ static int attach_proto(const char *path, unsigned int proto,
 
 		if (!hci) {
 			fprintf(stderr, "Failed to open HCI user channel\n");
-			close(fd);
 			return -1;
 		}
 
@@ -149,7 +143,7 @@ static int attach_proto(const char *path, unsigned int proto,
 					(bt_hci_destroy_func_t) bt_hci_unref);
 	}
 
-	return fd;
+	return _steal_fd_(&fd);
 }
 
 static void uart_callback(int fd, uint32_t events, void *user_data)
Signature · 1 line
2.54.0
#9 lore raw
In reply to Bastien Nocera
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 10 lines · On Mon, 11 May 2026 15:18:03 +0200 you wrote:

As discussed in:
https://lore.kernel.org/linux-bluetooth/[email protected]/

Implement a cleanup helper.

The MIN/MAX fix is here because it touches the same hunk in src/main.c as the other patches. Feel free to pick it up straight away while the rest is discussed.

[...]

Here is the summary with links:
- [BlueZ,v3,1/6] all: Remove more unneeded MIN/MAX macro definition

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=ca2b39b0c08e
  - [BlueZ,v3,2/6] shared/util: Add helper for "cleanup" variable attribute
    (no matching commit)
  - [BlueZ,v3,3/6] doc: Recommend using _cleanup_ and friends
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=9ba6c13df5fb
  - [BlueZ,v3,4/6] main: Use _cleanup_() to simplify configuration parsing
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=140e3569295c
  - [BlueZ,v3,5/6] client: Use _cleanup_fd_ to simplify urandom access
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=e33f5027b898
  - [BlueZ,v3,6/6] btattach: Use _cleanup_fd_ to simplify error paths
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=02aa9a8cfe6a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
#10 lore raw

Hi Bastien,

Quoted 32 lines · On Tue, May 12, 2026 at 3:21 PM <[email protected]> wrote:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 10 lines · On Mon, 11 May 2026 15:18:03 +0200 you wrote:

As discussed in:
https://lore.kernel.org/linux-bluetooth/[email protected]/

Implement a cleanup helper.

The MIN/MAX fix is here because it touches the same hunk in src/main.c as the other patches. Feel free to pick it up straight away while the rest is discussed.

[...]

Here is the summary with links:
- [BlueZ,v3,1/6] all: Remove more unneeded MIN/MAX macro definition

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=ca2b39b0c08e
  - [BlueZ,v3,2/6] shared/util: Add helper for "cleanup" variable attribute
    (no matching commit)
  - [BlueZ,v3,3/6] doc: Recommend using _cleanup_ and friends
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=9ba6c13df5fb
  - [BlueZ,v3,4/6] main: Use _cleanup_() to simplify configuration parsing
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=140e3569295c
  - [BlueZ,v3,5/6] client: Use _cleanup_fd_ to simplify urandom access
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=e33f5027b898
  - [BlueZ,v3,6/6] btattach: Use _cleanup_fd_ to simplify error paths
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=02aa9a8cfe6a
Quoted 4 lines

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html

We will probably need to revert the cleanup changes, they are causing crashes and other problems when run under Valgrind.

bluetoothd[941151]: src/main.c:parse_config() parsing src/main.conf ==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4003098: parse_config_hex (main.c:961)
==941151==    by 0x4003098: parse_general (main.c:1058)
==941151==    by 0x4003098: parse_config (main.c:1341)
==941151==    by 0x4003098: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F17EE: parse_config_int (main.c:467)
==941151==    by 0x40F1ECD: parse_config_u32.constprop.0.isra.0 (main.c:848)
==941151==    by 0x40030BD: parse_general (bluetooth.c:4925)
==941151==    by 0x40030BD: parse_config (main.c:1341)
==941151==    by 0x40030BD: main (main.c:1616)

==941151== Address 0x1fff000050 is on thread 1's stack ==941151== in frame #2, created by parse_config_int (main.c:465) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004C89: parse_device_id (main.c:974)
==941151==    by 0x4004C89: parse_general (main.c:1067)
==941151==    by 0x4004C89: parse_config (main.c:1341)
==941151==    by 0x4004C89: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack
==941151== in frame #2, created by main (main.c:1570)
==941151==
bluetoothd[941151]: src/main.c:parse_config_string() General.ControllerMode = le bluetoothd[941151]: GLib: g_error_free: assertion 'error != NULL' failed bluetoothd[941151]: ++++++++ backtrace ++++++++
bluetoothd[941151]: #1 log_handler+0x4e (src/main.c:1418) [0x40f0ffe] bluetoothd[941151]: #2 g_logv+0x247 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5477] bluetoothd[941151]: #3 g_log+0x93 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5803]

bluetoothd[941151]: #4  parse_config_string+0x60 (src/main.c:208) [0x40f1400]
bluetoothd[941151]: #5  main+0x4a2 (src/main.c:988) [0x4003262]
bluetoothd[941151]: #6  __libc_start_call_main+0x75

(/usr/lib64/libc.so.6) [0x4f90575] bluetoothd[941151]: #7 __libc_start_main@@GLIBC_2.34+0x88 (/usr/lib64/libc.so.6) [0x4f90628] bluetoothd[941151]: #8 _start+0x25 (/home/vudentz/git/bluez/src/bluetoothd) [0x40054c5] bluetoothd[941151]: +++++++++++++++++++++++++++ ==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4003311: parse_ctrl_mode (bluetooth.c:5033)
==941151==    by 0x4003311: parse_general (main.c:1074)
==941151==    by 0x4003311: parse_config (main.c:1341)
==941151==    by 0x4003311: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (bluetooth.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F17EE: parse_config_int (main.c:467)
==941151==    by 0x40F20C2: parse_config_u16.isra.0 (main.c:863)
==941151==    by 0x4003349: parse_general (bluetooth.c:2985)
==941151==    by 0x4003349: parse_config (main.c:1341)
==941151==    by 0x4003349: main (main.c:1616)

==941151== Address 0x1fff000050 is on thread 1's stack ==941151== in frame #2, created by parse_config_int (main.c:465) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004F55: parse_multi_profile (main.c:996)
==941151==    by 0x4004F55: parse_general (main.c:1078)
==941151==    by 0x4004F55: parse_config (main.c:1341)
==941151==    by 0x4004F55: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F15D4: parse_privacy (main.c:910)
==941151==    by 0x40033F9: parse_general (main.c:1081)
==941151==    by 0x40033F9: parse_config (main.c:1341)
==941151==    by 0x40033F9: main (main.c:1616)

==941151== Address 0x1fff000098 is on thread 1's stack ==941151== in frame #2, created by parse_privacy (main.c:909) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40047B5: parse_repairing (main.c:947)
==941151==    by 0x40047B5: parse_general (main.c:1082)
==941151==    by 0x40047B5: parse_config (main.c:1341)
==941151==    by 0x40047B5: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40049FF: parse_secure_conns (bluetooth.c:1597)
==941151==    by 0x40049FF: parse_general (main.c:1088)
==941151==    by 0x40049FF: parse_config (main.c:1341)
==941151==    by 0x40049FF: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (bluetooth.c:1570) ==941151==
bluetoothd[941151]: src/main.c:parse_config_bool() General.Experimental = true bluetoothd[941151]: GLib: g_error_free: assertion 'error != NULL' failed bluetoothd[941151]: ++++++++ backtrace ++++++++ bluetoothd[941151]: #1 log_handler+0x4e (src/main.c:1418) [0x40f0ffe] bluetoothd[941151]: #2 g_logv+0x247 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5477] bluetoothd[941151]: #3 g_log+0x93 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5803] bluetoothd[941151]: #4 parse_config_bool.isra.0+0x64 (src/main.c:208) [0x40f1f64]
bluetoothd[941151]: #5 main+0x7c0 (src/main.c:1089) [0x4003580] bluetoothd[941151]: #6 __libc_start_call_main+0x75 (/usr/lib64/libc.so.6) [0x4f90575]
bluetoothd[941151]: #7 __libc_start_main@@GLIBC_2.34+0x88 (/usr/lib64/libc.so.6) [0x4f90628]
bluetoothd[941151]: #8 _start+0x25 (/home/vudentz/git/bluez/src/bluetoothd) [0x40054c5] bluetoothd[941151]: +++++++++++++++++++++++++++ bluetoothd[941151]: src/main.c:parse_config_string() General.KernelExperimental = 6fbaf188-05e0-496a-9885-d6ddfdb4e03e bluetoothd[941151]: GLib: g_error_free: assertion 'error != NULL' failed bluetoothd[941151]: ++++++++ backtrace ++++++++ bluetoothd[941151]: #1 log_handler+0x4e (src/main.c:1418) [0x40f0ffe] bluetoothd[941151]: #2 g_logv+0x247 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5477] bluetoothd[941151]: #3 g_log+0x93 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5803]

bluetoothd[941151]: #4  parse_config_string+0x60 (src/main.c:208) [0x40f1400]
bluetoothd[941151]: #5  main+0x816 (src/main.c:1032) [0x40035d6]
bluetoothd[941151]: #6  __libc_start_call_main+0x75

(/usr/lib64/libc.so.6) [0x4f90575] bluetoothd[941151]: #7 __libc_start_main@@GLIBC_2.34+0x88 (/usr/lib64/libc.so.6) [0x4f90628] bluetoothd[941151]: #8 _start+0x25 (/home/vudentz/git/bluez/src/bluetoothd) [0x40054c5] bluetoothd[941151]: +++++++++++++++++++++++++++ bluetoothd[941151]: src/main.c:btd_parse_kernel_experimental() 6fbaf188-05e0-496a-9885-d6ddfdb4e03e
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4003607: parse_kernel_exp (main.c:1030)
==941151==    by 0x4003607: parse_general (main.c:1093)
==941151==    by 0x4003607: parse_config (main.c:1341)
==941151==    by 0x4003607: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F17EE: parse_config_int (main.c:467)
==941151==    by 0x40F19B8: parse_mode_config (main.c:552)
==941151==    by 0x40036D5: parse_le_config (bluetooth.c:2879)
==941151==    by 0x40036D5: parse_config (main.c:1343)
==941151==    by 0x40036D5: main (main.c:1616)

==941151== Address 0x1fff000020 is on thread 1's stack ==941151== in frame #2, created by parse_config_int (main.c:465) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004C4B: parse_gatt_cache (main.c:1103)
==941151==    by 0x4004C4B: parse_gatt (main.c:1171)
==941151==    by 0x4004C4B: parse_config (main.c:1344)
==941151==    by 0x4004C4B: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F17EE: parse_config_int (main.c:467)
==941151==    by 0x40F2072: parse_config_u8.isra.0 (main.c:878)
==941151==    by 0x40037E5: parse_gatt (bluetooth.c:2847)
==941151==    by 0x40037E5: parse_config (main.c:1344)
==941151==    by 0x40037E5: main (main.c:1616)

==941151== Address 0x1fff000050 is on thread 1's stack ==941151== in frame #2, created by parse_config_int (main.c:465) ==941151==
bluetoothd[941151]: src/main.c:parse_config_string() GATT.ExportClaimedServices = read-write
bluetoothd[941151]: GLib: g_error_free: assertion 'error != NULL' failed bluetoothd[941151]: ++++++++ backtrace ++++++++ bluetoothd[941151]: #1 log_handler+0x4e (src/main.c:1418) [0x40f0ffe] bluetoothd[941151]: #2 g_logv+0x247 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5477] bluetoothd[941151]: #3 g_log+0x93 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5803]

bluetoothd[941151]: #4  parse_config_string+0x60 (src/main.c:208) [0x40f1400]
bluetoothd[941151]: #5  main+0xae3 (src/main.c:1132) [0x40038a3]
bluetoothd[941151]: #6  __libc_start_call_main+0x75

(/usr/lib64/libc.so.6) [0x4f90575] bluetoothd[941151]: #7 __libc_start_main@@GLIBC_2.34+0x88 (/usr/lib64/libc.so.6) [0x4f90628] bluetoothd[941151]: #8 _start+0x25 (/home/vudentz/git/bluez/src/bluetoothd) [0x40054c5] bluetoothd[941151]: +++++++++++++++++++++++++++ ==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x400395E: parse_gatt_export (main.c:1129)
==941151==    by 0x400395E: parse_gatt (main.c:1178)
==941151==    by 0x400395E: parse_config (main.c:1344)
==941151==    by 0x400395E: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40040AD: parse_gatt_seclevel (main.c:1157)
==941151==    by 0x40040AD: parse_gatt (main.c:1179)
==941151==    by 0x40040AD: parse_config (main.c:1344)
==941151==    by 0x40040AD: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004A1E: parse_csis_sirk (bluetooth.c:1593)
==941151==    by 0x4004A1E: parse_csis (main.c:1197)
==941151==    by 0x4004A1E: parse_config (main.c:1345)
==941151==    by 0x4004A1E: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (bluetooth.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004A3D: parse_avdtp_session_mode (main.c:1276)
==941151==    by 0x4004A3D: parse_avdtp (main.c:1310)
==941151==    by 0x4004A3D: parse_config (main.c:1346)
==941151==    by 0x4004A3D: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004A5C: parse_avdtp_stream_mode (main.c:1293)
==941151==    by 0x4004A5C: parse_avdtp (main.c:1311)
==941151==    by 0x4004A5C: parse_config (main.c:1346)
==941151==    by 0x4004A5C: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004417: parse_cs_role (bluetooth.c:943)
==941151==    by 0x4004417: parse_le_cs_config (main.c:1264)
==941151==    by 0x4004417: parse_config (main.c:1348)
==941151==    by 0x4004417: main (main.c:1616)

==941151== Address 0x1fff0000f8 is on thread 1's stack ==941151== in frame #2, created by main (bluetooth.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40044FB: parse_config_signed_int (main.c:501)
==941151==    by 0x40044FB: parse_le_cs_config (main.c:1269)
==941151==    by 0x40044FB: parse_config (main.c:1348)
==941151==    by 0x40044FB: main (main.c:1616)

==941151== Address 0x1fff0000f8 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==

Signature · 1 line
Luiz Augusto von Dentz
#11 lore raw
Quoted 48 lines · Luiz Augusto von Dentz

Hi Bastien,

Quoted 44 lines · On Tue, May 12, 2026 at 3:21 PM <[email protected]> wrote:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 5 lines · On Mon, 11 May 2026 15:18:03 +0200 you wrote:

As discussed in:
https://lore.kernel.org/linux-bluetooth/[email protected]/

Implement a cleanup helper.

Quoted 7 lines

The MIN/MAX fix is here because it touches the same hunk in src/main.c
as the other patches. Feel free to pick it up straight away while the
rest is discussed.

[...]

Here is the summary with links:
  - [BlueZ,v3,1/6] all: Remove more unneeded MIN/MAX macro definition

https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=ca2b39b0c08e
  - [BlueZ,v3,2/6] shared/util: Add helper for "cleanup" variable attribute
    (no matching commit)
  - [BlueZ,v3,3/6] doc: Recommend using _cleanup_ and friends
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=9ba6c13df5fb
  - [BlueZ,v3,4/6] main: Use _cleanup_() to simplify configuration parsing

https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=140e3569295c
  - [BlueZ,v3,5/6] client: Use _cleanup_fd_ to simplify urandom access

https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=e33f5027b898
  - [BlueZ,v3,6/6] btattach: Use _cleanup_fd_ to simplify error paths

https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=02aa9a8cfe6a

Quoted 7 lines
Quoted 4 lines

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html

We will probably need to revert the cleanup changes, they are causing crashes and other problems when run under Valgrind.

Please do, I've managed to reproduce some of the problems locally, which I didn't see in my testing, even running under valgrind.

I'll send a v4 later.

Cheers

#12 lore raw
In reply to Luiz Augusto von Dentz

ti, 2026-05-12 kello 15:38 -0400, Luiz Augusto von Dentz kirjoitti:

Quoted 318 lines

Hi Bastien,

Quoted 32 lines · On Tue, May 12, 2026 at 3:21 PM <[email protected]> wrote:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 10 lines · On Mon, 11 May 2026 15:18:03 +0200 you wrote:

As discussed in:
https://lore.kernel.org/linux-bluetooth/[email protected]/

Implement a cleanup helper.

The MIN/MAX fix is here because it touches the same hunk in src/main.c as the other patches. Feel free to pick it up straight away while the rest is discussed.

[...]

Here is the summary with links:
- [BlueZ,v3,1/6] all: Remove more unneeded MIN/MAX macro definition

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=ca2b39b0c08e
  - [BlueZ,v3,2/6] shared/util: Add helper for "cleanup" variable attribute
    (no matching commit)
  - [BlueZ,v3,3/6] doc: Recommend using _cleanup_ and friends
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=9ba6c13df5fb
  - [BlueZ,v3,4/6] main: Use _cleanup_() to simplify configuration parsing
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=140e3569295c
  - [BlueZ,v3,5/6] client: Use _cleanup_fd_ to simplify urandom access
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=e33f5027b898
  - [BlueZ,v3,6/6] btattach: Use _cleanup_fd_ to simplify error paths
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=02aa9a8cfe6a
Quoted 4 lines

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html

We will probably need to revert the cleanup changes, they are causing crashes and other problems when run under Valgrind.

bluetoothd[941151]: src/main.c:parse_config() parsing src/main.conf ==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4003098: parse_config_hex (main.c:961)
==941151==    by 0x4003098: parse_general (main.c:1058)
==941151==    by 0x4003098: parse_config (main.c:1341)
==941151==    by 0x4003098: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F17EE: parse_config_int (main.c:467)
==941151==    by 0x40F1ECD: parse_config_u32.constprop.0.isra.0 (main.c:848)
==941151==    by 0x40030BD: parse_general (bluetooth.c:4925)
==941151==    by 0x40030BD: parse_config (main.c:1341)
==941151==    by 0x40030BD: main (main.c:1616)

==941151== Address 0x1fff000050 is on thread 1's stack ==941151== in frame #2, created by parse_config_int (main.c:465) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004C89: parse_device_id (main.c:974)
==941151==    by 0x4004C89: parse_general (main.c:1067)
==941151==    by 0x4004C89: parse_config (main.c:1341)
==941151==    by 0x4004C89: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack
==941151== in frame #2, created by main (main.c:1570)
==941151==
bluetoothd[941151]: src/main.c:parse_config_string() General.ControllerMode = le bluetoothd[941151]: GLib: g_error_free: assertion 'error != NULL' failed bluetoothd[941151]: ++++++++ backtrace ++++++++
bluetoothd[941151]: #1 log_handler+0x4e (src/main.c:1418) [0x40f0ffe] bluetoothd[941151]: #2 g_logv+0x247 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5477] bluetoothd[941151]: #3 g_log+0x93 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5803]

bluetoothd[941151]: #4  parse_config_string+0x60 (src/main.c:208) [0x40f1400]
bluetoothd[941151]: #5  main+0x4a2 (src/main.c:988) [0x4003262]
bluetoothd[941151]: #6  __libc_start_call_main+0x75

(/usr/lib64/libc.so.6) [0x4f90575] bluetoothd[941151]: #7 __libc_start_main@@GLIBC_2.34+0x88 (/usr/lib64/libc.so.6) [0x4f90628] bluetoothd[941151]: #8 _start+0x25 (/home/vudentz/git/bluez/src/bluetoothd) [0x40054c5] bluetoothd[941151]: +++++++++++++++++++++++++++ ==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4003311: parse_ctrl_mode (bluetooth.c:5033)
==941151==    by 0x4003311: parse_general (main.c:1074)
==941151==    by 0x4003311: parse_config (main.c:1341)
==941151==    by 0x4003311: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (bluetooth.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F17EE: parse_config_int (main.c:467)
==941151==    by 0x40F20C2: parse_config_u16.isra.0 (main.c:863)
==941151==    by 0x4003349: parse_general (bluetooth.c:2985)
==941151==    by 0x4003349: parse_config (main.c:1341)
==941151==    by 0x4003349: main (main.c:1616)

==941151== Address 0x1fff000050 is on thread 1's stack ==941151== in frame #2, created by parse_config_int (main.c:465) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004F55: parse_multi_profile (main.c:996)
==941151==    by 0x4004F55: parse_general (main.c:1078)
==941151==    by 0x4004F55: parse_config (main.c:1341)
==941151==    by 0x4004F55: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F15D4: parse_privacy (main.c:910)
==941151==    by 0x40033F9: parse_general (main.c:1081)
==941151==    by 0x40033F9: parse_config (main.c:1341)
==941151==    by 0x40033F9: main (main.c:1616)

==941151== Address 0x1fff000098 is on thread 1's stack ==941151== in frame #2, created by parse_privacy (main.c:909) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40047B5: parse_repairing (main.c:947)
==941151==    by 0x40047B5: parse_general (main.c:1082)
==941151==    by 0x40047B5: parse_config (main.c:1341)
==941151==    by 0x40047B5: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40049FF: parse_secure_conns (bluetooth.c:1597)
==941151==    by 0x40049FF: parse_general (main.c:1088)
==941151==    by 0x40049FF: parse_config (main.c:1341)
==941151==    by 0x40049FF: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (bluetooth.c:1570) ==941151==
bluetoothd[941151]: src/main.c:parse_config_bool() General.Experimental = true bluetoothd[941151]: GLib: g_error_free: assertion 'error != NULL' failed bluetoothd[941151]: ++++++++ backtrace ++++++++ bluetoothd[941151]: #1 log_handler+0x4e (src/main.c:1418) [0x40f0ffe] bluetoothd[941151]: #2 g_logv+0x247 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5477] bluetoothd[941151]: #3 g_log+0x93 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5803] bluetoothd[941151]: #4 parse_config_bool.isra.0+0x64 (src/main.c:208) [0x40f1f64]
bluetoothd[941151]: #5 main+0x7c0 (src/main.c:1089) [0x4003580] bluetoothd[941151]: #6 __libc_start_call_main+0x75 (/usr/lib64/libc.so.6) [0x4f90575]
bluetoothd[941151]: #7 __libc_start_main@@GLIBC_2.34+0x88 (/usr/lib64/libc.so.6) [0x4f90628]
bluetoothd[941151]: #8 _start+0x25 (/home/vudentz/git/bluez/src/bluetoothd) [0x40054c5] bluetoothd[941151]: +++++++++++++++++++++++++++ bluetoothd[941151]: src/main.c:parse_config_string() General.KernelExperimental = 6fbaf188-05e0-496a-9885-d6ddfdb4e03e bluetoothd[941151]: GLib: g_error_free: assertion 'error != NULL' failed bluetoothd[941151]: ++++++++ backtrace ++++++++ bluetoothd[941151]: #1 log_handler+0x4e (src/main.c:1418) [0x40f0ffe] bluetoothd[941151]: #2 g_logv+0x247 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5477] bluetoothd[941151]: #3 g_log+0x93 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5803]

bluetoothd[941151]: #4  parse_config_string+0x60 (src/main.c:208) [0x40f1400]
bluetoothd[941151]: #5  main+0x816 (src/main.c:1032) [0x40035d6]
bluetoothd[941151]: #6  __libc_start_call_main+0x75

(/usr/lib64/libc.so.6) [0x4f90575] bluetoothd[941151]: #7 __libc_start_main@@GLIBC_2.34+0x88 (/usr/lib64/libc.so.6) [0x4f90628] bluetoothd[941151]: #8 _start+0x25 (/home/vudentz/git/bluez/src/bluetoothd) [0x40054c5] bluetoothd[941151]: +++++++++++++++++++++++++++ bluetoothd[941151]: src/main.c:btd_parse_kernel_experimental() 6fbaf188-05e0-496a-9885-d6ddfdb4e03e
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4003607: parse_kernel_exp (main.c:1030)
==941151==    by 0x4003607: parse_general (main.c:1093)
==941151==    by 0x4003607: parse_config (main.c:1341)
==941151==    by 0x4003607: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F17EE: parse_config_int (main.c:467)
==941151==    by 0x40F19B8: parse_mode_config (main.c:552)
==941151==    by 0x40036D5: parse_le_config (bluetooth.c:2879)
==941151==    by 0x40036D5: parse_config (main.c:1343)
==941151==    by 0x40036D5: main (main.c:1616)

==941151== Address 0x1fff000020 is on thread 1's stack ==941151== in frame #2, created by parse_config_int (main.c:465) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004C4B: parse_gatt_cache (main.c:1103)
==941151==    by 0x4004C4B: parse_gatt (main.c:1171)
==941151==    by 0x4004C4B: parse_config (main.c:1344)
==941151==    by 0x4004C4B: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40F17EE: parse_config_int (main.c:467)
==941151==    by 0x40F2072: parse_config_u8.isra.0 (main.c:878)
==941151==    by 0x40037E5: parse_gatt (bluetooth.c:2847)
==941151==    by 0x40037E5: parse_config (main.c:1344)
==941151==    by 0x40037E5: main (main.c:1616)

==941151== Address 0x1fff000050 is on thread 1's stack ==941151== in frame #2, created by parse_config_int (main.c:465) ==941151==
bluetoothd[941151]: src/main.c:parse_config_string() GATT.ExportClaimedServices = read-write
bluetoothd[941151]: GLib: g_error_free: assertion 'error != NULL' failed bluetoothd[941151]: ++++++++ backtrace ++++++++ bluetoothd[941151]: #1 log_handler+0x4e (src/main.c:1418) [0x40f0ffe] bluetoothd[941151]: #2 g_logv+0x247 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5477] bluetoothd[941151]: #3 g_log+0x93 (/usr/lib64/libglib-2.0.so.0.8400.4) [0x4cc5803]

bluetoothd[941151]: #4  parse_config_string+0x60 (src/main.c:208) [0x40f1400]
bluetoothd[941151]: #5  main+0xae3 (src/main.c:1132) [0x40038a3]
bluetoothd[941151]: #6  __libc_start_call_main+0x75

(/usr/lib64/libc.so.6) [0x4f90575] bluetoothd[941151]: #7 __libc_start_main@@GLIBC_2.34+0x88 (/usr/lib64/libc.so.6) [0x4f90628] bluetoothd[941151]: #8 _start+0x25 (/home/vudentz/git/bluez/src/bluetoothd) [0x40054c5] bluetoothd[941151]: +++++++++++++++++++++++++++ ==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x400395E: parse_gatt_export (main.c:1129)
==941151==    by 0x400395E: parse_gatt (main.c:1178)
==941151==    by 0x400395E: parse_config (main.c:1344)
==941151==    by 0x400395E: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40040AD: parse_gatt_seclevel (main.c:1157)
==941151==    by 0x40040AD: parse_gatt (main.c:1179)
==941151==    by 0x40040AD: parse_config (main.c:1344)
==941151==    by 0x40040AD: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004A1E: parse_csis_sirk (bluetooth.c:1593)
==941151==    by 0x4004A1E: parse_csis (main.c:1197)
==941151==    by 0x4004A1E: parse_config (main.c:1345)
==941151==    by 0x4004A1E: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (bluetooth.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004A3D: parse_avdtp_session_mode (main.c:1276)
==941151==    by 0x4004A3D: parse_avdtp (main.c:1310)
==941151==    by 0x4004A3D: parse_config (main.c:1346)
==941151==    by 0x4004A3D: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004A5C: parse_avdtp_stream_mode (main.c:1293)
==941151==    by 0x4004A5C: parse_avdtp (main.c:1311)
==941151==    by 0x4004A5C: parse_config (main.c:1346)
==941151==    by 0x4004A5C: main (main.c:1616)

==941151== Address 0x1fff000100 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x4004417: parse_cs_role (bluetooth.c:943)
==941151==    by 0x4004417: parse_le_cs_config (main.c:1264)
==941151==    by 0x4004417: parse_config (main.c:1348)
==941151==    by 0x4004417: main (main.c:1616)

==941151== Address 0x1fff0000f8 is on thread 1's stack ==941151== in frame #2, created by main (bluetooth.c:1570) ==941151==
==941151== Invalid free() / delete / delete[] / realloc()

==941151==    at 0x4BADE43: free (vg_replace_malloc.c:990)
==941151==    by 0x4CC0444: g_free (in /usr/lib64/libglib-2.0.so.0.8400.4)
==941151==    by 0x40044FB: parse_config_signed_int (main.c:501)
==941151==    by 0x40044FB: parse_le_cs_config (main.c:1269)
==941151==    by 0x40044FB: parse_config (main.c:1348)
==941151==    by 0x40044FB: main (main.c:1616)
Quoted 3 lines

==941151== Address 0x1fff0000f8 is on thread 1's stack ==941151== in frame #2, created by main (main.c:1570) ==941151==

I think it needed something like the below.

I think it would be better to have _steal_() and _steal_fd_() be macros so that compiler can check the types so you can't mistake _steal_(keyfile) and _steal_(&keyfile)

https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/master/spa/include/spa/utils/cleanup.h

Not sure what can be done about the _cleanup_(g_free) char *str --- it would be great if this generated compiler warning/error but it looks like it didn't, so maybe this macro should be private so you're forced to declare the CLEANUP_FREEFUNC always.

src/main.c +15 -3
index 9053e74aa..9d1074c11 100644
@@ -205,8 +205,20 @@ static const struct group_table {
 	{ }
 };
 
-CLEANUP_FREEFUNC(GError, g_error_free);
-CLEANUP_FREEFUNC(GKeyFile, g_key_file_free);
+static void cleanup_g_error(GError *err)
+{
+	if (err)
+		g_error_free(err);
+}
+
+static void cleanup_g_key_file(GKeyFile *keyfile)
+{
+	if (keyfile)
+		g_key_file_free(keyfile);
+}
+
+CLEANUP_FREEFUNC(GError, cleanup_g_error);
+CLEANUP_FREEFUNC(GKeyFile, cleanup_g_key_file);
 
 static int8_t check_sirk_alpha_numeric(char *str)
 {
@@ -291,7 +303,7 @@ static GKeyFile *load_config(const char *name)
 		return NULL;
 	}
 
-	return _steal_(keyfile);
+	return _steal_(&keyfile);
 }
 
 static void parse_did(const char *did)
@@ -457,12 +469,19 @@ static bool parse_config_string(GKeyFile *config,

const char *group,
return true;
}

+static inline void cleanup_g_free(void *p)
+{
+ g_free(*(void **) p);
+}
+
+#define _cleanup_g_free_ _cleanup_(cleanup_g_free)
+
static bool parse_config_int(GKeyFile *config, const char *group,
const char *key, int *val, size_t min, size_t max)
{
size_t tmp;
- _cleanup_(g_free) char *str = NULL;
+ _cleanup_g_free_ char *str = NULL;
char *endptr = NULL;

if (!parse_config_string(config, group, key, &str))

and so on for the rest of _cleanup_(g_free)

Signature · 1 line
Pauli Virtanen
#13 lore raw

Hey,

Coming back to this nearly a month later.

Quoted 8 lines · Pauli Virtanen

I think it needed something like the below.

I think it would be better to have _steal_() and _steal_fd_() be macros
so that compiler can check the types so you can't mistake _steal_(keyfile) and _steal_(&keyfile)

https://gitlab.freedesktop.org/pipewire/pipewire/-/blob/master/spa/include/spa/utils/cleanup.h

I've implemented something "similar" (only so many ways to skin a cat...), thanks.

Quoted 6 lines

Not sure what can be done about the _cleanup_(g_free) char *str --- it
would be great if this generated compiler warning/error but it looks like it didn't, so maybe this macro should be private so you're forced
to declare the CLEANUP_FREEFUNC always.

Nowadays, calling g_free() just calls free():
https://github.com/GNOME/glib/blob/main/glib/gmem.c#L208
so we could replace this all with _cleanup_free_(), if we were to bump the glib requirements to match when glib started just calling free(), in 2.46 (released in 2021).

Or we could use g_autofree, but that also would require bumping glib requirements to 2.44.

I've implemented it the same way you did, copy/pasting the "free()" functions in src/shared/util.h

Quoted 10 lines
src/main.c +0 -0
index 9053e74aa..9d1074c11 100644
@@ -205,8 +205,20 @@ static const struct group_table {
  	{ }
  };

src/main.c +0 -2
-CLEANUP_FREEFUNC(GError, g_error_free);
-CLEANUP_FREEFUNC(GKeyFile, g_key_file_free);

src/main.c static const struct group_table { · line 217

Quoted 10 lines
src/main.c +10 -0
+static void cleanup_g_error(GError *err)
+{
+	if (err)
+		g_error_free(err);
+}
+
+static void cleanup_g_key_file(GKeyFile *keyfile)
+{
+	if (keyfile)
+		g_key_file_free(keyfile);

I added the "if p != NULL" conditional to the macro directly.

Quoted 41 lines
src/main.c +4 -0
+}
+
+CLEANUP_FREEFUNC(GError, cleanup_g_error);
+CLEANUP_FREEFUNC(GKeyFile, cleanup_g_key_file);


 static int8_t check_sirk_alpha_numeric(char *str) {

src/main.c +0 -0
@@ -291,7 +303,7 @@ static GKeyFile *load_config(const char *name)
  		return NULL;
  	}

src/main.c +1 -1
-	return _steal_(keyfile);
+	return _steal_(&keyfile);

 }

 static void parse_did(const char *did)

src/main.c +0 -0
@@ -457,12 +469,19 @@ static bool parse_config_string(GKeyFile
 *config,
 const char *group,
  	return true;
  }

src/main.c +7 -0
+static inline void cleanup_g_free(void *p)
+{
+	g_free(*(void **) p);
+}
+
+#define _cleanup_g_free_ _cleanup_(cleanup_g_free)
+

 static bool parse_config_int(GKeyFile *config, const char *group, const char *key, int *val, size_t min, size_t max) {
  size_t tmp;

src/main.c +1 -1
-	_cleanup_(g_free) char *str = NULL;
+	_cleanup_g_free_ char *str = NULL;

  char *endptr = NULL;

  if (!parse_config_string(config, group, key, &str))

and so on for the rest of _cleanup_(g_free)